A Script to Install & Configure ifplugd on Debian

The default configuration on some older Linux systems is to only send a DHCP request while booting up. This means if the network cable gets unplugged, or if the router is powered off, the system may lose its IP configuration. To restore the network connection, the system may need to be manually rebooted or have someone at the local console run the dhclient command to request a DHCP lease.

For systems that are only accessed remotely via SSH, such a scenario can be painful. What is needed is a daemon that watches the link status of the Ethernet jack and reconfigures the network (or sends out another DHCP request) when it detects a cable is plugged in (or the power to the router is restored).

ifplugd does exactly that:

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled.

On a Debian system, installing and configuring ifplugd is relatively simple using apt-get install ifplugd. Once its been installed, it needs to be configured by editing /etc/default/ifplugd. The most basic configuration is to simply set INTERFACES="auto" and HOTPLUG_INTERFACES="all". This configuration tells ifplugd to watch all network interfaces for a new link status and automatically reconfigure them using the Debian network configuration defined in /etc/network/interfaces.

I recently needed to automate the install and configuration of ifplugd on many remote Linux systems, so I wrote this simple script.

Download: install-ifplugd.tar.gz

[sourcecode lang="bash"]
#!/bin/sh

#########################################
# Author: Raam Dev
#
# This script installs ifplugd and configures
# it to automatically attempt to restore any
# lost connections.
#
# Must be run as root!
#########################################

# Check if we're running this as root
if [ $EUID -ne 0 ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

# Files used when configuring ifplugd
OUTFILE=/tmp/outfile.$$
CONFIG_FILE=/etc/default/ifplugd

# Update package list and install ifplugd, assuming yes to any questions asked
# (to insure the script runs without requiring manual intervention)
apt-get update --assume-yes ; apt-get install --assume-yes ifplugd

# Configure ifplugd to watch all interfaces and automatically attempt configuration
sed 's/INTERFACES=""/INTERFACES="auto"/g' < $CONFIG_FILE > $OUTFILE
mv $OUTFILE $CONFIG_FILE

sed 's/HOTPLUG_INTERFACES="auto"/HOTPLUG_INTERFACES="all"/g' < $CONFIG_FILE > $OUTFILE
mv $OUTFILE $CONFIG_FILE

[/sourcecode]

If you're interested in doing more with ifplugd, check out this article.

Write a Comment

Comment