Hot-Add NIC in Ubuntu on VMWareSometimes downtime is unacceptable, even if that downtime is only the 10 seconds it takes for a virtual server to reboot. I recently ran into the situation where I needed to add a new NIC to a virtual server to give it access to a second network. After adding the NIC in vSphere, it did not show up on the server. I had assumed that, like RedHat-based distros, Ubuntu would always be monitoring for hardware changes and update accordingly. It does not. Sure, a reboot would fix it, but that would take the public website down and that's not acceptable.
This, however, can be done without taking down the VM. There is a proper order to do this so that the new network comes up automatically. Let's start with the original network configuration:
root@server:~# cat /etc/network/interfaces auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 10.0.0.100 netmask 255.255.255.0 network 10.0.0.0 broadcast 10.0.0.255 gateway 10.0.0.1 dns-nameservers 126.96.36.199
We only have a single NIC, eth0, and it has a static address with a default gateway, as it should. We want to add a second NIC, eth1, to be on the 192.168.0/24 network. There are two caveats, however.
- When we add the configuration for the second NIC to the interfaces file we CAN NOT add in a gateway. There can only be ONE gateway per host.
- We need this server to be able to contact hosts on other 192.168/16 networks, so we do need some type of gateway. Instead of a gateway, however, we will use a static route.
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 10.0.0.100 netmask 255.255.255.0 network 10.0.0.0 broadcast 10.0.0.255 gateway 10.0.0.1 dns-nameservers 188.8.131.52 auto eth1 iface eth1 inet static address 192.168.0.100 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255
Notice how there is NOT a gateway for eth1. At this point, the server can only contact hosts on the 192.168.0/24 subnet. Let's add the static route (and also ensure the route gets added every time the system boots).
root@server:~# vi /etc/rc.local #!/bin/sh -e /sbin/ip route add 192.168.0.0/16 via 192.168.0.1 dev eth1
Ok, by editing the rc.local file we've ensure that the route gets added each time the system boots as rc.local is the last startup script that gets executed. But the route isn't in effect yet because we didn't actually run the command. So we have two things left to do:
- Rescan the pci bus so the new NIC we added in vSphere is detected
- Execute rc.local so our static route gets added
root@server:~# echo "1" > /sys/bus/pci/rescan
Now if you check the output of ifconfig you'll see eth1 with all of the settings you already added to your interfaces file (i.e., the NIC is already working).
Now the static route is active allowing our server to not only access hosts on 192.168.0/24 but the entire 192.168/16 netblock.