Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Networking and Firewalls

Network Interfaces, Addresses and Routes

Inspect and configure Linux networking with the ip command: interfaces, IPv4 and IPv6 addresses, CIDR, the routing table, and persistent setups with NetworkManager or netplan.

Every server you manage lives on a network, and most outages that are not disk or memory problems turn out to be network problems: a wrong address, a missing route, a link that is down. This lesson gives you the vocabulary and the commands to answer three questions quickly: which interfaces exist, which addresses do they have, and where does a packet go next?

Like the rest of this track, this chapter describes a real Linux server that the site cannot run for you. The commands are shown as read-only code blocks, and the runnable examples are small Python stand-ins that implement the same arithmetic (CIDR math and route lookup) so you can experiment safely. Output shown in this lesson is illustrative; your machine will print its own values.

Interfaces and the ip command

The ip command from the iproute2 package replaced the old ifconfig and route tools. Its structure is ip [options] OBJECT COMMAND.

Terminal
ip link                 # layer 2: interfaces, state, MAC addresses
ip -br link             # one compact line per interface
ip addr                 # layer 3: addresses on each interface
ip -br addr             # compact version
ip route                # the routing table
ip neigh                # ARP / neighbour cache (IP to MAC)

A typical ip -br addr listing has one line per interface: the name (lo, eth0, ens3, enp0s31f6, wlan0), its state (UP, DOWN, UNKNOWN) and the addresses with their prefix length. Names matter: modern systemd-based distributions use "predictable" names such as ens3 (PCI slot) or enp3s0 (bus/slot) so a name stays stable across reboots, instead of eth0/eth1 depending on driver load order.

Useful states to recognise:

  • LOWER_UP in ip link: a cable or virtual link is present.
  • NO-CARRIER: the interface is enabled but nothing is on the other end.
  • state DOWN: the interface is administratively disabled.
  • lo: the loopback interface, 127.0.0.1/8 and ::1/128. It never leaves the machine.

Addresses, prefixes and CIDR

An IPv4 address is 32 bits, written as four decimal bytes. The prefix length after the slash tells how many leading bits identify the network. 192.168.1.25/24 means the first 24 bits (192.168.1) are the network and the last 8 identify the host, so the network is 192.168.1.0/24, the broadcast address is 192.168.1.255, and there are 254 usable host addresses.

Ranges you will see constantly (RFC 1918 private space, not routed on the internet):

Range Size Typical use
10.0.0.0/8 16.7 million large internal networks, cloud VPCs
172.16.0.0/12 1 million Docker default bridge uses 172.17.0.0/16
192.168.0.0/16 65,536 home and small office
169.254.0.0/16 link-local self-assigned when DHCP fails

IPv6 addresses are 128 bits written in hex groups (2001:db8::1/64). fe80::/10 is link-local and every IPv6 interface has one; 2001:db8::/32 is reserved for documentation, which is why examples use it.

Python CIDR calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Adding an address temporarily

The ip command changes the running kernel state. Nothing it does survives a reboot, which is perfect for experiments and emergency repairs, and a trap if you forget to make the change permanent.

Terminal
sudo ip addr add 192.168.1.50/24 dev ens3
sudo ip addr del 192.168.1.50/24 dev ens3
sudo ip link set ens3 up
sudo ip link set ens3 down
sudo ip link set ens3 mtu 9000

Never run ip link set ... down on the interface you are connected through over SSH. You cut your own session. If you must change a remote interface, schedule a rollback first, for example sudo sh -c 'sleep 120 && ip link set ens3 up' &, or use the provider's console.

The routing table

The routing table answers "for this destination, which next hop and interface?" Each entry is a prefix. When several match, the longest prefix wins; 0.0.0.0/0 (written default) matches everything and has the shortest prefix.

Terminal
ip route
# default via 192.168.1.1 dev ens3 proto dhcp metric 100
# 192.168.1.0/24 dev ens3 proto kernel scope link src 192.168.1.25

ip route get 8.8.8.8        # which route would be used, and which source address
sudo ip route add 10.50.0.0/16 via 192.168.1.254 dev ens3
sudo ip route del 10.50.0.0/16
sudo ip route replace default via 192.168.1.1

The first entry is the default gateway: anything not on the local network goes to 192.168.1.1. The second is created by the kernel automatically when an address is added, and it means "this /24 is directly reachable on ens3". When two routes have the same prefix, the lower metric wins, which is how a server prefers a wired link over Wi-Fi.

ip route get is the single most useful troubleshooting call here, because it shows the decision the kernel would actually make rather than what you think the table says.

Python Route lookup: longest prefix wins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Making configuration permanent

Which tool stores the permanent configuration depends on the distribution.

Tool Typical on Command / file
NetworkManager Fedora, RHEL, Rocky, Alma, Ubuntu Desktop nmcli, /etc/NetworkManager/system-connections/
netplan Ubuntu Server /etc/netplan/*.yaml, sudo netplan apply
systemd-networkd minimal servers, containers /etc/systemd/network/*.network
ifupdown Debian (classic) /etc/network/interfaces

A static address with NetworkManager:

Terminal
nmcli connection show
sudo nmcli connection modify "Wired connection 1" \
  ipv4.method manual \
  ipv4.addresses 192.168.1.50/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "1.1.1.1 9.9.9.9"
sudo nmcli connection up "Wired connection 1"

The same with netplan on Ubuntu Server:

YAML
# /etc/netplan/01-static.yaml  (file must be mode 600)
network:
  version: 2
  ethernets:
    ens3:
      dhcp4: false
      addresses: [192.168.1.50/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [1.1.1.1, 9.9.9.9]

Apply with sudo netplan try, which waits for you to confirm and rolls back automatically if you lose connectivity. That safety net is why you should prefer it over netplan apply on remote machines.

Reading the network in 4 commands

ip -br link Is the link up?
ip -br addr Which addresses?
ip route Where do packets go?
ip route get What will happen for this IP?
1 / 2

Common mistakes

  • Changing an address with ip and forgetting to persist it, then losing it at the next reboot.
  • Two interfaces on the same subnet, which causes confusing asymmetric routing.
  • A wrong prefix length (/16 instead of /24) that makes the host believe distant machines are local, so it ARPs for them instead of using the gateway.
  • Missing default route: local pings work, the internet does not. Check ip route first.

Next you will see how names turn into these addresses: DNS and Name Resolution.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A host has the address 10.8.4.77/20. Which statement is true?

    /20 means 20 network bits (netmask 255.255.240.0), so the local network is 10.8.0.0/20, and 10.0.0.0/8 is private space.
  2. 2. Two routes match a destination: default via the gateway and 10.50.7.0/24 via a router. Which is used?

    The kernel prefers the most specific matching prefix; metrics only break ties between equal prefixes.
  3. 3. Which command shows the route the kernel would pick for 8.8.8.8?

    ip route get performs a lookup and prints the chosen gateway, interface and source address.
  4. 4. Why prefer netplan try over netplan apply on a remote server?

    If the new config cuts your connection, the timeout expires and the old configuration returns.

Summary

  • ip link, ip addr, ip route and ip route get are the core inspection tools; ip changes are temporary.
  • A CIDR prefix splits an address into network and host bits; private ranges are 10/8, 172.16/12 and 192.168/16.
  • The routing table uses the longest matching prefix; default is 0.0.0.0/0, and the metric breaks ties.
  • Persist configuration in NetworkManager, netplan, systemd-networkd or ifupdown, whichever your distribution uses.
  • Never take down the interface you are logged in through; use netplan try or a scheduled rollback.

Next: DNS and Name Resolution