Linux › Section I · Your network and its configuration › Topic I.7

Configuring interfaces and network managers

You now know how to read addresses, routes and interfaces; this topic shows how they get set. You can type the settings into the kernel by hand, but on real systems a network manager such as NetworkManager or systemd-networkd applies them at boot and keeps them right as networks come and go.

Four steps to a working interface

Whatever tool does the work, bringing an interface onto a network takes the same steps. 1. The kernel needs a driver for the hardware, so the interface appears in ip link. 2. Any physical-layer setup must happen, such as joining a Wi-Fi network with its password. 3. The interface needs IP addresses and prefix lengths. 4. Routes must be added, above all the default route through the gateway, and DNS servers recorded. On a fixed desktop on one wired LAN this hardly changes, but laptops move between networks all day, and that is why network managers exist.

Configuring by hand with ip

As root, you can give the kernel every setting directly. ip link set dev enp3s0 up enables the interface. ip address add 192.0.2.150/26 dev enp3s0 assigns an address, and the kernel adds the subnet route by itself. ip route add default via 192.0.2.129 dev enp3s0 sets the gateway, which must lie inside an attached subnet. To undo, use ip address del 192.0.2.150/26 dev enp3s0 and ip route del default. A route to one extra network looks like ip route add 198.51.100.0/24 via 192.0.2.140. Changes take effect at once and are lost at the next reboot, or sooner if a network manager rewrites them.

Worked example: a temporary static setup

A technician must put a test machine on 192.0.2.128/26, gateway 192.0.2.129, without touching its saved configuration. 1. ip link set dev enp3s0 up, then ip -br link to confirm UP and LOWER_UP. 2. ip address add 192.0.2.150/26 dev enp3s0. 3. ip route add default via 192.0.2.129. 4. ip route get 203.0.113.9 should now name via 192.0.2.129 dev enp3s0. 5. ping -c 3 192.0.2.129 tests the gateway. Check: the gateway lies in 192.0.2.128/26, whose hosts run from .129 to .190, so step 3 is accepted. Warning: on a remote server, a wrong ip command can cut off your own session, so work from a console where possible.

Why boot scripts were not enough

Traditionally, a boot script ran the same commands from a configuration file. Debian's ifupdown, with ifup, ifdown and /etc/network/interfaces, still does this on some servers and minimal installs, and older Red Hat systems used ifcfg files in /etc/sysconfig/network-scripts, now legacy. Static scripts cope badly with modern machines. A laptop must choose between wired and wireless, scan for and join Wi-Fi networks, ask DHCP for an address, react when a cable is unplugged, and let an ordinary user switch networks without becoming root. Those needs call for a service that runs all the time and watches for events.

NetworkManager

NetworkManager is the daemon used on most desktops and laptops, and by default on Fedora, RHEL and Ubuntu Desktop. It tracks devices, reported by the kernel and udev, and connections: saved profiles holding everything needed to use a network, such as an address method, DNS servers and a Wi-Fi password. On startup it activates a suitable profile, preferring wired links, and it reacts when links come and go. Desktop users meet it as the network icon. Current releases store profiles as keyfiles in /etc/NetworkManager/system-connections; the main settings file is /etc/NetworkManager/NetworkManager.conf. nm-online exits with status 0 once the network is up, which suits scripts.

Driving NetworkManager with nmcli

nmcli is NetworkManager's command-line client, and unique abbreviations such as nmcli con are accepted. nmcli on its own prints a summary of every device. nmcli general status gives the overall state, nmcli device status lists devices and their connections, and nmcli device show enp3s0 prints one device's addresses, gateway and DNS servers. nmcli connection show lists saved profiles, and nmcli connection up NAME or nmcli connection down NAME activates or deactivates one. nmcli connection modify NAME followed by setting.property value pairs edits a profile; the change takes effect the next time the profile is activated, for example with nmcli connection up NAME.

Worked example: a permanent static profile

Make the earlier test settings permanent with NetworkManager. 1. Create a profile: nmcli connection add type ethernet ifname enp3s0 con-name office ipv4.method manual ipv4.addresses 192.0.2.150/26 ipv4.gateway 192.0.2.129 ipv4.dns 192.0.2.53. 2. ipv4.method manual means static addressing; auto would mean DHCP. 3. Activate it: nmcli connection up office. 4. Verify with ip -br address and ip route, which should show the address and default via 192.0.2.129. 5. To switch back to DHCP later: nmcli connection modify office ipv4.method auto ipv4.addresses "" ipv4.gateway "", then nmcli connection up office. Check: the profile survives a reboot because it is saved as a keyfile.

systemd-networkd and networkctl

Servers often use systemd-networkd, a smaller service built into systemd that is well suited to machines whose network rarely changes. It reads .network files from /etc/systemd/network, among other directories. A file such as 10-office.network holds a [Match] section with Name=enp3s0 and a [Network] section with Address=192.0.2.150/26, Gateway=192.0.2.129 and DNS=192.0.2.53; for DHCP, the [Network] section simply says DHCP=yes. networkctl list shows the interfaces it manages, networkctl status enp3s0 their details, and networkctl reload rereads the files. The systemd.network(5) manual page lists every setting. Only one manager should control a given interface.

Netplan

Ubuntu adds Netplan, which is not a manager at all but a translator. You describe the network once in YAML files in /etc/netplan: a network: block with version: 2, a renderer of networkd or NetworkManager, and under ethernets the interface with its addresses list, a routes entry with to: default and via: 192.0.2.129, and nameservers. netplan generate writes the chosen backend's configuration and netplan apply puts it into effect. netplan try applies it and rolls it back unless you confirm within 120 seconds, a safety net for remote work. The older gateway4 key is deprecated in favour of routes.

Common mistakes to avoid

First, expecting ip address add or ip route add to survive a reboot; put lasting settings into the manager's configuration. Second, letting two managers, say NetworkManager and systemd-networkd, control the same interface, so they fight over it. Third, editing a profile with nmcli connection modify and forgetting to reactivate it. Fourth, setting a gateway outside the interface's subnet. Fifth, changing the network of a remote machine without a fallback; use netplan try or console access. Sixth, mixing YAML indentation with tabs in a Netplan file; YAML requires consistent spaces.

Key takeaways

An interface needs a driver, any physical setup, addresses and routes. ip link set, ip address add and del, and ip route add and del change the kernel at once but not permanently. Network managers apply settings at boot and track changes: NetworkManager, driven by nmcli and storing keyfile profiles, dominates desktops and Fedora, RHEL and Ubuntu Desktop; systemd-networkd reads .network files in /etc/systemd/network and is managed with networkctl; ifupdown and ifcfg files are older schemes. Netplan's YAML in /etc/netplan generates configuration for either backend, and netplan try protects remote changes.

Flashcards

Flashcard

ip link set dev NAME up

Enable an interface (as root)

down disables it.

Test your understanding

Question 1 of 5

An administrator runs ip address add 192.0.2.150/26 dev enp3s0 on a server. What happens after the next reboot?

Question 2 of 5

Which nmcli setting makes a NetworkManager profile use a static address rather than DHCP?

Question 3 of 5

In a systemd-networkd .network file, which line requests an address by DHCP?

Question 4 of 5

You must change the Netplan configuration of a remote server you reach only over SSH. Which command is safest?

Question 5 of 5

After nmcli connection modify office ipv4.dns 192.0.2.53, the old DNS server is still in use. What is the most likely reason?

← Previous topicNext topic →