LinuxCommandLibrary

ifmetric

Configure interface metric (routing priority)

TLDR

Set the priority of the specified network interface (a higher number indicates lower priority)

$ sudo ifmetric [interface] [value]
copy

Reset the priority of the specified network interface
$ sudo ifmetric [interface] [0]
copy

SYNOPSIS

ifmetric <interface> <metric>

PARAMETERS

interface
    The name of the network interface for which the routing metric is to be set (e.g., eth0, wlan0).

metric
    An integer value representing the desired routing metric. Lower values indicate a higher preference for routes associated with this interface. Common values range from 0 to 65535.

DESCRIPTION

ifmetric is a utility used to set the routing metric for network interfaces on Linux systems.
The metric is a value associated with a route in the kernel's routing table, indicating its preference. A lower metric value signifies a more preferred route.
This command is particularly useful when multiple network interfaces on a system might provide a default route (e.g., a wired connection and a wireless connection).
By assigning different metrics, administrators can control which interface's default route is used by the system for outbound traffic.
For instance, if eth0 has a metric of 100 and wlan0 has a metric of 200, traffic will prefer the route via eth0 if both offer a default route.
ifmetric modifies existing routes or influences routes added subsequently by other network configuration tools (like DHCP clients) to ensure they are assigned the desired metric.
It operates by interacting with the kernel's routing subsystem, typically using netlink sockets, similar to ip route.

CAVEATS

ifmetric typically adjusts the metric of routes that are already present or are added via the specified interface. It does not create routes itself.
It's often used in conjunction with DHCP clients (e.g., dhclient, dhcpcd) where it can be called from client hooks (like /etc/dhcp/dhclient-exit-hooks.d/).
Its functionality can largely be replicated and managed more granularly using the ip route command from the iproute2 suite, which offers more comprehensive routing control.
The effectiveness depends on how the kernel's routing table is managed and how other network services interact with it.
Requires root privileges to execute.

PURPOSE IN DHCP SCRIPTS

ifmetric is frequently utilized within DHCP client scripts or hooks. When a DHCP client obtains an IP address and default route, ifmetric can be invoked to set a specific metric for that interface's routes, ensuring its priority relative to other network connections. This allows for automated network failover or preference management.

REQUIRED PRIVILEGES

Executing ifmetric requires superuser privileges (root). This is because it modifies the kernel's routing table, a system-wide networking configuration. Users typically run it with sudo.

HISTORY

ifmetric is a relatively simple utility, often provided as a standalone package or within smaller network tool collections.
Its design addresses the specific need to easily adjust route metrics, especially in dynamic networking environments where interfaces might come up and down, and routes are automatically added.
While ip route provides similar and more powerful capabilities, ifmetric offers a more direct and concise way to handle the metric aspect specifically, making it convenient for scripting or simple configurations. It predates widespread iproute2 adoption in many scripts.

SEE ALSO

ip(8), route(8), dhclient(8), dhcpcd(8)

Copied to clipboard