Netplan Cheatsheet
Table of Contents
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:
- Ubuntu Blog - Ubuntu Bionic: Netplan
- websiteforstudents.com - Configuring Static IP Addresses On Ubuntu 17.10 Servers
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
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
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
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
- Ubuntu.com - Ubuntu Bionic: Netplan
- Geek Rewind - How to Configure Static IP Address on Ubuntu Linux
- TechRepublic - How to create a bridge network on Linux with Netplan
- Ask Ubuntu - Can netplan configured nameservers supersede (not merge with) the DHCP nameservers?
- Fabian Lee - KVM: Creating a bridged network with NetPlan on Ubuntu 22.04
- Fabian Lee - KVM: Creating a bridged network with NetPlan on Ubuntu 18.04 bionic
- Hetzner Network configuration Debian / Ubuntu
- Creating an Ubuntu 20.04 KVM Networked Bridge Interface
- Unix & Linux - netplan generate: gateway4 has been deprecated, use default routes instead
- netplan.readthedocs.io - YAML configuration
First published: 18th February 2023