Network configuration on modern Ubuntu Server releases—including Ubuntu 22.04 LTS and Ubuntu 24.04 LTS (Noble Numbat)—is governed by Netplan, Canonical’s declarative YAML-based network abstraction utility. Rather than editing legacy /etc/network/interfaces configurations or managing standalone systemd-networkd units manually, system administrators declare interface state inside human-readable YAML documents stored in /etc/netplan/.
When applying changes, Netplan generates the requisite backend configuration primitives for the designated renderer—primarily systemd-networkd for headless server environments or NetworkManager for desktop instances. Setting static IPv4 and IPv6 addresses, default routes, and resilient Domain Name System (DNS) nameservers requires strict compliance with YAML indentation rules and modern route syntax (the deprecated gateway4 parameter has been replaced by structured routes stanzas). This technical guide walks through identifying physical interface names, authoring production Netplan YAML files, testing configurations without risking SSH lockout, and verifying active routes.
Locating Physical Network Interfaces
Before writing a static configuration, query the Linux kernel to discover the predictable network interface name assigned to your physical network interface card (NIC):
ip link show
Modern Linux systems employ systemd predictable interface naming (e.g., enp3s0, ens18, or eth0). Note your primary active interface identifier. To inspect the current IP address assigned via DHCP prior to migration:
ip -br addr show
Understanding Modern Netplan YAML Syntax Requirements
Netplan enforces strict whitespace rules: use two spaces for each indentation level, and never insert tab characters. Furthermore, starting in Netplan 0.100, the legacy gateway4 and gateway6 keys are deprecated in favour of explicit route definitions:
| Directive Key | Functional Role | Example Value |
|---|---|---|
renderer: |
Specifies the backend daemon (networkd for servers, NetworkManager for desktops). |
networkd |
dhcp4: / dhcp6: |
Disables or enables automated DHCP leasing. | false / no |
addresses: |
Declares static IP addresses using Classless Inter-Domain Routing (CIDR) notation. | [192.168.1.50/24] |
routes: |
Specifies default gateway routes (to: default). |
via: 192.168.1.1 |
nameservers: |
Defines search domains and upstream DNS resolver IP addresses for systemd-resolved. |
addresses: [1.1.1.1, 8.8.8.8] |
Step-by-Step: Authoring the Netplan Configuration
Inspect existing configuration files located inside /etc/netplan/:
ls /etc/netplan/
Common filenames include 00-installer-config.yaml, 01-netcfg.yaml, or 50-cloud-init.yaml.
Creating a Safe Backup
Always back up the factory configuration before modifying network definitions:
sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak
Writing the Static Network YAML File
Edit the configuration file using sudo nano /etc/netplan/50-cloud-init.yaml and structure the document as follows (substitute your real interface name, IP subnet, and gateway):
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: false
dhcp6: false
addresses:
- 192.168.1.150/24
routes:
- to: default
via: 192.168.1.1
nameservers:
search:
- localdomain.internal
addresses:
- 1.1.1.1
- 1.0.0.1
- 8.8.8.8
Testing Configuration with Automatic Rollback (Preventing Lockout)
Applying network changes across a remote SSH session carries inherent risk. If you introduce a syntax error or specify an invalid default gateway, the server drops offline and disconnects your terminal session. To protect against SSH lockouts, execute:
sudo netplan try
The netplan try command executes a sandboxed configuration test. It applies the new routing tables and IP bindings while running an interactive 120-second countdown timer in your terminal. If the settings are correct, press Enter to commit the changes. If your SSH connection drops or you do not confirm within 120 seconds, Netplan automatically rolls back the network interfaces to the previous working configuration.
Applying and Verifying Network State
If you are connected via local console or have confirmed the settings with try, commit the configuration permanently:
sudo netplan apply
Verifying Assigned IP Addresses
ip addr show dev enp3s0
Confirm that the specified static IPv4 address appears with the inet prefix and proper subnet mask.
Verifying Default Routing
ip route show
Verify that default via 192.168.1.1 dev enp3s0 proto static is present at the head of the routing table.
Testing DNS Resolution via systemd-resolved
To inspect active upstream nameservers parsed by Ubuntu’s local resolver:
resolvectl status enp3s0
Verify external connectivity and domain translation:
ping -c 3 archive.ubuntu.com
Troubleshooting Common Netplan Errors
- Indentation Syntax Errors: Running
netplan generatedetects indentation errors before applying. If you receive “mapping values are not allowed here”, verify that indentation uses only spaces and matches the required nesting levels. - Cloud-Init Overwrites: In cloud environments (AWS, Azure, DigitalOcean), cloud-init may overwrite
/etc/netplan/files on reboot. To disable cloud-init network management, create the file/etc/cloud/cloud.cfg.d/99-disable-network-config.cfgcontaining:network: {config: disabled} - Interface Carrier Drops: If the interface remains in
NO-CARRIERstate after applying, confirm physical Ethernet cable connectivity or virtual network switch bridge bindings on hypervisor hosts.