Skip to content
Tutorials Center
My learning

Topics

Lesson 81 of 95 ยท System Administration

Network Configuration: NetworkManager, netplan, DNS

Configure Linux networking that survives a reboot: static IPs with nmcli and netplan, systemd-networkd, resolvers, /etc/hosts, search domains and hostnames.

A freshly installed server usually gets its network settings from DHCP, a service on the network that hands out an address, gateway and DNS servers automatically. That is perfect for laptops and for many cloud machines. But servers that other machines must find reliably need a fixed address, and sometimes you need to change DNS servers, set a hostname or add a second network. This lesson covers what to configure and, because there are several competing tools, which tool owns the configuration on your system.

In Network Troubleshooting we diagnosed problems. Here we make settings permanent. You cannot change the network in our browser sandbox, so the tool commands are read-only; what you can run are models in Bash: a subnet calculator, a validator for a static configuration, a DNS resolver that follows /etc/hosts, search domains and ndots, and a hostname checker.

Temporary versus persistent

The ip command changes the running kernel immediately, but the change disappears at reboot or when the network manager rewrites the interface:

CONSOLE
$ sudo ip addr add 192.168.1.50/24 dev eth0      # temporary
$ sudo ip route add default via 192.168.1.1      # temporary
$ sudo ip link set eth0 up

Use this for experiments and emergencies. For anything that must last, change the configuration of the program that manages networking at boot. Several exist, and distributions pick different ones:

System Configuration lives in Command-line tool
Ubuntu Server (18.04+) /etc/netplan/*.yaml (generates config for a backend) netplan
Ubuntu Desktop, Fedora, RHEL, most desktops NetworkManager connection profiles nmcli, nmtui
Debian (classic servers) /etc/network/interfaces ifup, ifdown
Minimal servers, containers systemd-networkd files in /etc/systemd/network/ networkctl
Older RHEL/CentOS /etc/sysconfig/network-scripts/ifcfg-* ifup, nmcli
flowchart TD
    N["netplan YAML<br/>/etc/netplan/*.yaml"] -->|renderer: networkd| D["systemd-networkd"]
    N -->|renderer: NetworkManager| M["NetworkManager"]
    D --> K["Kernel: addresses,<br/>routes, links"]
    M --> K
    M -.->|writes DNS to| R["systemd-resolved /<br/>/etc/resolv.conf"]
    D -.-> R

On Ubuntu, netplan is only a translator: you write one YAML file and it configures either systemd-networkd or NetworkManager.

Find out what is in charge before editing anything: systemctl is-active NetworkManager systemd-networkd, or look for /etc/netplan. Two tools fighting over the same interface is a frequent source of confusion.

The numbers you must get right

A static configuration needs four items: an address with a prefix length (192.168.1.50/24), a default gateway (a router on the same subnet), DNS servers, and optionally a search domain. The classic mistake is a gateway outside the interface's own subnet. The kernel then has no way to reach the gateway directly, and the route is refused or useless.

Let us make a calculator that turns a CIDR notation into the values you see in older tools (netmask, network, broadcast) and checks that a gateway fits:

Bash Subnet calculator and gateway check

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 read or cat), it gets the next line from this box.

Notice how a /26 leaves only 62 usable hosts: the all-zero host address is the network itself and the all-ones address is the broadcast. A /24 is the common home and office size with 254 usable hosts.

NetworkManager: nmcli

On desktops and many servers NetworkManager keeps connection profiles (a saved set of settings that can be applied to a device). nmcli is its command-line client; nmtui is a text menu version. All read-only here:

CONSOLE
$ nmcli device status                     # devices and their state
$ nmcli connection show                   # saved profiles
$ nmcli device show eth0                  # current IP, gateway, DNS of a device

$ 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" \
      ipv4.dns-search example.com
$ sudo nmcli connection up "Wired connection 1"   # apply

$ sudo nmcli connection modify "Wired connection 1" ipv4.method auto   # back to DHCP
$ sudo nmcli connection modify "Wired connection 1" ipv4.addresses "" ipv4.gateway ""
$ nmcli device wifi list
$ sudo nmcli device wifi connect "CafeWifi" password "secret"

Remember that modify only edits the profile; connection up (or reload plus re-activating) applies it. NetworkManager stores profiles under /etc/NetworkManager/system-connections/, and these files hold secrets such as Wi-Fi passwords, so they are readable by root only.

netplan: YAML for Ubuntu

Ubuntu servers read YAML files from /etc/netplan/. They are applied in alphabetical order, so a later file overrides an earlier one. Indentation uses spaces (never tabs), and the files should be mode 600 because newer versions warn about world-readable ones.

YAML
# /etc/netplan/01-static.yaml   (read-only example)
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      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]
        search: [example.com]
CONSOLE
$ sudo netplan generate           # check syntax and write backend config
$ sudo netplan try                # apply, then roll back after 120 s unless you confirm
$ sudo netplan apply              # apply permanently
$ sudo netplan get                # show the merged configuration

netplan try is the safe way to change a remote server: if the new settings cut your SSH session you cannot press Enter to confirm, and after the timeout the old settings come back. The same idea as the at rollback in Firewalls. Older guides use gateway4:, which is deprecated in favour of routes:.

A short script can check the facts a YAML file declares. This one reads a netplan-style file, extracts the address and gateway and applies the subnet test from before:

Bash Validating a static configuration

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 read or cat), it gets the next line from this box.

systemd-networkd

On minimal servers and in many container hosts, systemd-networkd is configured with small INI-style files. Files in /etc/systemd/network/ with the suffix .network match interfaces and set their properties:

INI
# /etc/systemd/network/10-eth0.network   (read-only example)
[Match]
Name=eth0

[Network]
Address=192.168.1.50/24
Gateway=192.168.1.1
DNS=1.1.1.1
DNS=9.9.9.9
Domains=example.com
CONSOLE
$ sudo systemctl enable --now systemd-networkd
$ sudo networkctl reload && sudo networkctl reconfigure eth0
$ networkctl status eth0

Name resolution: three layers

When a program asks for db.example.com, the C library decides how to find the address. Understanding the layers explains most "works here, not there" mysteries:

  1. /etc/nsswitch.conf has a line such as hosts: files dns. It sets the order: the local files (/etc/hosts) first, then DNS.
  2. /etc/hosts maps names to addresses on this machine only. It beats DNS, so a forgotten entry here overrides the real DNS record.
  3. DNS is asked through the resolver configured in /etc/resolv.conf.
TEXT
# /etc/hosts
127.0.0.1   localhost
127.0.1.1   web01.example.com web01
10.0.0.5    db.internal

# /etc/resolv.conf
nameserver 10.0.0.2
nameserver 1.1.1.1
search corp.example.com example.com
options ndots:1 timeout:2 attempts:2

nameserver lines are tried in order (up to three). search is a list of domains that are appended to short names, so ping db can find db.corp.example.com. options ndots:N sets how many dots a name must contain before the resolver tries it as an absolute name first. With ndots:1, db (no dots) is tried with the search domains first, while www.example.com (two dots) is tried as it is first. A trailing dot (www.example.com.) always means "absolute, do not append anything".

The next script follows those rules exactly with a mock DNS database, so you can see every attempt, including the wasted ones:

Bash Resolver order: hosts file, search domains and ndots

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 read or cat), it gets the next line from this box.

The ndots:5 run shows why some container platforms set a high ndots: every external name first wastes several queries on search domains before the real lookup. Fixing it (a lower ndots or a trailing dot) can noticeably speed up applications. See also Environment Variables for another setting that quietly changes behaviour.

systemd-resolved

On many modern systems /etc/resolv.conf is not a normal file. It is a symlink to a file written by systemd-resolved, whose local stub listens on 127.0.0.53, caches answers, and forwards to the real servers learned from DHCP or from your network profile. So nameserver 127.0.0.53 in resolv.conf is normal there. Never edit the generated file; change the settings in the network tool (nmcli, netplan, networkd) or in /etc/systemd/resolved.conf.

CONSOLE
$ ls -l /etc/resolv.conf              # a symlink? where to?
$ resolvectl status                   # DNS servers per interface
$ resolvectl query example.com        # resolve using the same path as programs
$ resolvectl flush-caches
$ resolvectl dns eth0 1.1.1.1         # temporary change for one interface

If you manage resolv.conf by hand on a server without these services, a plain file with a few nameserver lines works, but some DHCP clients and tools overwrite it. Make sure only one thing writes it. Troubleshooting with dig versus getent hosts is covered in Network Troubleshooting.

Hostnames

Every machine has a name. The static hostname is stored in /etc/hostname, and the tool to change it is hostnamectl:

CONSOLE
$ hostnamectl                             # show names and OS info
$ sudo hostnamectl set-hostname web01     # persistent, takes effect at once
$ hostname -f                             # the fully qualified name

Also fix /etc/hosts, because some software looks up its own name and stalls when it cannot resolve it. Debian-based systems conventionally map the machine's own name to 127.0.1.1, as in the example above. A hostname follows strict rules: labels separated by dots, each label 1 to 63 letters, digits or hyphens that does not start or end with a hyphen, and at most 253 characters overall. Underscores and spaces are not allowed. A checker makes the rules concrete:

Bash Is this a valid hostname?

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 read or cat), it gets the next line from this box.

The hostname does not need to match a DNS record, but for servers it should: the name on the machine, the DNS entry and the reverse (PTR) record should agree, which helps mail servers and logs. Short names are fine on a laptop; use a fully qualified name on servers.

A safe procedure for changing remote network settings

  1. Identify the network tool in charge (netplan, nmcli, networkctl) and the interface name (ip -br link).
  2. Back up the current file (cp file file.bak) and write down the present address, route and DNS (ip -br addr; ip route; resolvectl status).
  3. Edit using spaces, then validate (netplan generate, or re-read with nmcli connection show NAME).
  4. Apply with a rollback: netplan try, or schedule at now + 5 minutes to restore the backup. Keep a second SSH session open.
  5. Verify in layers as in the troubleshooting lesson: address, route, gateway ping, DNS lookup, and a real connection.
  6. Reboot at a quiet moment to prove the settings persist.

Changing DNS also has a social side: other systems cache the old answers for as long as the TTL says. Lower the TTL before a planned move.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You run sudo ip addr add 192.168.1.50/24 dev eth0 on a server. What happens after the next reboot?

    ip changes only the running kernel. For a lasting setting you must edit netplan, NetworkManager or systemd-networkd configuration.
  2. 2. A static configuration has address 192.168.1.50/24 and gateway 192.168.2.1. What is wrong?

    The gateway must be an address on the same subnet as the interface. 192.168.2.1 is on 192.168.2.0/24, not 192.168.1.0/24.
  3. 3. Which command applies a netplan change and automatically reverts it if you do not confirm within the timeout?

    netplan try waits for confirmation and rolls back after 120 seconds by default, which protects remote sessions from a bad configuration.
  4. 4. /etc/nsswitch.conf contains hosts: files dns. A name appears in both /etc/hosts and DNS with different addresses. Which one do programs get?

    files comes before dns, so the entry in /etc/hosts wins. dig ignores /etc/hosts, which is why it can disagree with what programs see.
  5. 5. With search corp.example.com and ndots:1, which name does the resolver try first for the short name db?

    db contains fewer than 1 dot, so the search domains are appended before the bare name is tried.

Summary

  • ip changes are temporary. Persistent settings live in the configuration of the tool that manages networking: netplan YAML on Ubuntu Server, NetworkManager profiles (nmcli), /etc/network/interfaces on classic Debian, or systemd-networkd files.
  • A static setup needs address plus prefix, a gateway on the same subnet, DNS servers and optionally a search domain.
  • netplan try and a second SSH session protect you from locking yourself out; YAML needs spaces, not tabs.
  • Names resolve through nsswitch.conf order (usually /etc/hosts, then DNS); search and ndots in resolv.conf decide how short names are expanded.
  • With systemd-resolved, resolv.conf points to the stub at 127.0.0.53; inspect it with resolvectl status and do not edit the generated file.
  • Set the hostname with hostnamectl set-hostname, keep /etc/hosts consistent, and follow the RFC rules for labels.

You have now covered the system administration chapter. Review the commands in the Linux Cheat Sheet, or go back to Network Troubleshooting to practise diagnosing the configuration you just built.