Netplan Examples for Ubuntu 18.04

Below are a collection of example netplan configurations for common scenarios. If you see a scenario missing or have one to contribute, please file a bug against this documentation with the example using the links at the bottom of this page. Thank you!

Configuration

To configure netplan, save configuration files under /etc/netplan/ with a .yaml extension (e.g. /etc/netplan/config.yaml), then run sudo netplan apply. This command parses and applies the configuration to the system. Configuration written to disk under /etc/netplan/ will persist between reboots.

DHCP and static addressing

To let the interface named ‘enp3s0’ get an address via DHCP, create a YAML file with the following:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true

To instead set a static IP address, use the addresses key, which takes a list of (IPv4 or IPv6), addresses along with the subnet prefix length (e.g. /24). Gateway and DNS information can be provided as well:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 10.10.10.2/24
      gateway4: 10.10.10.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [10.10.10.1, 1.1.1.1]

Wireless interfaces

Wireless devices use the ‘wifis’ key and share the same configuration options with wired ethernet devices. The wireless access-point name and password should also be specified:

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: "**********"

Multiple addresses on an interface

The addresses key can take a list of addresses to assign to an interface:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
     addresses:
       - 10.100.1.38/24
       - 10.100.1.39/24
     gateway4: 10.100.1.1

Interface aliases (e.g. eth0:0) are not supported.

Multiple addresses with multiple gateways

Similar to the example above, interfaces with multiple addresses can be configured with 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
     #gateway4:    # unset, since we configure routes below
     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

Given that there are multiple addresses, each with their own gateway, we do not specify gateway4 here, and instead configure individual routes to 0.0.0.0/0 (everywhere) using the address of the gateway for the subnet. The metric value should be adjusted so the routing happens as expected.
DHCP can be used to receive one of the IP addresses for the interface. In this case, the default route for that address will be automatically configured with a metric value of 100. As a short-hand for an entry under routes, gateway4 can be set to the gateway address for one of the subnets. In that case, the route for that subnet can be omitted from routes. Its metric will be set to 100.

Network Manager

Netplan supports both networkd and Network Manager as backends. You can specify which network backend should be used to configure particular devices by using the

renderer

key. You can also delegate all configuration of the network to Network Manager itself by specifying only the

renderer

key:

 

Bonding

Bonding is configured by declaring a bond interface with a list of physical interfaces and a bonding mode. Below is an example of an active-backup bond that uses DHCP to obtain an address:

 

Below is an example of a system acting as a router with various bonded interfaces and different types. Note the ‘optional: true’ key declarations that allow booting to occur without waiting for those interfaces to activate fully.

 

Bridging

To create a very simple bridge consisting of a single device that uses DHCP, write:

 

A more complex example, to get libvirtd to use a specific bridge with a tagged vlan, while continuing to provide an untagged interface as well would involve:

 

Then libvirtd would be configured to use this bridge by adding the following content to a new XML file under

/etc/libvirtd/qemu/networks/

. The name of the bridge in the tag as well as in need to match the name of the bridge device configured using netplan:

 

VLANs

To configure multiple VLANs with renamed interfaces:

 

Directly connected gateway

This allows setting up a default route, or any route, using the “on-link” keyword where the gateway is an IP address that is directly connected to the network even if the address does not match the subnet configured on the interface.

 

Source routing

Route tables can be added to particular interfaces to allow routing between two networks:

In the example below, ens3 is on the 192.168.3.0/24 network and ens5 is on the 192.168.5.0/24 network. This enables clients on either network to connect to the other and allow the response to come from the correct interface.

Furthermore, the default route is still assigned to ens5 allowing any other traffic to go through it.

 

Loopback interface

Networkd does not allow creating new loopback devices, but a user can add new addresses to the standard loopback interface, lo, in order to have it considered a valid address on the machine as well as for custom routing:

 

Windows DHCP Server

For networks where DHCP is provided by a Windows Server using the dhcp-identifier key allows for interoperability: