How to configure networking with Netplan

In this article I will give examples of network configuration using Netplan.
I also note that for example, starting with Ubuntu version 18.04, Netplan is used by default.

The configuration files are located in the /etc/netplan/ directory.
Usually, after installing the system, you can see the /etc/netplan/config.yaml file or /etc/netplan/50-cloud-init.yaml.

Inside each block, two spaces must be indented; tabs cannot be used!

For example, to configure a DHCP client on the enp3s0 network interface:

network:
    ethernets:
        enp0s3:
            dhcp4: true
    version: 2

I will give an example of setting a static IP address:

network:
    ethernets:
        enp0s3:
             addresses:
              - 10.10.10.2/24
             gateway4: 10.10.10.1
             nameservers:
               addresses: [10.10.10.1, 1.1.1.1]
    version: 2

Example of configuring multiple network interfaces:

network:
    ethernets:
        enp0s3:
            dhcp4: true
        enp0s8:
             addresses:
              - 10.10.10.1/24
    version: 2

You can specify multiple IP addresses on one network interface:

network:
    ethernets:
        enp0s3:
            dhcp4: true
        enp0s8:
             addresses:
              - 10.10.10.1/24
              - 10.10.20.1/24
              gateway4: 10.22.1.1
    version: 2

Example of specifying multiple IP addresses and multiple gateways:

network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 9.0.0.9/24
- 10.0.0.10/24
- 11.0.0.11/24
routes:
- to: 0.0.0.0/0
via: 9.0.0.1
metric: 100
- to: 0.0.0.0/0
via: 10.0.0.1
metric: 100
- to: 0.0.0.0/0
via: 11.0.0.1
metric: 100

Example of setting a Wi-Fi interface:

network:
version: 2
renderer: networkd
wifis:
wlp2s0b1:
dhcp4: no
dhcp6: no
addresses: [192.168.0.21/24]
gateway4: 192.168.0.1
nameservers:
addresses: [192.168.0.1, 8.8.8.8]
access-points:
"network_ssid_name":
password: "**********"

VLAN configuration example:

network:
    version: 2
    ethernets:
        ens3:
            addresses: 
                - 192.168.122.201/24
            gateway4: 192.168.122.1
            nameservers:
                addresses: [192.168.122.1]
        ens8: {}

    vlans:
        vlan101:
            id: 101
            link: ens8
            addresses: [192.168.101.1/24]
        vlan102:
            id: 102
            link: ens8
            addresses: [192.168.102.1/24]

Link aggregation and vlan configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0f0:
      dhcp4: no
    ensp3s0f1:
      dhcp4: no
  bonds:
    bond0:
      dhcp4: no
      interfaces: [enp3s0f0, enp3s0f1]
      parameters: 
        mode: 802.3ad
        mii-monitor-interval: 1
  vlan10:
      id: 10
      link: bond0
      dhcp4: no
      addresses: [10.10.10.2/24]
      routes:
        - to: 10.10.10.2/24
          via: 10.10.10.1
          on-link: true
  vlan20:
    id: 20
    link: bond0
    dhcp4: no
    addresses: [10.10.11.2/24]
    gateway: 10.10.11.1
    nameserver:
      addresses: [8.8.8.8]

To test the new configuration, execute:

sudo netplan try

And for confirmation, press the “ENTER” key, if the connection is lost and not press “ENTER”, then after 120 seconds the previous configuration will be restored.

You can immediately apply the configuration without testing:

sudo netplan apply

In case of errors, see the debugging information:

netplan --debug generate

See also my articles:
Adding Routes
VLAN Generation Script for Netplan
Configuring the Network in Linux

Leave a comment

Leave a Reply