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 [-h | --help] [-f | --force] INTERFACE METRIC

PARAMETERS

-h, --help
    Display usage summary and exit.

-f, --force
    Apply metric even if interface is down.

INTERFACE
    Name of the target network interface (e.g., eth0).

METRIC
    Integer metric value (typically 1-1000; lower is higher priority).

DESCRIPTION

The ifmetric command adjusts the routing metric associated with a specific Linux network interface. Routing metrics determine route precedence in the kernel's forwarding table; lower values indicate higher priority, influencing which gateway or route is selected for outbound traffic.

This utility is particularly useful in multi-homed systems or laptops switching between wired Ethernet and wireless connections. For example, assigning a low metric (e.g., 1) to a primary Ethernet interface and a higher one (e.g., 200) to Wi-Fi ensures Ethernet is preferred.

ifmetric operates by writing the specified metric value to /proc/sys/net/ipv4/conf/<interface>/metric for IPv4 or the IPv6 equivalent. It automatically detects the address family based on the interface configuration. Changes take effect immediately but are not persistent; they must be reapplied on boot or interface events, often via /etc/network/if-up.d/ hooks or systemd network scripts.

Root privileges are required, as it modifies kernel parameters. It's lightweight, dependency-free, and ideal for scripts automating network prioritization without full routing table manipulation.

CAVEATS

Requires root access. Changes are lost on reboot or interface restart unless scripted. Interface must exist (unless -f used). Affects only the specified interface's default route metric.

EXAMPLES

ifmetric eth0 10
Sets Ethernet metric to 10 (high priority).

ifmetric -f wlan0 250
Forces Wi-Fi metric to 250 even if down.

FILES

Modifies /proc/sys/net/ipv4/conf/INTERFACE/metric or IPv6 equivalent.

HISTORY

Developed for Debian systems as part of the ifmetric package (since ~2005). Evolved for use in ifupdown scripting to handle dynamic metrics without ip route complexity.

SEE ALSO

ip(8), route(8), ifup(8)

Copied to clipboard