Where to start First steps track · 15 stages
Linux Labs

Persistent networking with netplan

6 min Ubuntu 24.04 · netplan

Persistent network configuration on Ubuntu — netplan YAML files, static addresses and DHCP, applying safely.

Cheat sheet

4 commands Printable version
  • sudo netplan generate check syntax and build the backend config
  • sudo netplan try apply with an automatic rollback after 120s
  • sudo netplan apply apply right away
  • cat /etc/netplan/*.yaml what the files currently describe

Overview

ip commands change the network temporarily, until reboot. Persistent configuration on Ubuntu Server is set through netplan: YAML files in /etc/netplan/, which netplan translates into the backend's configuration — on servers this is usually systemd-networkd.

bash
ls /etc/netplan/                  # which network description files exist
cat /etc/netplan/01-netcfg.yaml   # a specific file's contents

netplan get doesn't print the current network state — it prints the merged configuration from the files in /etc/netplan/, i.e. what's supposed to be applied. Check the actual state of interfaces and addresses separately: ip -br a, ip -br l, networkctl status.

Configuration

DHCP on an interface:

yaml
# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true                 # get an address via DHCP

A static address:

yaml
network:
  version: 2
  ethernets:
    eth0:
      addresses: [192.168.1.10/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [1.1.1.1, 8.8.8.8]

YAML is sensitive to indentation: spaces only, no tabs, and the nesting level must be consistent throughout the file.

Recipes

3

Applying changes

bash
sudo netplan generate              # check syntax without applying
sudo netplan try                   # apply with a rollback after 120s if not confirmed with Enter
sudo netplan apply                 # apply right away, no rollback
sudo netplan --debug apply         # with verbose output

Over SSH on a remote machine, change network configuration with netplan try: if the new config breaks the connection, after 120 seconds without confirmation netplan restores the previous file and access comes back. netplan apply gives you no such safety net — a mistake in an address or gateway can cut off the only channel to the machine.

Several files under /etc/netplan

All the files are applied together, in name order (01-..., 50-...); a later file can override an earlier one's settings for the same interface. Keep one interface in one file, so you don't have to hunt for a conflict across several.

A permanent route to a network behind a neighbour

A route added with ip route add lasts until a reboot. A permanent one goes into routes: of the interface the neighbour is reachable through: to is the destination network, via is the neighbour's address on the shared network. The default route (to: default) stays as it was; the new entry sits next to it:

yaml
network:
  version: 2
  ethernets:
    eth0:
      addresses: [192.168.50.10/24]
      routes:
        - to: default
          via: 192.168.50.1
        - to: 10.99.0.0/24        # the network behind the neighbour
          via: 192.168.50.20      # the neighbour's address
bash
sudo netplan try                  # or apply on a practice machine
ip route get 10.99.0.1            # … via 192.168.50.20 dev eth0 …
ip route show 10.99.0.0/24        # the route in the table

ip route get shows which way the kernel will send a packet to an address: if the answer has the neighbour as via, the route works. The via address must be on the interface's own network, or the route will not be installed. More on the routing table in ip.

Troubleshooting

5

The route is gone after a reboot

It was added with ip route add, and it is not in the files under /etc/netplan/. Write the route into the interface's routes: and apply the configuration.

Network access disappeared after apply

There's an error in the config: a wrong address, gateway, or indentation. Go through the hypervisor's console, fix the file, and apply it with netplan try, not apply. If access was lost specifically because of apply without a rollback, switch to try for every later network change on this machine.

netplan apply: nothing changed

Check the syntax (sudo netplan generate will show the error and the file), whether you edited the right file, and whether the interface name (ip -br a) matches what's in the YAML.

No DNS after configuring it

Add a nameservers: addresses: [...] block to the interface's config. Check which servers are actually in use: resolvectl status (see dns-resolv).

netplan get shows something different from what's on the system

netplan get only reads the files in /etc/netplan/, not the actual state of the interfaces. If there's a big gap after manual ip commands or a failed apply, cross-check against ip addr and networkctl status, bring the files in line, and apply them again.

Terms

2
YAML
A text format for structured data in which indentation establishes nesting. Also appears in: docker compose, ansible-playbook: running a playbook, cloud-init, ip and 1 more
network renderer
The networking implementation for which Netplan generates configuration.

See also

  • ip — Manage interfaces, addresses and routes (iproute2) — the replacement for ifconfig/route.
  • dns-resolv — How a name becomes an IP — resolv.conf, systemd-resolved, /etc/hosts, dig.
  • network-diagnostics — A ladder of connectivity checks — from the gateway to DNS and the destination port.
Loading…