Knowledge
Netplan on Ubuntu 24.04: static IPs and DHCP with a single YAML file
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:
- networkd (
systemd-networkd): commonly used on servers, which have no graphical interface. - NetworkManager: commonly used on desktop machines with a graphical interface.
That way the same YAML syntax works on many kinds of machines. In this article we use renderer: networkd.
Preparation
- A machine running Ubuntu 24.04 LTS (a physical machine, a virtual machine or a container all work) and an account with
sudorights. - Know the name of the network card. Run the following command to see it:
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.
- All addresses in this article are reserved for documentation (the
192.0.2.0/24block, the domainexample.com). When you do this for real, replace them with your own network's addresses.
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:
addresses: a list of addresses with the prefix length./24means a subnet with the mask 255.255.255.0.routeswithto: default: the default route, that is the "gateway" that packets go through when the destination is outside the subnet. The old syntaxgateway4:has been marked as deprecated, so you should useroutes.nameservers: the DNS servers and the list of search domains (search).
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
- Wrong indentation or using Tab: netplan reports a YAML syntax error. Run
sudo netplan generateto find out which line is wrong. - Forgetting to turn off DHCP when setting a static IP: the machine may get an extra address, which causes confusion. Set
dhcp4: false. - Wrong network card name: the configuration cannot be applied because no card has that name. Check again with
ip -br link. - Cloud-init overwriting the configuration: on a machine created from a cloud image, cloud-init may regenerate
50-cloud-init.yamlat boot. To manage the network yourself, create the file/etc/cloud/cloud.cfg.d/99-disable-network-config.cfgwith the contentnetwork: {config: disabled}. - Wrong gateway or wrong subnet: the machine can ping other machines on the same network but cannot reach the outside. Check whether
viais in the same subnet as the machine's address. - Warning: do not apply the configuration with
netplan applywhile you are connected to the machine remotely over SSH without first testing it withnetplan try, because a small mistake can cut your connection and force you to go to the machine in person to fix it.
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
- The netplan(5) man page: https://manpages.ubuntu.com/manpages/noble/en/man5/netplan.5.html
- The netplan-try(8) man page: https://manpages.ubuntu.com/manpages/noble/en/man8/netplan-try.8.html
- Ubuntu Server, configuring networks: https://documentation.ubuntu.com/server/explanation/networking/configuring-networks/