netplugd Command in Linux



The netplugd is a daemon that manages changes in network connections on your Linux system, specifically when network interfaces are connected or disconnected from the network (e.g., when an Ethernet cable is plugged in or unplugged).

  • When a network interface (like an Ethernet port) is connected to a powered-on device (like a switch or another computer), it establishes a "carrier signal" that indicates the link is active. If the link goes down (e.g., the cable is unplugged), the carrier signal disappears.
  • netplugd listens for these changes (carrier signal detection or loss) using the Linux kernel's netlink interface. This interface allows the kernel to notify programs like netplugd when the status of a network interface changes (e.g., when it goes up or down).
  • When netplugd detects that a carrier signal has appeared on an interface (meaning a cable was plugged in or the link was re-established), it runs a script to bring the network interface up. Similarly, if the carrier signal is lost (meaning the cable was unplugged or the connection failed), it runs a script to bring the interface down.
  • netplugd doesn't decide how the network interfaces should be managed independently. Instead, it leaves that up to a script located at /etc/netplug.d/netplug. You can customize this script to define exactly what actions should be taken when an interface goes up or down.

In addition, you can specify which network interfaces netplugd should manage by providing a list of interface names using shell-style patterns (called "glob patterns"). For example −

  • eth[13] would match eth1 and eth3.
  • If you use *, it will match all interfaces that start with eth.

Suppose you specify interfaces that are not currently available (e.g., a PCMCIA network card that is not plugged in yet). In that case, netplugd will start managing them as soon as they become available, either by being plugged in or by loading the appropriate device drivers.

Table of Contents

Here is a comprehensive guide to the options available with the netplugd command −

Syntax of netplugd Command

The syntax for the netplugd command is as follows −

netplugd [options]

netplugd Command Options

The following is a breakdown of the different options available for the netplugd command −

Options Description
-F Run in the foreground; do not detach and run as a daemon. Messages are logged to stdout or stderr instead of using the syslog mechanism. This is mainly useful for debugging your configuration.
-P Prevent autoprobing for interfaces. netplugd normally probes for all possible interface names that might match the patterns you tell it to manage. This is needed to load and set up network driver modules. Autoprobing is generally safe and quick, but you can disable it with caution.
-c config_file Specify a configuration file with interface patterns. You can provide multiple config files. If not provided, netplugd tries to read from a default config file. To skip real config files, you can use /dev/null.
-i interface_pattern Specify a pattern that will be used to match interface names that netplugd should manage. You can provide this option multiple times to specify multiple patterns.
-p pid_file Write the daemon's process ID to the file pid_file. If you tell netplugd to run in the foreground, this option is ignored.

Different Files Available for netplugd

The following is a breakdown of different file descriptions that allow you to configure and manage how netplugd responds to network link events on your system −

Tag Description
/etc/netplug/netplugd.conf This is the default configuration file that netplugd reads if none is specified on the command line. The file should contain one pattern per line. It ignores whitespace, empty lines, and comments that start with a #. Patterns follow standard shell-style glob patterns, such as eth[0-9] to match interfaces like eth0, eth1, etc.
/etc/netplug.d/netplug

This is the "policy" program, typically a shell script, that netplugd uses to manage interfaces. It performs actions based on network link events. This program is called with the name of the interface as its first argument, and one of the following options −

in: This option indicates that a cable was plugged in or the carrier came up. The script should bring the interface up and exit with a status code of 0 if successful.

out: This option indicates that a cable was unplugged or the carrier went down. The script should bring the interface down and exit with a status code of 0 if successful.

probe: This option is used to load and initialize the driver for the interface if possible and bring it to the "up" state. The script is run synchronously and must exit with a status code of 0 if successful, otherwise with a non-zero exit code or signal.

/etc/rc.d/init.d/netplugd This is the init script that starts, stops, and displays the status of the netplugd daemon. It is typically used to manage the netplugd service during system startup and shutdown.

Examples of netplugd Command in Linux

In this section, we'll explore various examples that should help you understand how to use netplugd to manage network interfaces dynamically based on link events −

Running netplugd in Foreground Mode

By default, netplugd runs as a background service (daemon), but with the "-F" flag, it will run in the foreground, allowing you to see its output directly in the terminal instead of running silently in the background.

sudo netplugd -F

This command keeps netplugd running in the foreground, logging messages to stdout or stderr.

netplugd Command in Linux1

Preventing Autoprobing of Interfaces

To run netplugd without autoprobing for interfaces, you can simply run the netplugd command with the "-P" option −

sudo netplugd -P

This command starts netplugd but prevent it from probing for all possible interface names.

netplugd Command in Linux2

Using a Specific Configuration File

If you want netplugd to monitor a specific set of interfaces, such as eno1 or wlp2s0, and you have a specific configuration file ( /e.g etc/netplug.d/my_netplugd.conf) that includes those patterns, you can use the following command −

sudo netplugd -c /etc/netplug.d/my_netplugd.conf
netplugd Command in Linux3

Specifying Interface Patterns

To specify which network interfaces netplugd should manage, you can use the "-i" option followed by a glob pattern (a shell-style pattern) that matches the interface names you want it to handle.

For instance, if you want netplugd to manage only the interfaces eth0 and eth1, you can simply run −

sudo netplugd -i "eth[01]"
netplugd Command in Linux4

Writing the Daemon's Process ID to a File

To write the daemon's process ID to a file, you can use the following command −

sudo netplugd -p /var/run/netplugd.pid

This command writes netplugd's process ID to the specified file.

netplugd Command in Linux5

Conclusion

netplugd ability to dynamically manage network interfaces, coupled with options for running in the foreground or adjusting process handling, makes it a powerful tool for maintaining network connectivity in both static and dynamically changing network environments.

Advertisements