Sub topic 5
Setting a Static IP with Netplan
This chapter assumes you can edit files in a terminal and use sudo. It also assumes you know what an IPv4 address, a prefix length, and a default gateway are, and that you have run the inspection commands from the earlier chapter. You also need console or out-of-band access to the server, or a plan for recovery, because a wrong network setting can cut off your SSH session.
What Netplan does
Netplan is a network configuration layer used by Ubuntu Server and some other distributions. You describe the network you want in YAML files. Netplan translates those files into configuration for a backend, which is the program that configures the interfaces. The two backends are networkd (systemd-networkd) and NetworkManager.
Netplan does not configure interfaces itself. This has two consequences:
- The
renderersetting in the YAML decides which backend receives the generated configuration. - If you use the
networkdrenderer, the settings end up in generated files under/run/systemd/network/. If you useNetworkManager, they end up under/run/NetworkManager/. You should not edit those generated files by hand.
Step 1: Record the current state
Before you change anything, capture what the server uses now. You will compare against this later, and it tells you the interface name to put in the YAML.
ip -br address show
ip route show default
hostnamectl
resolvectl statusNote three values: the interface name (for example enp1s0), the current address with its prefix length, and the gateway on the default via line. The interface name must match exactly. Names such as eth0, ens3, and enp1s0 depend on the hardware and the virtualization platform, so do not copy a name from an example without checking it.
Step 2: Find the right file
Netplan reads every *.yaml file in these directories:
/etc/netplan//run/netplan//lib/netplan/(or/usr/lib/netplan/on newer releases)
Files are processed in lexical order of their file names, and a later file overrides or merges with an earlier one for the same interface. If two files with the same name exist in different directories, /etc wins over /run, and /run wins over /lib.
List the existing files first:
ls -l /etc/netplan/On a cloud image or an installer-built server you will often see a file such as 00-installer-config.yaml or 50-cloud-init.yaml. If you add a new file with a higher number, such as 99-static.yaml, its settings merge over the old ones. That can leave the old dhcp4: true in effect for keys you did not override. Two approaches avoid confusion:
- Edit the existing file in place and replace its content.
- Move the old file out of
/etc/netplan/(keep a backup elsewhere, not in the same directory with a.yamlextension) and create a single new file.
The examples here use /etc/netplan/01-static.yaml.
Step 3: Write the YAML
This file disables DHCP on enp1s0 and sets a static address, a default route, DNS servers, and a 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]Each key has a specific job:
| Key | Meaning |
|---|---|
| `version: 2` | The Netplan schema version. It is always 2. |
| `renderer` | The backend that applies the configuration: `networkd` or `NetworkManager`. |
| `ethernets` | The section for wired interfaces. Wi-Fi uses `wifis`, bonds use `bonds`, and so on. |
| `enp1s0` | The interface name. It must match what `ip -br address` shows. |
| `dhcp4: false` | Turns off DHCP for IPv4 so the static address is the only source. |
| `addresses` | A list of addresses in `address/prefix` form. The prefix length is required. |
| `routes` | Static routes. `to: default` with `via` sets the default gateway. |
| `nameservers` | DNS servers (`addresses`) and search domains (`search`) for this interface. |
Gateway syntax: `routes` versus `gateway4`
Older tutorials use gateway4: 192.168.1.1. That key is deprecated and prints a warning on current Netplan versions. The routes form shown above is the supported way. The spelling to: default needs Netplan 0.102 or later. On an older release, write to: 0.0.0.0/0 instead, which means the same thing.
YAML rules that cause most errors
- Indent with spaces only. A tab character makes the file invalid.
- Keep indentation consistent. Child keys sit exactly one level deeper than their parent, and two spaces per level is the convention.
- A list item starts with
-. Theaddresseskey takes a list even if you have one address. - The inline form
[a, b]and the dashed form are equivalent for lists.
Multiple addresses and static routes
An interface can carry several addresses. A server that hosts a second service address can list both:
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: false
addresses:
- 192.168.1.50/24
- 192.168.1.51/24
routes:
- to: default
via: 192.168.1.1
- to: 10.20.0.0/16
via: 192.168.1.254
nameservers:
addresses: [192.168.1.1, 9.9.9.9]
search: [example.com]The second route sends traffic for 10.20.0.0/16 through a different router, 192.168.1.254. The via address must be reachable directly on one of the interface's subnets. If it is not, the route cannot be installed.
Step 4: Set permissions
Netplan files can contain secrets, such as Wi-Fi passwords or WireGuard keys. Current Netplan versions warn about files that other users can read, and some releases refuse to use them. Set safe ownership and mode:
sudo chown root:root /etc/netplan/01-static.yaml
sudo chmod 600 /etc/netplan/01-static.yamlStep 5: Test before you commit
Run netplan generate to check the syntax without changing the running network. It parses every file and writes the backend configuration, and it reports errors with a line number.
sudo netplan generateThen apply the change in a way you can undo. netplan try applies the configuration and waits for you to press Enter to accept it. If you do nothing, it rolls back after 120 seconds by default.
sudo netplan tryIf the new settings cut off your SSH session, you cannot press Enter, and the timeout restores the previous configuration. You can then reconnect and fix the file. Use --timeout to change the wait, for example sudo netplan try --timeout 30.
After you confirm, make the change permanent for the running system and the next boot:
sudo netplan applyBecause try already applied the configuration, netplan apply is mostly a safeguard here. It re-reads the files and applies them again. On a local console, where you cannot lose your connection, you can skip try and run apply directly.
Step 6: Verify
Confirm what the system is actually using, not only what the file says:
ip -br address show dev enp1s0
ip route show default
ip route get 9.9.9.9
resolvectl status enp1s0
ping -c 3 192.168.1.1You should see this:
192.168.1.50/24onenp1s0, and no leftover DHCP-assigned address. A second address with adynamicflag means DHCP is still active somewhere, often from another YAML file.- A
default via 192.168.1.1 dev enp1s0line. ip route getshowing the gateway and the source address192.168.1.50.- DNS servers
192.168.1.1and9.9.9.9and the search domainexample.comin theresolvectloutput. - Replies from the gateway.
To see the configuration Netplan merged from all files, run sudo netplan get. It prints the final YAML after merging, which is the fastest way to find an override from a forgotten file.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Invalid YAML` or a parse error with a line number | Tab characters, wrong indentation, or a missing space after a colon | Fix the indentation with spaces and run `netplan generate` again |
| The address is not applied | The interface name in the file does not match the real name | Check `ip -br address show` and correct the key under `ethernets` |
| Two addresses, one marked `dynamic` | `dhcp4: true` is set in another file that merges in | Run `netplan get` and remove or edit the other file |
| Local subnet works, nothing outside does | Missing or wrong default route | Check `ip route show default` and the `via` address |
| Some local hosts are unreachable | Wrong prefix length, for example `/16` when the network uses `/24` | Correct the prefix in `addresses` |
| Permissions warning | The file is readable by other users | Run `chmod 600` on the file |
| Settings revert after reboot | cloud-init rewrote the file | Disable cloud-init network configuration as shown above |
| `Cannot find unit` or no effect | The renderer is `NetworkManager` but the service is not running, or the reverse | Make `renderer` match the service that manages the interface |
Exercise
You edit /etc/netplan/01-static.yaml over SSH to change the address of enp1s0 and run sudo netplan try. The new gateway is wrong, so your session freezes. What happens next, and what should you check once you reconnect?
Show answer
Because you cannot press Enter to accept the change, netplan try rolls back to the previous configuration after its timeout (120 seconds by default). The old address returns and you can reconnect over SSH. After reconnecting, check the gateway in the routes section, run sudo netplan generate to validate the file, and check the result with ip route show default and ip route get 9.9.9.9 before trying again.
Exercise
After you apply a static configuration, ip -br address show dev enp1s0 lists 192.168.1.50/24 and a second address that DHCP assigned. Which command shows the merged Netplan configuration, and what is the most likely cause?
Show answer
sudo netplan get shows the merged configuration. The most likely cause is another YAML file in /etc/netplan/ (for example 50-cloud-init.yaml or a backup ending in .yaml) that sets dhcp4: true for the same interface and merges with your file. Remove or edit that file, then run sudo netplan try again.