Becoming Unplugged

Learn how to install and setup ifplugd, a utility that configures and reconfigures your network as an Ethernet cable is connected and disconnected.
If you have a laptop and move from environment to environment, you’ve likely encountered different types of networks. Some networks are wired, yet an increasing number of networks are wireless. Wouldn’t it be nice if you could switch between the two types with little or no effort?
Your wish has been granted. (Long live Jambi!)
ifplugd is a Linux daemon that automatically configures an Ethernet device when a cable is plugged in and automatically “unconfigures” it if the cable is pulled. ifplugd leverages your distribution’s native network configuration utilities (including ifup and ifdown), can log to syslog, and supports multiple interfaces.
ifplugd, licensed under the GPL, is available from http://www.stud.uni-hamburg.de/users/lennart/projects/ifplugd/. As the magazine goes to press, the current version is 0.27. The only prerequisite for igfplugd is libdaemon, a lightweight C library that eases the writing of Unix daemons. libdaemon is available from http://0pointer.de/lennart/projects/libdaemon/. Both ifplugd and libdaemon were written by Lennart Poettering.
Installation for libdaemon is the usual ./configure&&make&&make install. Once libdaemon is installed, download, verify, and unpack the ifplugd source tarball, and run the usual ./configure&&make&&make install. make install should detect the appropriate place for your init scripts and will drop the two binaries comprising the ifplugd package into /usr/local/sbin/. (You may need to update the PKG_CONFIG_PATH environment variable to include the location of libdaemon.pc, which defaults to /usr/local/lib/pkgconfig/.)
Now that everything is installed, you can move on to configuration.
All configuration options are held in ifplugd.conf, which by default can be found in /usr/local/etc/ifplugd/. This file is a Bourne shell script sourced by the init script to start ifplugd with the appropriate arguments. The sample configuration file contains the following:
ARGS="–fwI –u0 –d10"
You may specify more than one Ethernet interface in INTERFACES by separating each with a space. For each interface, a separate instance of ifplugd is spawned.
The arguments specified in ARGS are appended to ifplugd’s command-line. To specify arguments for a specific interface, you can use ARGS_ interface, such as ARGS_eth1, but be aware that ARGS is ignored if you do. For a full list of parameters, type ifplugd –h.
The default ARGS line tells ifplugd to ignore detection failure (–f), to wait for the daemon to fork (–w), to not exit on non-zero return values (–I), to wait 0 seconds to configure the interface (–u0), and to wait for a connection to be marked as lost for 10 seconds before downing it (–d10).
Next, look at the ifplugd.action script. If your distribution doesn’t come with ifup and ifdown, change those references to ifconfig $1 up and ifconfig $1 down, respectively. You can also add other actions you’d like performed to this script.
You are now ready to start ifplugd. Execute the init script that was installed earlier with the start parameter. You should see something similar to:
Starting Network Interface Plugging Daemon: eth0.
You can test your setup simply by unplugging your network cable. Watch for messages in /var/log/messages. Monitor this file for any errors and make any needed adjustments. Now plug the cable back in and verify that it too works as expected.
Since ifplugd’s actions are really just scripts, you can do almost anything as an action. That’s incredibly powerful. You can reconfigure routes, modify your iptables to suit different networks, and start certain services only when you’re connected to your home network. The sky (or at least your scripting ability) is the limit.
You can also use ifmetric, another tool written by Poettering, in conjunction with two interfaces controlled by ifplugd to ensure that all network traffic always goes over the fastest connection available.
ifplugd is a small but powerful program that lets you easily script how your network should act when an Ethernet cable is attached or unplugged. It allows you to spend less time configuring your network and more time using it. Enjoy.

Jeremy Garcia is the founder and administrator of LinuxQuestions.org, a free, friendly, and active Linux community. Please send questions and feedback to class="emailaddress">jeremy@linuxquestions.org.

Comments are closed.