Creative Commons

2013-06-07 10:23:06

Hot-Add NIC in Ubuntu on VMWare

Sometimes 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 netmask network broadcast gateway dns-nameservers

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.
  1. 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.
  2. 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.
First, let's update the interfaces file to reflect the new NIC:
auto lo iface lo inet loopback auto eth0 iface eth0 inet static address netmask network broadcast gateway dns-nameservers auto eth1 iface eth1 inet static address netmask network broadcast

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 via 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:
  1. Rescan the pci bus so the new NIC we added in vSphere is detected
  2. 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).
root@server:~# /etc/rc.local

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.



2014-04-13 06:25:00

wtf says...

2014-04-13 06:25:27

---_____--- says...

2014-04-13 06:26:07

SWAG: You need to designate an interface such as says...

2016-08-11 02:11:15

LoL says...
Wzap Guys

Post a comment!