👉 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.
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.
We configure netplan by writing a YAML file according to a fixed structure, then applying said file.
Go to the netplan examples site to get an idea of the required structure.
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.
As new install of ubuntu server 18.04 & 20.04 will have a default netplan yaml file stored in
joe@ubserv2:~$ ls /etc/netplan 00-installer-config.yaml
I usually copy this as a starting point.
sudo cp 00-installer-config.yaml netplan1.yaml
Configure the new yaml file as required. See below for an example.
Netplan can verify the syntax of the new file before applying it with the
netplan generate <your-file-name>command.
Once you are ready to go, activate your new networking configuration with
netplan apply <your-file-name>
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
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.
Here's an example that will:
- Configure static IPv4 and IPv6 on an ethernet interface (eth1).
- Configure default gateways for v4 and v6.
- Configure v4 and v6 nameservers.
- Set a DNS search-list of "mynet.home".
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: [188.8.131.52, "2001:4860:4860::8888"] version: 2