Configure Ubuntu Networking with Netplan
Tags: ubuntu, linux, networking, netplan
Published: 2020-09-03 21:15:36

# Why configure a static IP address

👉 By default, *Ubuntu server boots with DHCPv4 enabled*. 
👉 While dynamic IP addressing is convenient, it is more aligned with clients, that joins and leave networks, rather than servers, which tend to remain fixed on a network. 
👉 If you're building anything beyond the most basic deployment, it is wise to plan out your server infrastructure. 
👉 That means using **static IP addressing** for your servers, which requires a little knowledge about how to configure netplan.

# Enter netplan
Ubuntu uses netplan to control its networking configuration. It is a powerful tool, consolidating the regular networking configuration points, such as interface addressing and DNS servers, with more advanced features, such as tunneling, interface bonding and security.

# Yet more YAML
We configure netplan by writing a YAML file according to a fixed structure, then applying said file.

Go to the [netplan examples site](https://netplan.io/examples/) to get an idea of the required structure.

# Netplan is annoying. Netplan examples? Also annoying!
Netplan itself is pretty annoying. Not only are there *no default examples in the base file*, the structure and syntax need to be exact. Cue some trial and error to get the yaml to work.
In addition to this, the examples given on the examples page are seemingly a random mix of features. There's a definite bias towards IPv4 on that page as well, IPv6 configuration is not worthy of its own snippet and has to just co-star in some other feature's example.

# How to configure netplan

## A netplan workflow

1. As new install of ubuntu server 18.04 & 20.04 will have a default netplan yaml file stored in `/etc/netplan`.
    ```bash
      joe@ubserv2:~$ ls /etc/netplan
      00-installer-config.yaml
    ```

2. I usually copy this as a starting point.

    ```bash
    sudo cp 00-installer-config.yaml netplan1.yaml
    ```

3. Configure the new yaml file as required. See below for an example.

4. Netplan can verify the syntax of the new file before applying it with the `netplan generate <your-file-name>` command.

5. Once you are ready to go, activate your new networking configuration with `netplan apply <your-file-name>`

6. If you are using an SSH connection into the device using the previously configured IP address, your connection will be cut at this point.
To avoid being locked out from your server, use the <br> `netplan try <your-file-name>` command. This will apply your selected netplan yaml file but will rollback to the previous configuration if you do not confirm the change within 120 seconds.

## A netplan example

Here's an example that will:

1. Configure static IPv4 and IPv6 on an ethernet interface (eth1).
2. Configure default gateways for v4 and v6.
3. Configure v4 and v6 nameservers.
4. Set a DNS search-list of "mynet.home".


<pre class="highlight shell"><code>network:
  ethernets:
    eth1:
      addresses:
        - 192.168.20.99/24
        - "2001:db8:20::99/64"
      gateway4: 192.168.20.1
      gateway6: "2001:db8:20::99"
      nameservers:
        search: [mynet.home]
        addresses: [8.8.8.8, "2001:4860:4860::8888"]
  version: 2
</code></pre>