Linux

Linux Networking with ip(8)

·2 min read

The net-tools package (ifconfig, route, netstat) has been deprecated for a decade. The replacement — iproute2, primarily its ip command — is more capable and more consistent. If you're still typing ifconfig by muscle memory, it's time to rewrite that muscle memory.

Listing interfaces and addresses

ip link              # interfaces, MAC addresses, MTU, state
ip addr              # interfaces with their IP addresses
ip -br addr          # brief one-line-per-interface view
ip -c addr           # colorized

The -br flag is criminally underused. For a quick "what does this box look like":

$ ip -br -c addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
eth0             UP             10.0.1.42/24 fe80::5054:ff:fe12:34/64
wg0              UNKNOWN        10.88.0.1/24

Routes

ip route                       # main table
ip route get 1.1.1.1           # which interface and gateway will be used?
ip route add 192.168.50.0/24 via 10.0.1.1 dev eth0
ip route del 192.168.50.0/24

Bringing interfaces up and down

ip link set eth0 up
ip link set eth0 down
ip link set eth0 mtu 1400
ip link set eth0 address 02:42:ac:11:00:42

Adding and removing addresses

ip addr add 10.0.1.42/24 dev eth0
ip addr add 192.168.1.50/24 dev eth0    # secondary address — eth0 now has both
ip addr del 192.168.1.50/24 dev eth0
ip addr flush dev eth0                   # remove all addresses

Persistence

Commands above are runtime-only. Reboot and they're gone. For persistence: on Debian-family use /etc/network/interfaces or systemd-networkd; on RHEL-family use NetworkManager (nmcli) or networkd. nmcli is itself worth learning:

nmcli connection show
nmcli connection modify eth0 ipv4.addresses 10.0.1.42/24
nmcli connection modify eth0 ipv4.gateway 10.0.1.1
nmcli connection modify eth0 ipv4.method manual
nmcli connection up eth0

Network namespaces

Namespaces give you isolated network stacks — separate interfaces, routes, firewall rules. Containers use them; you can too:

ip netns add testns
ip netns exec testns ip link set lo up
ip netns exec testns curl https://example.com

Replacing netstat

netstat is deprecated in favour of ss (also from iproute2). Same use cases, faster:

ss -tulnp               # TCP+UDP, listening, numeric, process
ss -tnp state established      # established TCP connections
ss -tn dport = :443     # connections to port 443

Related