Sub topic 12
Check and Configure Networking and DNS
This chapter covers how to inspect and configure a server's network settings after the first boot. It assumes you can log in on the console or over SSH, you know what an IP address, a gateway and a DNS resolver are, and you are comfortable using sudo and editing files in a terminal editor.
What "configured" means
A server is network-ready when it has four things:
- An address on the right interface, either static or leased from DHCP.
- A default route, which is the gateway used for any destination outside the local subnet.
- A working DNS resolver, which turns names into addresses.
- A configuration that survives a reboot.
The installer usually sets all four. Your job on first boot is to confirm that and to change what does not fit the server's role. Servers normally need a stable address, so you either configure a static address or reserve a fixed lease for the machine on the DHCP server.
Inspect the current state
Start by reading the state. Do not change anything until you know what is there.
ip -br addr # interfaces and addresses, one line each
ip route # routing table, look for the "default via" line
resolvectl status # DNS servers per interface (systemd-resolved)In the ip -br addr output, the state column shows UP, DOWN or UNKNOWN. The loopback interface lo always shows UNKNOWN, which is normal. An Ethernet interface with no IPv4 address listed has not received a lease or has no static configuration.
Then test each layer in order, from the nearest hop outward. This shows you where the failure is.
ping -c 2 192.168.1.1 # 1. gateway (use your own gateway address)
ping -c 2 1.1.1.1 # 2. internet by address, no DNS involved
getent hosts example.com # 3. name resolution through the system resolver
ping -c 2 example.com # 4. name plus connectivityThe results map to causes:
| Failing step | Likely cause |
|---|---|
| 1 | Wrong address or subnet, cable or virtual switch problem, interface down |
| 2 (1 passes) | Missing or wrong default route, upstream filtering |
| 3 (2 passes) | DNS server unreachable or misconfigured |
| 4 only | ICMP blocked for that host, which does not prove DNS is broken |
getent hosts is a better DNS test than nslookup or dig for first checks, because it uses the same resolution path (/etc/nsswitch.conf, then the resolver) that your applications use. dig queries a DNS server directly and can succeed when applications fail.
Which tool owns the configuration
Distributions use different network managers. Editing the wrong place is the most common error, because the change is silently overwritten or ignored.
| Distribution | Default manager | Configuration location |
|---|---|---|
| Ubuntu Server | Netplan, with systemd-networkd as the backend | `/etc/netplan/*.yaml` |
| Debian | ifupdown (older installs) or systemd-networkd | `/etc/network/interfaces` |
| RHEL, Rocky, Alma, Fedora | NetworkManager | Connection profiles managed with `nmcli` |
Check which one is active before you edit anything:
systemctl is-active NetworkManager systemd-networkdThe next two sections show a static address on the two most common setups. If your DHCP server hands out the right address already, you can skip them.
Static address on Ubuntu with Netplan
Netplan reads YAML files and generates configuration for the backend. Find the interface name with ip -br addr. The example uses ens3, and you must replace it with yours.
# /etc/netplan/01-static.yaml
network:
version: 2
ethernets:
ens3:
dhcp4: false
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 192.168.1.1
- 1.1.1.1YAML is sensitive to indentation, so use spaces only. Netplan also warns when the file is readable by other users, so restrict it:
sudo chmod 600 /etc/netplan/01-static.yaml
sudo netplan trynetplan try applies the configuration and waits for you to confirm it. If you do not press Enter within 120 seconds, for example because the change cut your SSH session, it rolls back automatically. This is the safe way to change the address of a remote machine. Use sudo netplan apply only when you have console access.
If an older cloud-init file such as 50-cloud-init.yaml also defines the interface, the two files merge in name order and may conflict. On cloud images, cloud-init can regenerate that file at boot, so either edit the cloud-init configuration or disable its network management.
Static address on RHEL family with NetworkManager
NetworkManager stores settings in connection profiles. List them first:
nmcli connection showThen modify the profile for your interface. The profile name is often the interface name, such as ens3, but it can differ.
sudo nmcli connection modify ens3 \
ipv4.method manual \
ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "192.168.1.1 1.1.1.1"
sudo nmcli connection up ens3Running connection up over SSH may drop your session for a moment while the address is reapplied. If the address changed, your session ends and you reconnect to the new address. Do this from the console when you can.
How DNS resolution works on the host
Most modern distributions run systemd-resolved, or NetworkManager manages /etc/resolv.conf directly. Applications read /etc/resolv.conf, and when systemd-resolved is in use this file points to a local stub listener at 127.0.0.53. The real upstream servers are shown by resolvectl status, not by cat /etc/resolv.conf.
Do not edit /etc/resolv.conf by hand on these systems. The managing service rewrites it. Set the DNS servers in Netplan, in the NetworkManager profile, or in /etc/systemd/resolved.conf for a global setting.
For a quick check of a single lookup, including which server answered and whether the result came from cache:
resolvectl query example.comA related setting is the search domain, a suffix appended to short names. If ssh db01 should resolve to db01.internal.example, set search internal.example through the same tool that sets your nameservers.
Common errors and edge cases
- Locked out after a static change. A wrong gateway or prefix length makes the server unreachable. Use
netplan try, or keep the console open. - Wrong prefix length.
/24and/16look similar but define different subnets, and a mismatch causes partial connectivity: some hosts answer, others do not. - DHCP and static together. A static address on an interface that also has
dhcp4: trueyields two addresses. Pick one method per interface. - Duplicate IP. If another device already uses your static address, you see intermittent drops.
arping -D -I ens3 192.168.1.50reports a duplicate. - IPv6. Many networks hand out IPv6 addresses automatically. If a name resolves to an IPv6 address your network cannot route, connections hang before falling back to IPv4. Test with
ping -4andping -6to see which family fails. - Hostname resolution of itself. If
sudoprints "unable to resolve host", the machine's own hostname is missing from/etc/hosts. Add a line mapping127.0.1.1(Debian family) to the hostname you set earlier. - Cloud and virtual machines. Providers often manage addresses through DHCP and metadata. Setting a static address inside the guest can conflict with the provider's network. Check provider documentation first.
Confirm the result
Run the checks again after any change, and reboot once to confirm that the configuration persists:
ip -br addr
ip route | grep default
resolvectl status | grep -A2 "Link"
getent hosts example.comYou will use ip -br addr and ss -tlnp again in the final baseline checklist. The next chapter builds on a working network by enabling the host firewall.
Exercise
After you set a static address, ping 1.1.1.1 succeeds but ping example.com fails with "Temporary failure in name resolution". Which layer is broken, and what do you check first?
Show answer
Connectivity and routing work, because the ping by address succeeds. The failure is in DNS resolution. Run resolvectl status to see which DNS servers the interface uses. A static configuration often omits the nameservers entry, which leaves the interface without a resolver. Add the nameserver addresses in the Netplan file or the NetworkManager profile, apply the change, and test again with getent hosts example.com.
Exercise
Why should you use netplan try instead of netplan apply when changing the address of a server you reach over SSH?
Show answer
netplan try rolls back to the previous configuration automatically if you do not confirm within the timeout. If the new settings break your SSH session, the server restores the old working settings by itself. netplan apply has no rollback, so a mistake can leave the server unreachable until you get console access.