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 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
-
As new install of ubuntu server 18.04 & 20.04 will have a default netplan yaml file stored in
/etc/netplan
.
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.
A netplan example
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: [8.8.8.8, "2001:4860:4860::8888"]
version: 2
Top comments (3)
There is a reason people don't switch to linux distros. Although more than willing to do so.I have been working for two nights now trying to configure my network interfaces on Ubuntu server.
So I google how to configure Netplan and get here.
I get as far as Paragraph 2" I usually copy this as a starting point"
and then what? You copy the file and then???? where do you put the copy?
do you create a new folder/directory/config file?
Linux is so easy, we're always told. That may be true, but most of us speak windows and while willing and sometimes desperate to switch, looking for help online is an exercise in frustration..
Imagine you're buying a new piece of furniture from IKEA. sight-unseen, and your assembly instructions have no graphics and are only written in Italian
That's how this feels.
And don't even get me started on "FORUMS"
You post a question and the first reply is usually someone with a wagging finger informing you that you posted your question in the wrong place.
Then eventually you get an answer from someone well meaning .
Then you get someone else disagreeing with the first answer and then you get a whole group of people chiming in and arguing whilst ignoring you
And being defeated once again, you go back to your much hated windows machine
Cheers
The copied file is just a backup, which is good practice when editing configuration files in Linux (and any other system).
There are numerous naming conventions when backing up files. I usually use the suffix .bak, like this: "sudo cp myfile.txt myfile.txt.bak"
And regarding your frustrations with the Linux world, I'll probably speak for everyone when I say: we've all been there! If I were you, I would uninstall windows completely and force yourself into Linux - suddenly you will find that the learning curve flattens out, and will feel relieved to have gotten rid of Windows' tick box nightmare :)
Highly appreciate your steps you've shared. .
I am stuck on VLAN's. IP addresses I have managed and I am able ssh to my server laptop to server direct cable.
I can't reach it on it's respective ip address when I connect it to the switch.
An explanation on VLAN configuration will help too.
Thanks
Some comments have been hidden by the post's author - find out more