Sub topic 2
Setting a Static IP with Netplan
This chapter assumes you can edit files in a terminal, use sudo, and read an IPv4 address with a prefix length, such as 192.168.1.50/24. Have console access ready before you change a remote server, because a bad network change can end your SSH session.
Setting a static IP with Netplan
Netplan is the configuration layer used on Ubuntu Server and many other Debian-based systems. You describe the network in YAML, and Netplan generates the settings for its backend, usually systemd-networkd. Start by recording the current state:
ip -br address show dev enp1s0
ip route show defaultCreate /etc/netplan/01-static.yaml:
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: false
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [192.168.1.1, 9.9.9.9]
search: [example.com]Restrict the file to root, then test the change:
sudo chmod 600 /etc/netplan/01-static.yaml
sudo netplan trynetplan try applies the change and rolls it back after a timeout unless you confirm it. Run sudo netplan apply to make the change permanent.
Setting a static IP with NetworkManager
Many desktop and some server images use NetworkManager, a service that manages connections. Run nmcli device status to see whether it controls your interface. If it does, edit the connection profile:
nmcli connection show
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 "192.168.1.1 9.9.9.9" \
ipv4.dns-search example.com
sudo nmcli connection up "Wired connection 1"Setting the hostname and keeping /etc/hosts consistent
A hostname is the name a machine gives itself. Set it with:
sudo hostnamectl set-hostname web01.example.com
hostnamectl --staticThen make /etc/hosts match the new name:
127.0.0.1 localhost
192.168.1.50 web01.example.com web01Use the server's fixed address on the second line. If the file still maps the name to 127.0.1.1, which some Debian-style systems do by default, the hostname resolves locally to the wrong address.
How the system turns a name into an address
The hosts: line in /etc/nsswitch.conf sets the lookup order. On most servers it reads files dns, which means the system checks /etc/hosts first and then asks the DNS resolver.
Use these commands to diagnose common failures:
| Symptom | Likely cause | Check |
|---|---|---|
| Server unreachable after a change | Wrong prefix length or gateway | `ip route show default` |
| Name does not resolve | Wrong nameserver or hosts entry | `resolvectl query example.com` |
| Local name points to wrong address | Stale line in /etc/hosts | `getent hosts web01.example.com` |
Exercise
Your server uses 10.0.0.20/24, but its default gateway is 10.0.1.1. What is wrong, and how do you confirm it?
Show answer
The gateway is outside the 10.0.0.0/24 subnet, so the server cannot reach it. Change the gateway to an address in that subnet, such as 10.0.0.1. Confirm with ip route show default and ping -c 3 10.0.0.1.