Netplan Guide

Netplan is a YAML-based network configuration tool for Linux systems. It simplifies the process of configuring network interfaces and is used by various distributions like Ubuntu, Fedora, and Debian. In this guide, we’ll explore some common Netplan examples to help you set up and manage your network connections efficiently.

Prerequisites

Before you begin, ensure that you have:

  • A Linux system with Netplan installed (pre-installed on most modern distributions).
  • Basic knowledge of YAML syntax.

Example 1: Configuring a Static IPv4 Address

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses: [192.168.1.2/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

In this example, we configure the interface enp0s3 with a static IPv4 address (192.168.1.2), a subnet mask of 24 (which corresponds to 255.255.255.0), and set the gateway to 192.168.1.1. We also specify Google’s DNS servers (8.8.8.8 and 8.8.4.4) for name resolution.

Example 2: DHCP Configuration

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

This configuration sets enp0s3 to obtain an IPv4 address dynamically using DHCP.

Example 3: Configuring VLANs

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
  vlans:
    vlan10:
      id: 10
      link: enp0s3
      addresses: [192.168.10.2/24]
      gateway4: 192.168.10.1

In this example, we first configure enp0s3 to obtain an IPv4 address via DHCP. Then, we define a VLAN (vlan10) with an ID of 10 on the enp0s3 interface. The VLAN interface is assigned the IP address 192.168.10.2, with a subnet mask of 24, and a gateway of 192.168.10.1.

network:
  version: 2
  renderer: networkd
  bonds:
    bond0:
      interfaces: [enp0s3, enp0s8]
      addresses: [192.168.1.2/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

In this example, we create a bond interface (bond0) that aggregates enp0s3 and enp0s8. The bond interface is assigned the IP address 192.168.1.2, with a subnet mask of 24, and a gateway of 192.168.1.1. DNS servers are set to Google’s DNS servers.

Example 5: Multiple Interfaces and Routes

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
    enp0s8:
      dhcp4: no
      addresses: [192.168.1.2/24]
      gateway4: 192.168.1.1
  routes:
    - to: 192.168.2.0/24
      via: 192.168.1.254

In this example, we configure two interfaces (enp0s3 and enp0s8). enp0s3 uses DHCP for IPv4, while enp0s8 has a static IPv4 address (192.168.1.2). We also define a route to the 192.168.2.0/24 network via the gateway 192.168.1.254.

Applying Configuration

After creating your YAML configuration, you can apply it by saving the file (e.g., my_netplan.yaml) and using the command:

sudo netplan apply

This will apply the changes you’ve defined in the configuration file.

Conclusion

Netplan provides a straightforward and flexible way to configure network interfaces on Linux systems. By using YAML syntax, you can easily define various network setups, from static addresses to complex setups like bonding and VLANs. With these examples, you’ll be well-equipped to configure your network connections efficiently.