Ever since Ubuntu, my Linux distro of choice, switched from ifconfig and /etc/network/interfaces to start using Netplan I have a mental block about how to configure a static IP. Hence this post.

Netplan supports two renderers, NetworkManager and Systemd-networkd. For my Ubuntu servers I use networkd.

Netplan configuration files are written in yaml and have a .yaml extension. A pretty straight forward file structure. There are 3 potential locations that these files are read from in this order:

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

I store mine on Ubuntu in the /etc/netplan directory. Files are loaded in alphabetical order – with later files appending keys to the previous file. So for the static IP I tend to name the file 01-ifcfg.yaml so it’s loaded early in the process.

Configuring a Static IP Address

The first thing we’re going to need is the interface name. These used to be similar to eth0, but have since switched to using a name based on the PCI slot they are using – now something like ens160. So to find out what you inteface is called you can use the following:

ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:3c:bb:54 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 10.1.2.3/24 brd 10.1.2.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe3c:bb54/64 scope link
       valid_lft forever preferred_lft forever

So in this case, I only have 1 network adapter. It’s called ens160. Next we need a few details about the network:

  • Network subnet mask (typically I use a /24 for my lab)
  • The static IP would want to assign to this machine (10.10.10.3 in my case)
  • Gateway IP Address (typically this will be the .1 address. 10.10.10.1 in my case)
  • Any DNS servers you want to configure
    • For my lab, I have the router (10.10.10.1) which can connect to the outside world if necessary, so I have the DNS set up to forward DNS along
    • Inside the lab, I have a DNS server running on 10.10.10.20 so these are the two address I’ll use below

Armed with this knowledge, we can now create the configuration file.

sudo nano /etc/netplan/01-ifcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      dhcp4: no
      addresses:
        - 10.10.10.3/24
      gateway4: 10.10.10.1
      nameservers:
        addresses: [10.10.10.20,10.10.10.1]

Before you apply the changes, let’s test them to make sure we didn’t make a mistake creating the config file.

sudo netplan try

Netplan will validate the configuration file. If there are no issues, netplan will attempt to apply the new settings. If the configuration fails, netplan will revert the settings after the timeout period (120 seconds I think). Otherwise the new settings will be kept.

If you like to live dangerously, you can skip the try command and jump right into:

sudo netplan apply

This will just apply the settings in your new config file.

DHCP Configuration

If for some odd reason, you need to configure DHCP, you can use this as a base template. Just edit as needed.

sudo nano /etc/netplan/01-ifcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      dhcp4: true
      addresses: []
      optional: true