A Comprehensive Guide to Using Netplan on Ubuntu | From Basic to Advanced Network Configuration

1. Overview of Netplan in Ubuntu

What is Netplan?

Netplan is a network configuration management tool introduced in Ubuntu 17.10 and later versions. Previously, ifconfig and /etc/network/interfaces were used for network configuration, but Netplan provides a new, more structured approach. One of Netplan’s key features is the use of YAML files for network configuration, allowing for a simple and consistent setup that is easy to manage, even for complex network configurations.

Netplan supports backends such as NetworkManager and systemd-networkd, making it suitable for both Ubuntu desktop and server environments. This enables a unified method of managing networks across different setups.

Why Use Netplan?

Compared to traditional network configuration methods, Netplan offers several advantages:

  1. Simple Syntax: YAML format is intuitive and structured, making configurations easier to read and understand, even for beginners.
  2. Unified Management: It can be used in both desktop and server environments, allowing centralized management of various network configurations.
  3. Dynamic Changes: You can modify the configuration file and apply changes instantly, reflecting updates in real-time.

Basic Structure of Netplan

Netplan configuration files are typically located in the /etc/netplan/ directory and have a .yaml extension. These files contain settings for network interfaces, IP addresses, and DNS servers.

An example of a basic Netplan configuration file is shown below:

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

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

Role of Netplan in Ubuntu 18.04 LTS and Later

From Ubuntu 18.04 LTS onwards, Netplan is installed by default and is widely used for network management in both server and desktop environments. In server environments, where multiple network interfaces and static IP configurations are often required, Netplan proves to be especially useful.

Next, we will go over how to configure networks using Netplan.

2. Basic Netplan Configuration

Location of Netplan Configuration Files

Netplan configuration files are typically stored in the /etc/netplan/ directory. By editing the .yaml files within this directory, you can modify network settings. A common file name is 50-cloud-init.yaml, but it may vary depending on the system environment.

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

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

Configuring Dynamic IP Address (DHCP)

To obtain an IP address automatically via 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

Configuring a Static IP Address

In some environments, it is necessary to assign a fixed IP address to servers or specific devices. The following example shows 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 changes using the following command:

sudo netplan apply

Verifying Configuration

To verify whether the Netplan settings have been applied correctly, use the following command to check the status of network interfaces:

ip a

3. Configuring Multiple Network Interfaces

Setting Up Multiple Ethernet Interfaces

Servers and devices with multiple network interfaces can assign different IP addresses and settings to each interface. The following example configures two Ethernet interfaces with different settings:

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

Configuring Network Bonding for Redundancy

Network bonding combines multiple network interfaces into a single virtual interface to improve redundancy and availability. The following example configures two Ethernet interfaces as a bonded interface 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

Configuring Wi-Fi Connections

Netplan can also configure Wi-Fi connections. The following example sets up a connection to a specific SSID:

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

Configuring VLANs

For specific use cases, Virtual LANs (VLANs) can be used to logically segment networks. Netplan supports VLAN configuration. The following example sets up a VLAN on the enp3s0 interface:

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

Configuring Static Routing

When connecting networks through multiple routers, static routing is required. Using Netplan, you can configure static routes to specify the path for traffic to certain IP addresses or networks. The following is an example of static routing:

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

In this configuration, the enp3s0 interface is assigned a static route to the 10.0.0.0/24 network via the default gateway 192.168.1.1. This allows traffic to be routed through a designated path.

Configuring Multiple Default Gateways

When multiple network interfaces exist, Netplan allows setting different default gateways for each interface. This is useful for environments that require internet access through different network segments. The following is an example configuration:

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

Configuring DNS Servers

Netplan allows easy configuration of static DNS servers. The following example specifies Google’s public DNS servers (8.8.8.8 and 8.8.4.4):

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

Advanced Network Bonding

In addition to basic bonding, you can adjust bonding modes to achieve different functionalities. The following example 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 (round-robin) mode alternates traffic across the interfaces to distribute bandwidth, improving performance. Other modes, such as active-backup for failover and balance-tlb for load balancing, can be selected based on requirements.

Advanced VLAN Configuration

VLAN (Virtual LAN) is used in large networks to logically segment traffic. Netplan supports VLAN configuration, as shown in the following example:

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

Here, VLAN ID 100 is assigned to enp3s0, and it is given a static IP address of 192.168.100.1. This helps in segmenting the network for improved security and traffic management.

5. Netplan Troubleshooting

While Netplan is highly convenient, configuration errors and system-specific issues may arise. This section explains common Netplan problems and their solutions, helping you efficiently address network connection errors and misconfigurations.

Common Netplan Issues and Causes

1. Configuration Not Applying

If changes made in Netplan are not being applied, possible causes include:

  • YAML Indentation Errors: YAML syntax is strict about indentation. Incorrect spaces or tabs can cause the configuration to be invalid.
  • Incorrect Interface Name: Netplan requires the correct network interface name. Use the ip a command to verify and ensure it matches your configuration.

Solutions

  1. After saving the configuration file, apply it using the following command:
sudo netplan apply
  1. If an error occurs, use the sudo netplan try command to test the changes before applying them permanently.
sudo netplan try

2. Network Connection Errors

If the network is not working, possible causes include:

  • Incorrect Gateway or DNS Configuration: Ensure the correct IP addresses and DNS servers are specified.
  • Physical Interface Issues: Check network cables and hardware to confirm they are properly connected.

Solutions

  1. Use the ping command to check connectivity, for example:
ping 8.8.8.8
  1. Restart the network service if needed:
sudo systemctl restart networkd

Checking Logs for Errors

To troubleshoot issues, system logs provide valuable insights. Use the following command to check Netplan-related logs:

journalctl -u systemd-networkd

6. Summary and Next Steps

Netplan simplifies Ubuntu network configuration, making it more efficient and manageable. Below is a summary of its key benefits:

  1. Intuitive YAML-based Configuration: Easier to read and write compared to traditional methods.
  2. Flexible Network Setup: Supports multiple interfaces, bonding, static routing, and VLANs.
  3. Unified Interface: Works across different Ubuntu environments with a consistent approach.
  4. Real-time Configuration Changes: Network settings can be updated instantly.

Further Learning

Once comfortable with the basics, consider exploring:

  • Virtual Network Setup: Implement VLANs for enhanced network security.
  • IPv6 Support: Configure IPv6 for future-proof networking.
  • Automation with Scripts: Use Ansible or Puppet to automate network configurations.

Useful Resources