Stackvora.tech

Replace ifconfig and route With ip Command

For decades, the ifconfig and route utilities were the de facto standard for configuring and querying network interfaces and routing tables on Linux…

For decades, the ifconfig and route utilities were the de facto standard for configuring and querying network interfaces and routing tables on Linux systems. While still present on many distributions for backward compatibility, they are considered deprecated. The modern, more powerful, and consistent alternative is the ip command, part of the iproute2 suite. This article details the transition from older tools to ip, covering common network configuration and troubleshooting tasks.

The iproute2 utilities provide a unified interface for managing network configuration, offering finer-grained control and support for advanced features like policy routing, VLANs, and VRF (Virtual Routing and Forwarding) instances. The move to ip is not just a syntax change; it represents a shift to a more robust and scalable networking paradigm on Linux.

Interface Management: ip link and ip addr

The ip link command is used for managing the state of network interfaces themselves (e.g., bringing them up or down, changing MAC addresses, adjusting MTU). ip addr (or ip address) is for managing IP addresses assigned to those interfaces.

Viewing Interface Status

To list all network interfaces and their current status (link state, MAC address, MTU), previously done with ifconfig -a, now use:

ip link show

For more detailed information, including assigned IP addresses (equivalent to ifconfig <interface>):

ip addr show

To view details for a specific interface, such as eth0:

ip link show eth0
ip addr show eth0

Output for ip addr show eth0 might look like this:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:ab:cd:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
       valid_lft 42930sec preferred_lft 42930sec
    inet6 fe80::20c:29ff:feab:cdef/64 scope link 
       valid_lft forever preferred_lft forever

Configuring Interfaces

  • Bringing an interface up/down:
    sudo ip link set eth0 up
    sudo ip link set eth0 down
    This replaces ifconfig eth0 up and ifconfig eth0 down.
  • Assigning an IP address:
    sudo ip addr add 192.168.1.200/24 dev eth0
    This replaces ifconfig eth0 192.168.1.200 netmask 255.255.255.0.
  • Removing an IP address:
    sudo ip addr del 192.168.1.200/24 dev eth0
  • Changing MAC address:
    sudo ip link set dev eth0 address 00:11:22:33:44:55
    Note: The interface must typically be down before changing its MAC address for some drivers.
  • Changing MTU:
    sudo ip link set dev eth0 mtu 9000

Routing Table Management: ip route

The ip route command is the successor to the route command for managing the kernel's IP routing tables.

Viewing Routing Tables

To display the kernel's IP routing table (previously route -n or netstat -rn):

ip route show

A typical output might include:

default via 192.168.1.1 dev eth0 proto dhcp metric 100 
10.0.0.0/8 via 192.168.1.10 dev eth0 
172.16.0.0/16 dev tun0 proto kernel scope link src 172.16.0.1 
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100

To show entries from a specific routing table (e.g., table 100):

ip route show table 100

Adding and Deleting Routes

  • Adding a default gateway:
    sudo ip route add default via 192.168.1.1 dev eth0
    This replaces route add default gw 192.168.1.1.
  • Adding a static route to a network:
    sudo ip route add 10.20.0.0/24 via 192.168.1.1 dev eth0
    This replaces route add -net 10.20.0.0 netmask 255.255.255.0 gw 192.168.1.1. The dev eth0 is optional if the gateway is directly reachable via eth0's subnet.
  • Adding a static route to a host:
    sudo ip route add 172.16.1.100 via 192.168.1.1 dev eth0
  • Deleting a route:
    sudo ip route del 10.20.0.0/24 via 192.168.1.1 dev eth0
    Note that the deletion command must exactly match the addition command parameters.

ARP/NDP Cache Management: ip neigh

The ip neigh (or ip neighbour) command manages the Address Resolution Protocol (ARP) cache for IPv4 and Neighbor Discovery Protocol (NDP) cache for IPv6.

Viewing ARP/NDP Cache

To display the current ARP/NDP cache (previously arp -a):

ip neigh show

Example output:

192.168.1.1 dev eth0 lladdr 00:50:56:c0:00:08 REACHABLE
192.168.1.254 dev eth0 lladdr 00:50:56:f0:00:02 STALE

Adding and Deleting ARP Entries

While usually managed automatically, static ARP entries can be useful for security or specific network configurations.

  • Adding a static ARP entry:
    sudo ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0 permanent
  • Deleting an ARP entry:
    sudo ip neigh del 192.168.1.100 dev eth0

Policy Routing and Traffic Control: ip rule, ip route

iproute2 significantly enhances routing capabilities beyond a single main routing table. With ip rule, you can define rules that look at various packet attributes (source address, destination address, interface, etc.) to determine which routing table to use. This enables policy-based routing.

Viewing and Adding Rules

To view existing routing policy rules:

ip rule show

A basic output:

0:      from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default

To add a rule that sends traffic from source IP 192.168.5.10 to routing table 100:

sudo ip rule add from 192.168.5.10 table 100

Then, you'd define routes within table 100 using ip route add ... table 100.

Persistent Configuration

Commands executed with ip are ephemeral; they are lost upon reboot. For persistent configuration, you must use your distribution's specific network configuration management system. Common examples include:

  • Netplan (Ubuntu 17.10+): YAML-based configuration in /etc/netplan/*.yaml.
  • NetworkManager (Fedora, RHEL, CentOS, newer Ubuntu/Debian): Config files in /etc/NetworkManager/system-connections/ or nmcli utility.
  • systemd-networkd (Arch, some RHEL/CentOS, embedded systems): Config files in /etc/systemd/network/.
  • Traditional ifupdown (Debian, older Ubuntu): Configuration in /etc/network/interfaces.

Example Netplan configuration for a static IP address and route:

# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      routes:
        - to: 10.20.0.0/24
          via: 192.168.1.1
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

After modifying Netplan files, apply changes with sudo netplan apply.

Diagnostic Tools: ss instead of netstat

The netstat command, like ifconfig and route, is deprecated and replaced by ss (Socket Statistics). ss provides more detailed information, works faster, and is part of the iproute2 suite.

Viewing Socket Statistics

To list all TCP connections (similar to netstat -at):

ss -t

To list all UDP connections (similar to netstat -au):

ss -u

To list all listening sockets with process info (similar to netstat -tulpn):

ss -tulpn
  • -t: Show TCP sockets
  • -u: Show UDP sockets
  • -l: Show listening sockets
  • -p: Show process using socket
  • -n: Don't resolve service names or hostnames

For more detailed information, including kernel socket states and connection timers, ss is invaluable.

Summary of Old vs. New Commands

Old Command New iproute2 Command Description
ifconfig -a ip link show / ip addr show Show all interfaces and their addresses
ifconfig eth0 ip addr show eth0 Show info for a specific interface
ifconfig eth0 up ip link set eth0 up Bring interface up
ifconfig eth0 192.168.1.100/24 ip addr add 192.168.1.100/24 dev eth0 Add an IP address
route -n ip route show Show routing table
route add default gw 192.168.1.1 ip route add default via 192.168.1.1 dev eth0 Add default gateway
route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1 ip route add 10.0.0.0/8 via 192.168.1.1 dev eth0 Add static route to network
arp -a ip neigh show Show ARP/NDP cache
netstat -tulpn ss -tulpn Show listening sockets and processes

Common Pitfalls

  • Ephemeral Changes: Remember that any changes made directly with ip commands are temporary. Always ensure your persistent configuration files (Netplan, NetworkManager, systemd-networkd, etc.) reflect desired states. For quick testing, ip is great; for production, always use the persistence layer.
  • Interface Naming: Modern Linux distributions often use Predictable Network Interface Names (e.g., enp0s3, ens33) instead of generic eth0. Always verify the correct interface name with ip link show before applying commands.
  • dev Parameter: When adding routes, explicitly specifying the outgoing interface with dev <interface> can prevent ambiguity, especially in multi-homed systems or when the gateway is on a different subnet than the interface's primary IP.
  • Lack of Root Privileges: Most configuration commands (e.g., adding addresses, routes, bringing interfaces up/down) require root privileges, so remember to prefix with sudo. Querying commands (ip link show, ip addr show, ip route show, ip neigh show, ss) typically do not.
  • Incorrect CIDR Notation: When specifying IP addresses or networks, ip universally uses CIDR notation (e.g., 192.168.1.100/24) rather than separate netmask parameters. Ensure you use the correct CIDR prefix.

Back to the knowledge base · Ask the AI assistant