Understanding Netplan in Ubuntu
For many years, network configuration in Ubuntu and Debian-based systems was managed by editing the /etc/network/interfaces file. However, starting with Ubuntu 17.10 and becoming standard in Ubuntu 18.04 LTS and beyond, Canonical introduced Netplan. Netplan is a network configuration abstraction renderer that uses simple YAML files to configure network interfaces.
Netplan does not manage the network itself. Instead, it reads the YAML configuration files and generates the necessary backend configurations for either systemd-networkd (typically used on Ubuntu Servers) or NetworkManager (typically used on Ubuntu Desktop). This provides a modern, unified, and declarative way to manage complex networking setups, such as static IP addresses, bonding, and VLANs.
Where to Find Netplan Configuration Files
Netplan configuration files are stored in the /etc/netplan/ directory. The files must end with a .yaml extension.
By default, you might find a file named something like 01-network-manager-all.yaml (on Desktop) or 00-installer-config.yaml (on Server). If there are multiple YAML files in this directory, Netplan parses them in alphanumeric order. Settings in later files will override settings in earlier files.
How to Configure a Static IP Address with Netplan
One of the most common tasks for Ubuntu Server administrators is assigning a static IP address to a network interface rather than relying on DHCP.
Step 1: Identify Your Network Interface
Before writing the configuration, you need to know the name of your network interface. Open the terminal and run:
ip a
Look for your primary ethernet interface, which is usually named something like eth0, ens33, or enp3s0.
Step 2: Edit the Netplan YAML File
Open the default Netplan configuration file using a text editor like Nano. Remember to use sudo as this requires root privileges:
sudo nano /etc/netplan/00-installer-config.yaml
Modify the file to set a static IP. Important: YAML relies strictly on indentation (using spaces, never tabs). Incorrect indentation will cause the configuration to fail.
A basic static IP configuration looks like this:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: no
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
In this example:
- enp3s0 is the interface name.
- dhcp4: no disables automatic IP assignment.
- addresses assigns the static IP
192.168.1.100with a/24subnet mask. - routes defines the default gateway (router IP).
- nameservers assigns the DNS servers (Google DNS in this case).
Save the file and exit the text editor.
How to Apply and Test Netplan Configurations
Because syntax errors in YAML files can immediately drop your network connection, Netplan includes a safe testing mechanism.
Testing the Configuration
Instead of applying the changes immediately, use the try command:
sudo netplan try
This command will apply the new settings and start a 120-second countdown. If your SSH connection drops or you realise you made a mistake, you can simply wait for the countdown to expire, and Netplan will automatically revert to the previous working configuration. If the connection is successful, press Enter to accept and permanently apply the changes.
Applying the Configuration Directly
If you are confident in your configuration or working directly on the physical machine rather than over SSH, you can apply the changes instantly:
sudo netplan apply
This command parses the YAML files, generates the backend configurations, and restarts the relevant networking services immediately.
Troubleshooting Netplan Errors
If the netplan apply command returns an error, the most common culprit is incorrect YAML formatting. Ensure that:
- You have not used any Tab characters for indentation.
- Each level of indentation uses exactly two or four spaces consistently.
- You have not missed any colons or hyphens in lists.
You can also run the apply command with debugging output to see exactly where the parsing failed:
sudo netplan --debug apply
This will provide a detailed log of the backend generation process, helping you pinpoint the exact line in your YAML file causing the issue.