Menu
Persistent network configuration
“Give a Linux host a name, an address, a gateway and DNS that survive a reboot, with NetworkManager, ifupdown and systemd-networkd.”
Typing ip addr add gets a machine online, but only until the next reboot. A server needs its address, gateway, DNS servers and name written down somewhere so they come back every time it boots. That is persistent configuration, and every distribution does it a little differently.
The good news: underneath, every method sets the same four things. Once you know those, the tools are just different ways of saying it. This byte covers the files and tools the exam lists, with NetworkManager as the main tool. Changing settings on the fly, without saving them, is the job of basic network troubleshooting.
What a host needs#
To talk to the world, a Linux host needs:
| Setting | Example | Where it ends up |
|---|---|---|
| IP address and prefix | 192.168.1.50/24 | on the interface |
| Default gateway | 192.168.1.1 | in the routing table |
| DNS servers | 1.1.1.1, 9.9.9.9 | /etc/resolv.conf |
| Hostname | web01 | /etc/hostname |
You can set these statically (typed by you) or get them from a DHCP server, which hands out address, gateway and DNS automatically. Laptops and desktops use DHCP. Servers usually get a static address so others can find them.
First, find your interfaces. Modern Linux gives them predictable names based on where the hardware sits: enp0s3 (Ethernet, PCI bus 0, slot 3), ens33, wlp2s0 (Wi-Fi). Older systems and many containers use eth0 and wlan0.
ip -br linklo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
enp0s3 UP 08:00:27:4e:66:a1 <BROADCAST,MULTICAST,UP,LOWER_UP>
wlp2s0 DOWN 3c:a9:f4:12:7b:90 <NO-CARRIER,BROADCAST,MULTICAST,UP>The hostname#
The machine’s name lives in /etc/hostname: one line, nothing else.
cat /etc/hostnameweb01On any systemd-based distribution, use hostnamectl to view and change it. It updates the running system and the file in one go:
hostnamectl Static hostname: web01
Icon name: computer-vm
Chassis: vm
Machine ID: 4f6c2b8e0d1a4c7e9b3f5a2d8c6e1b07
Boot ID: a91d3e5c7b2f4a6e8d0c1b3a5f7e9d2c
Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
Kernel: Linux 6.1.0-28-amd64
Architecture: x86-64sudo hostnamectl set-hostname web02systemd knows three kinds of hostname:
- static: the one in
/etc/hostname, used at boot. - transient: the name the kernel has right now, which DHCP may change.
- pretty: a free-form name for humans, like
Amin's laptop, set withhostnamectl set-hostname --pretty "Amin's laptop".
The old hostname command (more on it in the troubleshooting byte) also changes the name, but only until reboot, because it doesn’t touch /etc/hostname.
Tip
After renaming, update /etc/hosts too. Debian and Ubuntu map the hostname to 127.0.1.1 there, and sudo complains “unable to resolve host” when the two don’t match.
/etc/hosts, /etc/resolv.conf and /etc/nsswitch.conf#
Three small files decide how names become addresses. They get a full byte of their own in client side DNS, so here is just what you need for host setup.
/etc/hosts is a static list of names. Each line is an address, the official name, then optional aliases:
127.0.0.1 localhost
127.0.1.1 web02.example.lan web02
192.168.1.10 db01.example.lan db01
::1 localhost ip6-localhost ip6-loopback/etc/resolv.conf says which DNS servers to ask:
# Generated by NetworkManager
search example.lan
nameserver 1.1.1.1
nameserver 9.9.9.9/etc/nsswitch.conf sets the order: check the hosts file first, then DNS.
hosts: files dnsImportant
On most modern systems /etc/resolv.conf is written by a program (NetworkManager, systemd-resolved, netplan or resolvconf). Edit it by hand and your change disappears at the next reconnect. Set DNS servers in that program’s configuration instead. The first line of the file usually tells you who owns it.
NetworkManager and nmcli#
NetworkManager is the standard network service on RHEL, Fedora, Ubuntu Desktop and most desktop distributions. Debian uses it when you install a desktop. It handles Ethernet, Wi-Fi, VPNs, bridges and more, and it reconnects on its own when a cable or access point comes back.
Two ideas are key:
- A device is a network interface:
enp0s3,wlp2s0. - A connection (or profile) is a saved set of settings: address, DNS, Wi-Fi password. A device uses one active connection at a time. You can keep several, like “office” and “home”, for the same device.
You control it with nmcli on the command line, nmtui (a text menu in the terminal) or the settings panel of your desktop. nmcli accepts abbreviations: nmcli c is nmcli connection, nmcli d is nmcli device.
Look around#
nmcli general status
nmcli device statusSTATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled missing enabled
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected Wired connection 1
wlp2s0 wifi disconnected --
lo loopback connected (externally) lonmcli connection showNAME UUID TYPE DEVICE
Wired connection 1 9b5e3c1a-7f2d-4e8b-a6c0-1d4f2e8b7a93 ethernet enp0s3
lo 2c7e1f4a-8d3b-4a9e-b5c6-0e2f7d1a4b85 loopback lonmcli connection show "Wired connection 1" prints every setting of one profile, and nmcli device show enp0s3 shows what the device has right now (addresses, gateway, DNS).
A static Ethernet connection#
Create a new profile called office with a fixed address:
sudo nmcli connection add type ethernet con-name office ifname enp0s3 \
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" \
ipv4.dns-search example.lan
sudo nmcli connection up officeConnection 'office' (5e1d7c3b-2a4f-4b9e-8c6d-3f0a1e7b9d24) successfully added.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)To change an existing profile, use modify, then bring it up again so the change takes effect:
sudo nmcli connection modify office ipv4.addresses 192.168.1.60/24
sudo nmcli connection modify office +ipv4.dns 8.8.8.8
sudo nmcli connection up officeA + adds a value to a list setting, a - removes one. To switch the profile back to DHCP:
sudo nmcli connection modify office ipv4.method auto ipv4.addresses "" ipv4.gateway ""
sudo nmcli connection up officeOther everyday commands:
| Command | What it does |
|---|---|
nmcli connection down office | deactivate a profile |
nmcli connection delete office | remove a profile |
nmcli connection reload | re-read profile files you edited by hand |
nmcli device disconnect enp0s3 | take a device offline (and stop auto-connect) |
nmcli device reapply enp0s3 | apply changed settings without a full down and up |
nmcli networking off / on | turn all networking off and on |
Wi-Fi#
nmcli radio wifi on
nmcli device wifi listIN-USE BSSID SSID MODE CHAN RATE SIGNAL BARS SECURITY
A4:2B:B0:11:22:33 HomeNet Infra 36 540 Mbit/s 82 ▂▄▆█ WPA2 WPA3
C8:3A:35:44:55:66 Neighbor Infra 6 130 Mbit/s 41 ▂▄__ WPA2Connect, and NetworkManager creates and saves a profile named after the network:
sudo nmcli device wifi connect HomeNet password 'correct horse battery'Device 'wlp2s0' successfully activated with '0d3a7e2c-5b1f-4c8d-9e6a-2f4b7c1d8e30'.Add hidden yes for a network that doesn’t broadcast its name. nmcli device wifi rescan refreshes the list, and nmcli radio wifi off turns the radio off (handy on a plane).
Where the profiles live#
| Distribution | Profile storage |
|---|---|
| RHEL 9, Fedora, Debian | keyfiles in /etc/NetworkManager/system-connections/*.nmconnection (readable by root only, they can hold passwords) |
| RHEL 8 and older | ifcfg-* files in /etc/sysconfig/network-scripts/ (still read by RHEL 9, but deprecated) |
| Ubuntu 23.10 and newer | netplan YAML in /etc/netplan/, which NetworkManager writes for you |
You can edit a keyfile by hand, then run nmcli connection reload and bring it up again.
nmcli also has a handy terse mode for scripts: nmcli -t -f DEVICE,STATE device prints enp0s3:connected, one device per line, with no table padding.ifupdown: /etc/network/interfaces, ifup and ifdown#
Debian servers installed without a desktop use the classic ifupdown system. Its configuration is /etc/network/interfaces:
auto lo
iface lo inet loopback
# Static address, brought up at boot
auto enp0s3
iface enp0s3 inet static
address 192.168.1.50/24
gateway 192.168.1.1
# DHCP, brought up when the device appears
allow-hotplug enp0s8
iface enp0s8 inet dhcpautobrings the interface up at boot (and withifup -a).allow-hotplugbrings it up when the kernel detects the device.inetis IPv4,inet6is IPv6. The method isstatic,dhcp,loopbackormanual.- DNS servers are set in
/etc/resolv.conf, or with adns-nameserversline if theresolvconfpackage is installed.
After editing, apply the change by taking the interface down and up:
sudo ifdown enp0s3 && sudo ifup enp0s3ifup and ifdown read only this file. They don’t know about NetworkManager profiles, and NetworkManager leaves interfaces listed here alone by default.
On the Red Hat family, ifup and ifdown were scripts from the old network-scripts package, which reads the ifcfg-* files. That package is gone in RHEL 9. Where the commands still exist there, they hand the work to NetworkManager, so prefer nmcli connection up and down.
Caution
Running ifdown on the interface your SSH session uses can cut you off. If your shell dies before ifup runs, the server stays offline. Use a console, or run the pair inside tmux or screen so it finishes even if you drop.
systemd-networkd#
systemd-networkd is systemd’s own lightweight network service. You find it on servers, in containers and cloud images, and behind netplan on Ubuntu Server. It has no GUI and no Wi-Fi password manager. It reads plain .network files:
1[Match]
2Name=enp0s3
3
4[Network]
5Address=192.168.1.50/24
6Gateway=192.168.1.1
7DNS=1.1.1.1For DHCP, replace the address lines with DHCP=yes (or ipv4, ipv6). Files are read from /usr/lib/systemd/network/, /run/systemd/network/ and /etc/systemd/network/, and a file in /etc overrides one with the same name in the other two.
sudo systemctl enable --now systemd-networkd
networkctl listIDX LINK TYPE OPERATIONAL SETUP
1 lo loopback carrier unmanaged
2 enp0s3 ether routable configured
2 links listed.Ubuntu Server usually writes these files for you from netplan. You edit a YAML file in /etc/netplan/, then run sudo netplan apply (or sudo netplan try, which rolls back if you lose the connection):
1network:
2 version: 2
3 renderer: networkd
4 ethernets:
5 enp0s3:
6 addresses: [192.168.1.50/24]
7 routes:
8 - to: default
9 via: 192.168.1.1
10 nameservers:
11 addresses: [1.1.1.1, 9.9.9.9]Run only one network manager per interface. NetworkManager, ifupdown and systemd-networkd fighting over the same device is a classic cause of “my settings keep changing”.
Which tool where#
| System | Usual tool | Persistent config |
|---|---|---|
| RHEL 9, Fedora | NetworkManager | keyfiles, nmcli |
| Ubuntu Desktop | NetworkManager via netplan | nmcli, /etc/netplan/ |
| Ubuntu Server | systemd-networkd via netplan | /etc/netplan/*.yaml |
| Debian server (no desktop) | ifupdown | /etc/network/interfaces |
| Debian desktop | NetworkManager | nmcli |
| Containers, minimal images | systemd-networkd or none | /etc/systemd/network/*.network |
dhclient from ISC is no longer maintained. NetworkManager and systemd-networkd have built-in DHCP clients, so on most current systems you never run a separate one.Exam checklist#
- A host needs an address with prefix, a default gateway, DNS servers and a hostname. DHCP can provide the first three.
/etc/hostnameholds the static name.hostnamectl set-hostnamechanges it permanently,hostname newnameonly until reboot./etc/hosts= static name to address list,/etc/resolv.conf=nameserverandsearchlines,/etc/nsswitch.confhosts:line = lookup order.- NetworkManager: devices vs connections (profiles).
nmcli device status,nmcli connection show,add,modify,up,down,delete,reload. - Static IPv4 with nmcli:
ipv4.method manual,ipv4.addresses,ipv4.gateway,ipv4.dns. Back to DHCP:ipv4.method auto. Amodifyneedsconnection upto take effect. - Wi-Fi:
nmcli radio wifi on,nmcli device wifi list,nmcli device wifi connect SSID password PASS. - Debian ifupdown:
/etc/network/interfaceswithauto,iface ... inet static|dhcp; apply withifdownthenifup(ifup -afor allautoones). - systemd-networkd:
.networkfiles with[Match]and[Network]in/etc/systemd/network/, status withnetworkctl. Ubuntu Server drives it through netplan. - Don’t hand-edit a managed
/etc/resolv.conf; set DNS in NetworkManager, netplan or systemd-resolved.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-109): Persistent network configuration"
[main 0b9b382] learn(lpic-109): Persistent network configuration
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: