Mastering Netplan on Ubuntu: Complete Guide to Network Configuration with YAML

1. Overview of Netplan on Ubuntu

What is Netplan?

Netplan is a network configuration management tool introduced in Ubuntu versions starting from 17.10. Previously, tools such as ifconfig and /etc/network/interfaces were used, but Netplan provides a new approach to network configuration. One of Netplan’s greatest advantages is that it uses YAML files to describe network settings. This makes configuration simple and consistent, allowing even complex network environments to be managed easily.

Netplan supports backends such as NetworkManager and systemd-networkd and is available on both Ubuntu Desktop and Server editions. This enables unified network management across different environments.

Why use Netplan?

Compared to traditional network configuration methods, Netplan offers the following advantages:

  1. Simple syntax: YAML format is intuitive and clearly structured, making configurations easy to understand even for beginners.
  2. Unified management: Since it works on both desktop and server environments, various network configurations can be managed centrally.
  3. Dynamic changes: Editing and applying the configuration file allows real-time updates to network settings.

Basic structure of Netplan

Netplan configuration files are typically located in the /etc/netplan/ directory and use the .yaml extension. These files contain information such as network interface configurations, IP addresses, and DNS server details.

Below is an example of a basic Netplan configuration:

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

In this example, the enp3s0 Ethernet interface is configured to obtain an IP address via DHCP.

The role of Netplan on Ubuntu 18.04 LTS and later

Netplan is installed by default on Ubuntu 18.04 LTS and later versions and is widely used for network management in desktop and server environments. In server environments, where multiple network interfaces or static IP assignments are often required, Netplan’s flexibility becomes particularly useful.

Next, we will explore practical examples of configuring networks using Netplan.

2. Basic Netplan Configuration

Location of Netplan configuration files

Netplan configuration files are typically stored in the /etc/netplan/ directory. You can modify network settings by editing these .yaml files. File names such as 50-cloud-init.yaml are common, but may vary depending on the environment.

To open the configuration file, use a text editor such as vi or nano:

sudo vi /etc/netplan/50-cloud-init.yaml

Dynamic IP address configuration (DHCP)

To automatically obtain an IP address using DHCP, use the following YAML configuration. This is the simplest setup and is commonly used in home and office environments.

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

Static IP address configuration

Some environments require assigning a static IP address to servers or specific devices. The following example demonstrates how to configure a static IP address.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Applying configuration

After editing the configuration file, apply the Netplan configuration using the command below:

sudo netplan apply

Verifying configuration

To verify whether Netplan settings have been applied successfully, check the network interface status with the following command:

ip a

3. Configuring Multiple Network Interfaces

Multiple Ethernet interface configuration

Devices with multiple network interfaces can assign different settings to each interface. The example below configures two Ethernet interfaces:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true
    enp4s0:
      addresses:
        - 192.168.1.150/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Bonding for redundancy

Bonding combines multiple network interfaces into a single virtual interface, providing redundancy and improving availability. The example below creates a bond interface named bond0:

network:
  version: 2
  renderer: networkd
  bonds:
    bond0:
      interfaces:
        - enp3s0
        - enp4s0
      addresses:
        - 192.168.1.200/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
      parameters:
        mode: active-backup
        primary: enp3s0

Wi-Fi configuration

Netplan also supports Wi-Fi configuration. The following example connects to a specific SSID:

network:
  version: 2
  renderer: networkd
  wifis:
    wlp2s0:
      access-points:
        "my_wifi_network":
          password: "password1234"
      dhcp4: true

VLAN configuration

Virtual LANs (VLANs) can logically segment networks. The example below creates a VLAN on enp3s0:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true
  vlans:
    vlan10:
      id: 10
      link: enp3s0
      addresses:
        - 192.168.10.1/24

4. Advanced Netplan Configuration

Static routing configuration

When connecting networks through multiple routers, static routing is required. The example below specifies a route for accessing a specific network:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 192.168.1.100/24
      routes:
        - to: 10.0.0.0/24
          via: 192.168.1.1

This configuration sets a static route for the 10.0.0.0/24 network through the default gateway 192.168.1.1.

Multiple default gateways

Netplan allows setting different default gateways per interface. This is useful when accessing the Internet through different network segments:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
    enp4s0:
      addresses:
        - 10.0.0.100/24
      gateway4: 10.0.0.1

DNS server configuration

The example below specifies Google Public DNS servers:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 192.168.1.100/24
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Advanced bonding configuration

Bonding modes can be changed for different behaviors. The example below configures round-robin bonding:

network:
  version: 2
  renderer: networkd
  bonds:
    bond0:
      interfaces:
        - enp3s0
        - enp4s0
      addresses:
        - 192.168.1.200/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
      parameters:
        mode: balance-rr

The balance-rr mode alternates traffic across the two interfaces, improving performance through bandwidth distribution.

Advanced VLAN configuration

VLANs are used in large-scale networks to logically divide environments. The example below assigns VLAN ID 100:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true
  vlans:
    vlan100:
      id: 100
      link: enp3s0
      addresses:
        - 192.168.100.1/24

This assigns VLAN ID 100 to the enp3s0 interface, creating a segmented virtual network.

5. Netplan Troubleshooting

Although Netplan is convenient, configuration mistakes or system limitations may cause issues. This section explains common problems and their solutions.

Common Netplan issues and causes

1. Configuration not applied

  • YAML indentation error: YAML is strict about indentation. Incorrect spacing prevents correct parsing.
  • Incorrect interface names: Ensure interface names match the output of the ip a command.

Solutions

  1. Run netplan apply after saving the configuration file.
  2. Use sudo netplan try to test changes before applying them permanently.
sudo netplan apply
sudo netplan try

2. Network connection errors

  • Incorrect gateway or DNS settings: Verify IP addresses and DNS configuration.
  • Physical interface problems: Check cables and hardware.

Solutions

  1. Use the ping command to test connectivity:
ping 8.8.8.8
  1. Reapply network configuration and restart services:
sudo systemctl restart networkd

3. Error messages during netplan apply

Errors occur when the configuration is incorrect or the interface is not recognized.

  • Example error message: Error in network configuration: failed to bring up device enp3s0

Verify the correct interface name using ip a.

Solution

Check indentation, spelling, interface names, and IP address correctness.

Checking logs

System logs are useful for troubleshooting. Use the command below to view network-related logs:

journalctl -u systemd-networkd

This displays detailed information to help resolve configuration errors.

6. Summary and Next Steps

Using Netplan allows Ubuntu network settings to be managed simply and efficiently. Below is a summary and suggested next steps for deeper exploration.

Main advantages of Netplan

  1. Intuitive YAML configuration: Easy to read and modify.
  2. Flexible network design: Supports multiple interfaces, bonding, routes, and VLANs.
  3. Unified interface: Works across systemd-networkd and NetworkManager.
  4. Real-time updates: Apply changes immediately with one command.

Recommended next steps

  1. Virtual network design: Use multiple VLANs to segment networks logically.
  2. IPv6 configuration: Prepare for modern networking infrastructure.
  3. Automation scripts: Automate configurations using Ansible or Puppet.
  4. Security enhancements: Strengthen firewall and access control settings.

Additional resources

侍エンジニア塾