Skip to main content

Ubuntu 20.04 & 22.04 Netplan Network Configuration Guide

·1148 words·6 mins
Ubuntu Netplan Linux Networking Ubuntu Server Network Configuration Systemd-Networkd YAML
Table of Contents

Ubuntu 20.04 & 22.04 Netplan Network Configuration Guide

Ubuntu 20.04 LTS and 22.04 LTS use Netplan as the network configuration abstraction layer. Netplan defines network interfaces, addresses, routes, and DNS settings through YAML files stored under /etc/netplan/, then generates configuration for the selected networking backend.

On Ubuntu Server, systemd-networkd is commonly used as the renderer, while desktop installations generally use NetworkManager.

This guide covers static IPv4 configuration, multiple addresses on a single NIC, routing metrics, validation, and common Netplan pitfalls.

🌐 Netplan Architecture and Configuration Files
#

Netplan configuration files normally reside in:

/etc/netplan/

Common filenames include:

/etc/netplan/00-installer-config.yaml
/etc/netplan/01-netcfg.yaml

The exact filename depends on how Ubuntu was installed and how the network configuration was provisioned.

A typical server configuration specifies:

network:
  version: 2
  renderer: networkd

The renderer determines which networking subsystem Netplan generates configuration for.

For server environments, networkd is generally appropriate:

renderer: networkd

Desktop installations using NetworkManager can instead use:

renderer: NetworkManager

📡 Configure a Single Static IPv4 Address
#

A static address can be assigned directly to an Ethernet interface through a Netplan YAML file.

Edit the Netplan configuration
#

First identify the available network interfaces:

ip addr

Then edit the appropriate Netplan configuration:

sudo nano /etc/netplan/01-netcfg.yaml

Use a configuration similar to:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1

Replace eth0, the IP address, prefix length, gateway, and DNS servers with values appropriate for your environment.

Why use routes instead of gateway4?
#

The older syntax:

gateway4: 192.168.1.1

has been deprecated in newer Netplan configurations.

The preferred form is an explicit default route:

routes:
  - to: default
    via: 192.168.1.1

This approach is also more flexible when configuring multiple routing paths.

Validate before applying
#

For remote systems, especially SSH-managed servers, test the configuration first:

sudo netplan try

netplan try applies the configuration temporarily and provides an automatic rollback mechanism if the change is not confirmed.

If the configuration is correct, apply it normally:

sudo netplan apply

Then verify the address:

ip addr show eth0

Check the routing table:

ip route show

🔀 Configure Multiple IP Addresses on One NIC
#

Netplan can assign multiple IPv4 addresses to the same physical interface.

This is useful when a server needs to:

  • Host services on multiple addresses
  • Bind applications to different IPs
  • Serve multiple logical networks
  • Maintain multiple routing paths
  • Support migration between network segments

A basic configuration is:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.100/24
        - 192.168.2.100/24
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Both addresses are attached directly to eth0.

Configure Multiple Default Routes
#

Multiple addresses become more complex when they belong to different subnets with different gateways.

For example:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.100/24
        - 192.168.2.100/24
      routes:
        - to: default
          via: 192.168.1.1
          metric: 100
        - to: default
          via: 192.168.2.1
          metric: 200
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

The lower metric has higher routing preference.

In this example:

192.168.1.1 → metric 100
192.168.2.1 → metric 200

The kernel therefore prefers the first default route when both are available.

🧭 Routing Metrics and Asymmetric Routing
#

Multiple gateways on the same interface can introduce routing problems if traffic leaves through a different path from the one expected by the receiving network.

Consider a server with:

192.168.1.100/24 → gateway 192.168.1.1
192.168.2.100/24 → gateway 192.168.2.1

Without deliberate routing policy, the system may select one default route for outbound traffic even when the incoming connection arrived through another network.

This can produce asymmetric routing.

Depending on the network topology, asymmetric paths can result in:

  • Dropped packets
  • Stateful firewall failures
  • Incorrect source-address selection
  • Unstable connections
  • Difficult-to-diagnose application timeouts

Route metrics can help establish a preferred default path, but they are not a universal solution for source-dependent routing.

For complex multi-homed systems, policy routing with ip rule and dedicated routing tables may be more appropriate.

🛠️ Essential Netplan and Networking Commands
#

Operation Command Purpose
Test configuration sudo netplan try Temporarily applies configuration with rollback protection
Apply configuration sudo netplan apply Applies Netplan configuration immediately
Debug configuration sudo netplan --debug apply Provides detailed diagnostic output
List interfaces ip a Displays interfaces and assigned addresses
Inspect an interface ip addr show eth0 Displays addresses assigned to eth0
Inspect routes ip route show Displays the kernel routing table
Inspect routing rules ip rule show Displays policy-routing rules

For remote production systems, netplan try should generally be preferred over immediately executing netplan apply when testing potentially disruptive changes.

🧪 Troubleshooting Netplan Configuration
#

Netplan is sensitive to YAML syntax, interface names, and indentation. Several common mistakes can prevent a configuration from being applied correctly.

YAML indentation
#

YAML uses indentation to represent structure.

Use spaces rather than tabs and maintain consistent indentation:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false

Incorrect indentation can cause parsing failures or produce a configuration different from what was intended.

Verify the interface name
#

Modern Ubuntu systems may use predictable interface names such as:

enp1s0
ens18
eno1

rather than:

eth0

Always verify the actual interface name:

ip link show

Then reference that name in the Netplan YAML.

Inspect Netplan-generated behavior
#

When configuration does not behave as expected, run:

sudo netplan --debug apply

Then inspect the resulting network state:

ip addr
ip route
ip rule

This helps distinguish YAML parsing problems from routing or backend-level issues.

⚙️ gateway4 and Modern Default Routes
#

Legacy configurations commonly contain:

gateway4: 192.168.1.1

Modern Netplan configurations should instead express the default gateway as a route:

routes:
  - to: default
    via: 192.168.1.1

This explicit route representation is especially useful when the host has multiple gateways, custom metrics, or more advanced routing requirements.

🔐 Production Considerations
#

Static network configuration should be tested carefully on remotely administered systems.

Before changing a production interface:

  1. Confirm the correct interface name.
  2. Validate YAML indentation.
  3. Confirm the address prefix matches the intended subnet.
  4. Verify the gateway is reachable through that subnet.
  5. Use netplan try when possible.
  6. Check the resulting routing table.
  7. Test DNS resolution and application connectivity.

For simple single-interface servers, a static address with one default route is usually sufficient.

For multi-homed servers, high-availability systems, routers, virtualization hosts, or systems with independent ingress and egress paths, explicit policy routing may be required rather than relying solely on route metrics.

📌 Practical Configuration Summary
#

A minimal static IPv4 configuration for Ubuntu 20.04 or 22.04 can be expressed as:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1

Apply it safely with:

sudo netplan try
sudo netplan apply

Then verify:

ip addr show eth0
ip route show

The key principle is to keep simple deployments simple: use one static address and one default route when that is all the topology requires. Introduce route metrics and policy routing only when the network design actually calls for multiple paths.