Ubuntu Server: Acquire an IP from a newly connected DHCP server automatically

dhcpUbuntu

I know how to request that a machine to acquire a new IP address, however I've got a slightly different scenario.

I have configured a box locally on my network which has DHCP. Therefore, it acquired an IP address. I want to ship it to somebody on a different network which also has DHCP. Ideally, they would plug in their Ethernet cable before starting up their machine; in reality, they will just plug in cables as fast as they can.

The problem I've seen: they plug in the power first, and then the Ethernet. The machine doesn't automatically acquire an IP address using the new DHCP server unless we have them restart the box (or, if we had them log in, by restarting networking or forcing DHCP to release/renew).

Is there a way to configure an Ubuntu server (10.04 LTS) to automatically try to release/renew any time that an Ethernet cable is connected?

Best Answer

It looks like one common way to handle this is to add a udev rules file. I believe this gets installed by some package on a desktop system, but I cannot find it on any of my Ubuntu or Debian servers.

Here is a blog that seems to have some details.

The blog suggests adding a udev rules like this.

/etc/udev/rules.d/85-ifupdown.rules

# This file causes network devices to be brought up or down as a result
# of hardware being added or removed, including that which isn't ordinarily
# removable.
# See udev(7) for syntax.

SUBSYSTEM=="net", GOTO="net_start"
GOTO="net_end"

LABEL="net_start"

# Bring devices up and down only if they're marked auto.
# Use start-stop-daemon so we don't wait on dhcp
ACTION=="add",          RUN+="/sbin/start-stop-daemon --start --background --pidfile /var/run/network/bogus --startas /sbin/ifup -- --allow hotplug $env{INTERFACE}"

ACTION=="remove",       RUN+="/sbin/start-stop-daemon --start --background --pidfile /var/run/network/bogus --startas /sbin/ifdown -- --allow hotplug $env{INTERFACE}"

LABEL="net_end"

If you look at the ifup command being used you will notice that it only applies to hotplug interfaces. So be sure to use allow-hotplug instead of auto in your network interfaces file.