Persistent network configuration

Configuring an interface so it survives a reboot: the modern ip commands, the files each distribution family uses, and the name resolution chain from /etc/hosts to DNS.

Lesson 2 of 4 in objective 109. Networking fundamentals, part of LPIC-1 Exam 102-500.

One takes effect now and forgets; the other is remembered. ip addr add — Takes effect: Immediately; After a reboot: Gone; Lives in: Nowhere. It is runtime state. The config file — Takes effect: Next boot, or on ifup; After a reboot: Kept; Lives in: /etc/network/interfaces, ifcfg files, or NetworkManager ip addr add The config file Takes effect Immediately Next boot, or on ifup After a reboot Gone Kept Lives in Nowhere. It is runtime state /etc/network/ interfaces, ifcfg files, or NetworkManager
One takes effect now and forgets; the other is remembered.

ip, and the commands it replaced

The iproute2 tools are the current ones. ip addr show lists addresses, ip addr add 192.168.1.10/24 dev eth0 assigns one, ip link set eth0 up brings an interface up, and ip route shows or sets routing — ip route add default via 192.168.1.1 sets a default gateway. Their predecessors, ifconfig, route and netstat, are deprecated but still appear in questions and still installed on plenty of machines; the equivalences are worth knowing in both directions. ss has replaced netstat for listing sockets: ss -tulpn shows listening TCP and UDP sockets with the process behind each.

Everything set with ip is lost at reboot. Persisting it depends on the family: Debian traditionally uses /etc/network/interfaces, Red Hat uses ifcfg files under /etc/sysconfig/network-scripts/, and both are increasingly replaced by NetworkManager (nmcli on the command line) or systemd-networkd. DHCP is the alternative to configuring anything, with a client requesting an address, mask, gateway and DNS servers from a server on the network.

The Debian file is worth being able to read and write, because it is short enough to be quoted whole in a question. It is a series of two-line stanzas: auto eth0 says bring this interface up at boot, and iface eth0 inet dhcp says how — inet for IPv4, then dhcp or static. A static stanza indents address, netmask and gateway lines underneath it. allow-hotplug is the alternative to auto and means "when the kernel says this interface appeared" rather than "at boot", which is what you want for a cable that may not be plugged in. Miss the auto line and the stanza is correct and never applied — ifup eth0 by hand would still work, and the machine would come up with no address.

Editing that file changes nothing on its own, and the way to apply it is the part candidates get wrong. ifup and ifdown are wrappers that read the stanzas and record which interfaces they have configured, historically under /run/network, and ifup refuses to touch an interface it believes is already up. So a changed address is applied by cycling the interface through the wrappers — ifdown eth0 && ifup eth0 — and NOT by ip link set eth0 down and up again, which toggles the link without ever re-reading the file. ifquery eth0 prints what ifup would apply, which is the safe way to check a stanza before committing to it.

The hostname is separate from any of this. hostname prints the running one and setting it that way lasts until reboot; the persistent name is in /etc/hostname. On a systemd machine hostnamectl set-hostname myserver does both at once — it writes the file and changes the running system — which is why it is the answer to a question that asks for a change that survives a reboot without saying which file.

Two hostname commands: one lasts until reboot, one writes the file as well. hostname myserver — The running name: Changed immediately; /etc/hostname: Untouched; Survives a reboot: No — the old name returns. hostnamectl set-hostname — The running name: Changed immediately; /etc/hostname: Written as well; Survives a reboot: Yes — file and runtime agree hostname myserver hostnamectl set-hostname The running name Changed immediately Changed immediately /etc/hostname Untouched Written as well Survives a reboot No — the old name returns Yes — file and runtime agree
Two hostname commands: one lasts until reboot, one writes the file as well.
in /etc/network/interfacesauto eth0iface eth0 inet dhcpor, for a fixed addressauto eth0iface eth0 inet static    address 192.168.1.10/24    gateway 192.168.1.1hostnamectl set-hostname myservercat /etc/hostnamemyserver
A Debian stanza for DHCP, then one for a static address.

systemd-networkd, the third place persistence can live

On a minimal server with neither ifupdown nor NetworkManager, the configuration is systemd-networkd's, and it reads three KINDS of file from /etc/systemd/network/, told apart by their extension: .link for udev-level device properties such as the name or MAC address, .netdev to CREATE a virtual device such as a bridge or a VLAN, and .network to configure addressing on an interface that already exists. Files are processed in lexicographic order of filename, which is why a numeric prefix is conventional, and the first .network file whose [Match] section matches an interface is the one applied.

So a static address is /etc/systemd/network/10-wired.network holding a [Match] section with Name=enp1s0 and a [Network] section with Address=10.0.0.10/24 and Gateway=10.0.0.1. For DHCP the [Network] section takes DHCP=yes in place of those two lines. Getting the extension wrong is the trap the distractors are built on: the same content in a .netdev or a .link file configures nothing, because neither of those is the file that assigns addresses.

networkctl is the client for the daemon, in the way nmcli is the client for NetworkManager. networkctl list gives the per-link summary and, crucially, says whether networkd has CONFIGURED each link or is leaving it unmanaged — a link showing unmanaged has no matching .network file. ip link show cannot answer that, because it reports the kernel's view of the interface and not who is looking after it. networkctl status enp1s0 expands one link with its addresses, routes and the .network file that matched, and networkctl reload picks up edited files.

Turning a name into an address

/etc/hosts is a static table of addresses and names, and it is consulted before DNS on a normally configured system — which makes it both the quick fix for a machine with no DNS entry and the reason a stale line there can override a correct DNS record. /etc/resolv.conf names the DNS servers to ask, with nameserver lines and a search list for unqualified names; on modern systems it is often generated by NetworkManager or systemd-resolved rather than edited.

/etc/nsswitch.conf is what decides the ORDER — the hosts line typically reads files dns, meaning /etc/hosts first and DNS second, and it is the file to point at when a question asks how the resolution order is configured. host and dig query DNS directly and therefore ignore /etc/hosts entirely, while getent hosts goes through the full name service and does not. That difference is the fastest way to prove which layer is answering.

The hosts line in nsswitch.conf sets this order, and the first hit ends the search. A search over 2 places, tried in this order: the first hit ends it, and everything under the one that hits is never looked at. Going down a step means the name is not in there. First, files — /etc/hosts (a static table of addresses and names, and the quick fix for a machine with no DNS entry); a hit there means DNS is never asked, so a stale line here beats a correct DNS record. Last, dns — the servers in /etc/resolv.conf (nameserver lines and a search list. dig and host ask these directly and skip /etc/hosts; getent does not); a hit there means that address is used and the search stops. A miss at every one of them: No address for the name. files — /etc/hosts a static table of addresses and names, and the quick fix for a machine with no DNS entry DNS is never asked, so a stale line here beats a correct DNS record the name is not in there dns — the servers in /etc/resolv.conf nameserver lines and a search list. dig and host ask these directly and skip /etc/hosts; getent does not that address is used and the search stops No address for the name
The hosts line in nsswitch.conf sets this order, and the first hit ends the search.

Worth carrying in

ip addr add … dev eth0
Assign an address. ip addr show lists them.
ip link set eth0 up
Bring an interface up or down.
ip route add default via …
Set the default gateway.
ss -tulpn
Listening TCP and UDP sockets with the owning process. Replaces netstat.
/etc/network/interfaces
Debian persistent configuration: auto eth0 then iface eth0 inet dhcp. Red Hat uses /etc/sysconfig/network-scripts/.
ifdown eth0 && ifup eth0
Apply an edited /etc/network/interfaces stanza. Toggling the link with ip does not re-read it.
/etc/systemd/network/10-wired.network
[Match] names the interface, [Network] the address. .netdev creates devices, .link sets properties.
networkctl list
Per-link state under systemd-networkd, including which links it is leaving unmanaged.
nmcli
NetworkManager on the command line.
hostnamectl set-hostname
Change the running hostname AND /etc/hostname in one command.
/etc/hosts
Static name-to-address table, consulted before DNS.
/etc/resolv.conf
nameserver and search lines. Often generated, not hand-edited.
/etc/nsswitch.conf
Decides the lookup ORDER — files then dns, typically.

What the exam does with this

Objective
109. Networking fundamentals
Share of the exam
23.33% (the whole objective)
Questions in this lesson
20
Signed for by a person
0

Partly checked. None of the 20 questions here has been read against the cited source by a person. 20 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Persistent network configuration

Questions in this lesson

Practise Persistent network configuration

The rest of objective 109