netplan

Updated: September 28, 2024

Netplan started in Ubuntu 17.10. It is what is now used to manage and configure networking on a Ubuntu system.

It is thought to be easier because it just uses yaml config files to abstract network interface configurations. There is are two primary rendereres: Server: networkd Desktop: NetworkManager


Table of Contents


tldr

sudo netplan
  generate      # generate config
  apply         # apply generated config
  try           # apply config, wait for confirm, if no, rollback
  get           # merge and display all condig on system
  set           # add new setting for dotted key=value pair
                # etc (ethernets.eth0.dhcp4=true)
  info          # show feature flags of installed version as YAML
  ip            # retrieve IP info from the system
  help          # show help messages

INTERFACES

good to run this first two to eliminate old setting before setting up netplan.

# flush ip addresses
ip addr flush dev eth0

# bring interfaces up or down
ip link set eth0 [up|down]

CONFIGS

The base config is in /etc/netplan/

# 50-cloud-init.yaml (default)
network:
  version: 2
  ethernets:
    enp0s3:
      addresses: []
      dhcp4: yes

set a static IP

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

a simple bridge

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

  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - enp3s0

use multiple addresses on a single interface

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [ 10.0.18.23/24, 10.0.18.24/24  ]
      routes:
       - to: default
         via: 10.0.18.1

reaching a directly connected gateway

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [ 10.0.18.1/24 ]
      routes:
       - to: 0.0.0.0/0
         via: 8.8.8.8
         on-link: true

routing on 2 different networks

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
       - 10.0.18.10/24
      routes:
       - to: 10.0.18.0/24
         via: 10.0.18.1
         table: 101
      routing-policy:
       - from: 10.0.18.0/24
         table: 101
    ens5:
      dhcp4: no
      addresses:
       - 192.168.1.178/24
      routes:
       - to: default
         via: 192.168.1.1
       - to: 192.168.1.0/24
         via: 192.168.1.1
         table: 102
      routing-policy:
       - from: 192.168.1.0/24
         table: 102

attach VLANs to network interfaces

network:
  version: 2
  renderer: networkd
  ethernets:
    mainif:
      match:
        macaddress: "de:ad:be:ef:ca:fe"
      set-name: mainif
      addresses: [ 10.3.0.5/23 ]
      nameservers:
        addresses: [ 8.8.8.8, 8.8.4.4 ]
        search: [ example.com ]
      routes:
     - to: default
       via: 10.3.0.1
  vlans:
    vlan18:
      id: 18
      link: mainif
      addresses: [ 10.3.98.5/24 ]
      nameservers:
        addresses: [ 127.0.0.1 ]
        search: [ domain1.example.com, domain2.example.com ]

Tutuorial to make a bridge

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no

  bridges:
    br0:
      dhcp4: no
      interfaces: [ eth0 ]
      addresses: [10.0.18.10/24]
      gateway4: 10.0.18.1
      mtu: 1500
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4 ]
      parameters:
        stp: true     # used to detect and remove loops (spanning tree protocol)
        forward-delay: 4