Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Netplan Cheatsheet

Table of Contents

  1. About
  2. Config Files
    1. Important - Full YAML Extension
  3. Commands
    1. Try - Testing Configurations Before Applying
    2. Apply - Apply Changes Immediately
  4. Example Configurations
    1. Basic DHCP
    2. DHCP With Manually Set DNS Nameservers
    3. DHCP On vLAN
    4. Static IP
    5. Basic KVM Bridge
    6. Specify MAC - Multiple Interfaces
  5. Appendix
    1. PHP Code To Generate Static IP YAML File
  6. References

About

Netplan is the new networking system that is taking over from editing the /etc/networking/interfaces file.

Netplan is actually pretty powerful and you can split your configuration up across multiple files in multiple locations as well as configuring bridges/bonding etc. I recommend reading these two posts for more information:

Config Files

Netplan configuration files can existing in three different locations with precidence from most important to least:

  • /run/netplan/*.yaml
  • /etc/netplan/*.yaml
  • /lib/netplan/*.yaml

Generally, you will want to find and edit the file(s) within /etc/netplan.

Important - Full YAML Extension

It is vitally important that you save files with the full .yaml extension rather than .yml. I usually save files as .yml, but if you do this, netplan will not "see" and use them and you will be asking yourself what is going wrong.

Commands

Try - Testing Configurations Before Applying

The following command will check the changes before applying them and waiting for 2 minutes. During this time, one can press enter/return on the keyboard to keep the changes. If one does not do this then the changes are reverted. This is particularly useful in case you mess up your network, and can't send any SSH commands to undo what you have just done.

sudo netplan try

Apply - Apply Changes Immediately

Once you have successfully edited your configuration file, you can apply it with:

sudo netplan apply

Example Configurations

Basic DHCP

The following configuration configures the ens2 interface to use DHCP.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens2:
      dhcp4: yes

DHCP With Manually Set DNS Nameservers

If you want to use DHCP for setting your IP address, but you prefer to manually set your DNS nameserver, then you would do something like below:

network:
  version: 2
  renderer: networkd
  ethernets:
    ens18:
      dhcp4: true
      dhcp4-overrides:
        use-dns: no
      nameservers:
        addresses:
          - 192.168.0.222

The above example assumes your nameserver is at 192.168.0.222.

DHCP On vLAN

Below is a basic example of DHCP on interface enp2s0f0 with a vLAN ID of 3.

# Manual settings using vlan on DHCP
network:
  version: 2
  renderer: networkd
  ethernets: 
    enp2s0f0:
      dhcp4: yes
  vlans:
    vlan3:
      id: 3
      link: enp2s0f0

You may find the Ubuntu - Connect To VLAN tutorial useful.

Static IP

Edit your netplan configuration file. E.g.

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

Here is an example configuration to set a static IP of 192.168.1.100 with your router/gateway being at 192.168.1.1 and wanting to use OpenDNS dns servers:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens2:
        addresses:
            - 192.168.1.100/24
        gateway4: 192.168.1.1
        nameservers:
            addresses:
                - 208.67.222.222
                - 208.67.220.220

The CIDR needs to reflect your network's subnet's netmask (useful lookup table). It is not the CIDR for IPs you allocate to the server (e.g. don't use /32) See the appendix if you want the PHP code for generating such a config.

Gateway Deprecated

If you use the above on Ubuntu 22.04, then you will get the following error message:

** (process:3609): WARNING **: 15:40:39.567: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

To resolve this you would want the following instead:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens2:
        addresses:
            - 192.168.1.100/24
        routes:
          - to: default
            via: 192.168.1.1
        nameservers:
            addresses:
                - 208.67.222.222
                - 208.67.220.220

Basic KVM Bridge

Below is a bridge setup you might use if setting up KVM on Ubuntu. This way, your virtual machine guests connect on the bridge.

network:
  version: 2
  renderer: networkd

  ethernets:
    enp1s0:
      dhcp4: false 
      dhcp6: false 

  bridges:
    br0:
      interfaces: 
        - enp1s0
      addresses: 
        - 192.168.1.239/24
      gateway4: 192.168.2.1
      mtu: 1500
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
      parameters:
        stp: true
        forward-delay: 4
      dhcp4: no
      dhcp6: no

Specify MAC - Multiple Interfaces

If you have multiple interfaces on your machine, then you may wish to use match against the MAC address to ensure the interface configuration is set against the relevant physical interface. E.g.

network:
    version: 2
    ethernets:

        # public internet NIC
        enp1s0:
            match:
                macaddress: 52:54:00:B7:7e:ff
            addresses:
                - 123.456.789.0/27
            gateway4: 85.10.198.65
            nameservers:
                addresses:
                    - 8.8.8.8
                    - 8.8.4.4

        # Private internal network
        enp7s0:
            match:
                macaddress: 52:54:00:a7:6e:ee
            addresses:
                - 192.168.0.1/16

Appendix

PHP Code To Generate Static IP YAML File

<?php

$interfaceName = "enp1s0";
$staticIp = "192.168.0.3";
$cidr = 16;
$gateway = "192.168.0.1";
$nameserverIps = array("8.8.8.8", "8.8.4.4");

$networkConfig = array(
    'network' => array(
        "version" => "2",
        "renderer" => "networkd",
        "ethernets" => array(
            $interfaceName => array(
                "addresses" => array(
                    "{$staticIp}/{$cidr}",
                ),
                "gateway4" => "$gateway",
                "nameservers" => array(
                    "addresses" => $nameserverIps,
                ),
            ),
        ),
    ),
);

print yaml_emit($networkConfig) . PHP_EOL;

References

Last updated: 1st June 2024
First published: 18th February 2023