Knowledge

Netplan on Ubuntu 24.04: static IPs and DHCP with a single YAML file

Updated 2026-11-08 · Present

Goal

After this article you will know what netplan is, be able to read the network configuration file on Ubuntu 24.04 LTS, and do the two most common tasks: let the machine get its address through DHCP, or give the machine a fixed IP address (a static IP). The article also shows how to test a new configuration safely so you do not "cut your own throat" when working remotely.

What is netplan?

Netplan is the default network configuration tool of Ubuntu. You describe the network you want in YAML files placed in the /etc/netplan/ directory. Netplan does not manage the network itself; it reads those files and generates configuration for a "backend" (renderer) that does the real work underneath:

That way the same YAML syntax works on many kinds of machines. In this article we use renderer: networkd.

Preparation

ip -br link

The name usually looks like enp1s0, ens3 or eth0. In this article we use enp1s0 as the example; replace it with the real name on your machine.

Steps

Steps

Step 1: Look at the existing configuration file

ls -l /etc/netplan/
sudo cat /etc/netplan/*.yaml

On many freshly installed servers you will see a file such as 50-cloud-init.yaml or 00-installer-config.yaml. Make a backup before editing:

sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak

Note: netplan reads every file ending in .yaml in the directory. The backup file ending in .bak is ignored, exactly as we want.

Step 2: Configure DHCP

This is the simplest form; the router or a DHCP server gives the machine its address:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: true

Note: YAML is very sensitive to indentation. Use spaces, not the Tab key, and keep each level indented by two spaces for consistency.

Step 3: Configure a static IP

To set a fixed address, turn off DHCP and declare the address, the default route and the DNS server:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: false
      addresses:
        - 192.0.2.10/24
      routes:
        - to: default
          via: 192.0.2.1
      nameservers:
        addresses:
          - 192.0.2.53
        search:
          - example.com

What each key means:

To use a static IPv6 address, add an IPv6 address to addresses (for example 2001:db8::10/64) and a matching IPv6 default route, in the same style as above.

Step 4: Set the file permissions

From Ubuntu 22.04 onward, netplan warns if the configuration file lets other users read it, because the file may contain Wi-Fi passwords. Set tight permissions:

sudo chmod 600 /etc/netplan/50-cloud-init.yaml

Step 5: Test first, then apply

Use netplan try instead of applying directly:

sudo netplan try

This command applies the new configuration and starts a countdown (120 seconds by default). If you do not press Enter to confirm within that time, it automatically goes back to the old configuration. That is a very valuable safety net when you are connected to the machine over SSH: if you get the address wrong and lose the connection, the configuration is reverted automatically.

When everything looks fine, you can apply it for good with:

sudo netplan apply

In addition, sudo netplan generate only checks the syntax and generates the configuration files without changing the running network.

Check

ip -br address show enp1s0
ip route

You should see the address 192.0.2.10/24 attached to the network card and a line default via 192.0.2.1. If you use DHCP, the address shown is the one the router assigned. How to read the output of the ip commands is explained in more detail in the article Checking network connectivity on Ubuntu with ip, ss, ping and tracepath; checking DNS is covered in the article resolvectl and DNS on Ubuntu: how your machine looks up domain names.

Common mistakes

In short

Netplan lets you describe the network in YAML in /etc/netplan/: dhcp4: true to get an address automatically, or addresses, routes and nameservers to set a static IP. Back up the file, set the permissions to 600, test with netplan try, and only then apply it for good.

References