Mastering Netplan on Ubuntu: From Static IP to Professional Bridge and Bond Configurations

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

Forget /etc/network/interfaces, It’s the Era of Netplan

If you’re used to editing the /etc/network/interfaces file on older Debian versions, getting started with Netplan on Ubuntu 18.04 and later might feel a bit unfamiliar. Instead of using separate configuration commands, Netplan requires us to work with the YAML format. This declarative approach makes infrastructure management more consistent.

I once wasted an entire afternoon switching from CentOS to Ubuntu 22.04 just trying to find where the network configuration was located. Initially, YAML’s sensitivity to whitespace was quite frustrating. However, when deploying to clusters with over 50 nodes, I realized how powerful Netplan truly is. It makes synchronizing configurations via Ansible much faster and less error-prone than traditional methods.

How Does Netplan Differ from Legacy Methods?

Take a quick look at the comparison table below to understand Netplan’s place in the Linux ecosystem:

  • ifupdown (Traditional): Simple but gets messy when a system has more than 10 network cards or runs complex Bonds/Bridges.
  • NetworkManager: The top choice for Laptops/Desktops due to flexible Wi-Fi handling, but too heavy for server environments.
  • Netplan (Modern): Acts as an abstraction layer. You write a YAML file, and Netplan compiles that configuration for systemd-networkd or NetworkManager to handle.

Pros and Cons in Real-World Operations

Pros

  • Hierarchical Structure: Looking at the YAML file, you immediately see the relationship between IPs, network cards, and Bridges. Everything is very intuitive.
  • Safe Mode: The netplan try command is the most “valuable” feature. You have 120 seconds to confirm changes. If a misconfiguration causes an SSH disconnect, the system automatically rolls back to the previous state.
  • Modular Management: You can split configurations into multiple files within /etc/netplan/ for easier maintenance.

Cons

  • YAML is Extremely Strict: Just one extra space or using a Tab key instead of Space will break the entire configuration. This is the most common mistake for beginners.

Three Most Common Configuration Scenarios

Before you start editing files, use the ip a command to check the exact interface names (e.g., eth0, enp0s3, or ens18).

1. Setting Up a Correct Static IP

Access the configuration directory and open the file (usually 01-netcfg.yaml):

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

Below is a standard configuration template for a server:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]

Important Note: Starting from Ubuntu 22.04, the gateway4 keyword has been deprecated. You should use the routes structure as shown above to ensure long-term compatibility.

2. Creating a Network Bridge for Virtualization (KVM/LXD)

When running virtual machines, you need a Bridge so VMs can communicate with the external network. Instead of assigning an IP to the physical card, we assign it to the Bridge.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
  bridges:
    br0:
      interfaces: [enp0s3]
      addresses: [192.168.1.100/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8]
      parameters:
        stp: true
        forward-delay: 4

3. Configuring Bonding (LACP) for Bandwidth and Redundancy

In enterprise environments, plugging two network cables into two different switches helps avoid a Single Point of Failure. Netplan handles this very cleanly:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
    enp0s8:
      dhcp4: no
  bonds:
    bond0:
      interfaces: [enp0s3, enp0s8]
      addresses: [10.0.0.50/24]
      parameters:
        mode: 802.3ad
        mii-monitor-interval: 100
        lacp-rate: fast
        transmit-hash-policy: layer2+3

The “Critical” Configuration Application Process

Never immediately type sudo netplan apply. If there is a syntax error or an incorrect IP, you will be instantly kicked out of SSH. If the server is in a data center hundreds of miles away, this is a major problem.

Always use:

sudo netplan try

This command checks the syntax and runs a trial. If you don’t confirm within 120 seconds, the system automatically reverts to the old configuration. To see detailed errors, add the debug flag:

sudo netplan --debug apply

Real-World Experience to Avoid Silly Mistakes

  • Always use 2 spaces: Never use Tabs. YAML considers Tabs to be invalid characters.
  • Check card names: After a kernel update or hardware change, eth0 might become ens18. Use ip link show to double-check.
  • Post-Reboot Errors: If netplan apply works fine but you lose connectivity after a reboot, check the service status with systemctl status systemd-networkd.

Mastering Netplan is an important stepping stone toward Infrastructure as Code (IaC). Once you’re comfortable with YAML, you’ll find network administration becomes much more logical, clean, and professional.

Share: