Sub topic 5
Setting a Static IP with Netplan
This chapter assumes that you can use a Linux shell, edit files, run commands with sudo, and understand IPv4 addresses, subnet prefixes, and default gateways. It also assumes that you have console or out-of-band access in case a remote network change interrupts SSH.
How Netplan fits into the network stack
Netplan is a configuration layer used by many Ubuntu systems. It reads YAML files from /etc/netplan/ and generates configuration for a backend such as systemd-networkd or NetworkManager. The backend then configures the interface.
A static configuration gives the server an address that does not change when the DHCP lease expires. The address must fit the local network, the gateway must be reachable through that network, and the DNS servers must be usable from the server.
For example, 192.168.1.50/24 means that the server has address 192.168.1.50 and a 24-bit network prefix. The prefix places the server on the 192.168.1.0/24 network. A default route through 192.168.1.1 handles destinations outside that local network. DNS servers translate names such as example.com into addresses.
Inspect the current configuration
Identify the real interface name before writing the file. Do not assume it is eth0; modern systems often use names such as enp1s0.
ip -br address show
ip route show default
hostnamectl
resolvectl statusRecord the current address, gateway, DNS servers, and interface name. If the server currently uses DHCP, check the DHCP-provided values before replacing them. You can also identify the network backend by inspecting existing Netplan files:
ls -l /etc/netplan/
grep -R "renderer:" /etc/netplan/
systemctl is-active systemd-networkd NetworkManagerUse the backend already selected by the system. Mixing independent configuration methods for the same interface can produce unpredictable results.
Write the Netplan configuration
Create or edit a file such as /etc/netplan/01-static.yaml. YAML uses indentation to represent structure, so use spaces rather than tabs.
The following example configures interface enp1s0 with a static IPv4 address, a default route, two DNS servers, and a DNS search domain.
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]Replace the interface, address, prefix, gateway, DNS addresses, and search domain with values from your network plan. The gateway must be on a directly connected network. For example, 192.168.2.1 is not a valid gateway for 192.168.1.50/24 unless an additional route provides access to it.
Protect the file because it contains network settings that should not be casually changed:
sudo chmod 600 /etc/netplan/01-static.yamlApply a remote change safely
netplan try applies the generated configuration temporarily and asks you to confirm it. If you lose connectivity or fail to confirm, Netplan can roll back the change. This is safer than immediately applying a change over SSH.
sudo netplan try
sudo netplan applyConfirm the test only after checking that your SSH session remains usable. Run netplan apply after confirmation so the configuration is applied normally. Keep a console session open when possible.
Verify the result
Check the address, route, resolver, and connectivity separately. Separating these tests helps identify which layer failed.
ip -br address show dev enp1s0
ip route get 9.9.9.9
resolvectl query example.com
getent hosts example.com
ping -c 3 192.168.1.1An address problem appears in ip address, a routing problem appears in ip route get, and a DNS problem appears in the resolver commands. A failed ping does not always prove that routing is broken because firewalls may block ICMP.
Exercise
Why does 192.168.1.50/24 usually require the gateway to be in the 192.168.1.0/24 network?
Show answer
The /24 prefix defines the directly connected local network. The server can send directly to a gateway on that network, while destinations outside it use the gateway through the default route.