Linux routing decides which network interface and gateway packets are sent through. The modern tool is the ip command from the iproute2 package.
Essential ip Commands
# Show all IP addresses
ip addr show
ip a # short form
# Show the routing table
ip route show
ip r # short form
# Show the route to a specific host
ip route get 8.8.8.8
# ARP table (IP → MAC mapping)
ip neigh show
Temporary Changes (until reboot)
# Add an IP address to an interface
sudo ip addr add 192.168.1.100/24 dev eth0
# Remove an IP address
sudo ip addr del 192.168.1.100/24 dev eth0
# Add a route through a specific gateway
sudo ip route add 10.0.0.0/8 via 192.168.1.1
# Add the default gateway
sudo ip route add default via 192.168.1.1
# Remove a route
sudo ip route del 10.0.0.0/8
# Bring the interface up/down
sudo ip link set eth0 up
sudo ip link set eth0 down
Persistent Routes via Netplan (Ubuntu 22.04+)
# /etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.10/24
routes:
- to: default
via: 192.168.1.1
- to: 10.0.0.0/8
via: 10.0.0.1
metric: 100
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
sudo netplan apply
Two Interfaces with Different Gateways
# /etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0: # Main one (internet)
dhcp4: false
addresses: [1.2.3.4/24]
routes:
- to: default
via: 1.2.3.1
eth1: # Private network
dhcp4: false
addresses: [10.0.0.10/24]
routes:
- to: 10.0.0.0/8
via: 10.0.0.1
ifconfig command is deprecated and not installed by default in Ubuntu 20.04+. Use ip addr and ip route — they support IPv6 and modern kernel features.Diagnosing common routing errors
"RTNETLINK answers: Network is unreachable" when adding a route through a gateway means the given gateway is not directly reachable — it is not in the same subnet as the interface address.
sudo ip route add 10.0.0.0/8 via 192.168.1.1
RTNETLINK answers: Network is unreachable
Check that the interface already has an address from the same subnet as the gateway: ip addr show dev eth0. If the gateway is 192.168.1.1 but the interface only has address 10.0.0.5/24, the route cannot be added until an address from network 192.168.1.0/24 appears.
After netplan apply, the internet disappears completely — almost always a YAML indentation mistake (YAML is strict about spaces), or a conflict with NetworkManager/systemd-networkd managing the same interface.
sudo netplan apply
Error in network definition //etc/netplan/01-netcfg.yaml: invalid indentation
Check the syntax before applying: sudo netplan try — the command shows the error and automatically rolls back the change after 120 seconds if you do not confirm it, which saves you from losing SSH access over a typo.
Route configuration methods compared
| Method | Persistence | Where it is used | Syntax |
|---|---|---|---|
| ip route add | Until reboot | Any Linux with iproute2 | CLI command |
| Netplan (YAML) | Permanent | Ubuntu 18.04+ | YAML declaration |
| /etc/network/interfaces | Permanent | Debian, older Ubuntu | Key-value |
| NetworkManager (nmcli) | Permanent | Desktop distributions, RHEL/CentOS | CLI or GUI |
When static routes are not enough
- The network topology changes often (multiple providers, backup links) — you need dynamic routing (BGP, OSPF) through FRRouting or Bird, not a list of static entries.
- You need automatic failover between providers — a static route does not switch itself when a link goes down; that needs dedicated monitoring mechanisms and scripts or routing protocols.
- The server serves dozens of VLANs with independent routing — manual management through
ip routeor Netplan becomes hard to maintain, and a dedicated router or FRRouting is worth considering.
Verifying the result
- Check the applied routes:
ip route showmust contain the expected entry with the correct gateway and metric. - Check connectivity through a specific route:
ip route get 10.0.0.5must show which interface and gateway the packet will use. - Confirm the settings survive a reboot:
sudo netplan generate && sudo reboot, then runip route showagain. - Check both interfaces in a multihomed setup:
ping -I eth0 8.8.8.8andping -I eth1 10.0.0.1must both succeed independently.
Before configuring routes it helps to understand subnet masks — see the article on CIDR, and the general address translation mechanism is covered in the article on NAT.