Setting a Static IP and Permanent DNS nameserver in Ubuntu Server

DNS Problem?
For DNS nameserver, You may encounter an issue where the dns-nameserver configuration is changed to the address 127.0.0.53
when rebooting Ubuntu Server. This will result in the inability to resolve domain names (although you can still ping or connect to IP addresses on the Internet).
After rebooting, the file will be changed back to the content nameserver 127.0.0.53
(near the end line), even though you had previously changed it to 8.8.8.8
When you perform a ping to an IP or domain name, you will encounter a notification as shown below.
The simple reason is that this is a symbolic link (soft link), so making changes here has no effect. By checking the command ls -alh /etc/resolv.conf
, you will see the file of the link.
Solution?
To address this issue, please install the resolvconf
package to handle this problem. Before installing, please modify the line nameserver 127.0.0.53
in the file /etc/resolv.conf
to nameserver 8.8.8.8
in order to access the internet and retrieve the package beforehand. In this guide, the configuration will be performed with root privileges or by using sudo su
to execute the commands.
After the installation is complete, please use the vi/vim editor or any familiar tool to edit the file /etc/resolvconf/resolv.conf.d/head
with the following content.
# Make edits to /etc/resolvconf/resolv.conf.d/head
nameserver 8.8.8.8
nameserver 8.8.4.4
Restart the resolvconf service for the configuration to take effect.
After that, reboot the system and check the result using the following steps:
Verify the content of the /etc/resolv.conf
file by using the cat
command.
Try pinging an external IP and domain to ensure that the internet connection is working fine.
Static IP
From version 18.04 onwards, the default configuration method in Ubuntu will use netplan instead of the traditional file editing method that we used to commonly use. Configuring IP addresses (static or dynamic) for Ubuntu will utilize the file /etc/netplan/01-netcfg.yaml
for processing, and the netplan
command will be used for control.
Solution
Before setting a static IP for Ubuntu Server 20.04, you need to SSH into the server. After that, use either vi/vim or nano depending on your preference to edit the file. In this guide, I will use vim.
To display the content of the file, you can use the command cat /etc/netplan/01-netcfg.yaml
.
So the whole config would be:
In the aforementioned file, please take note of the following parameters:
dhcp4
: Set to “no”.addresses
: The static IP address assigned to the machine, in this case, is 192.168.122.10.routes
: The gateway IP address for the machine is 192.168.122.1.- Under the
nameserver
tag within the “addresses” section, declare the DNS servers. Declare two DNS servers (02 DNS server). dhcp6
: Set to “no”.
One other tip when working on netplan files, yamllint
can save you a lot of trouble.
For example, I introduced a small formatting error:
Then when I run yamllint
, I’ll get a line number that should help track down where the errors are.
28:9 error syntax error: expected <block end>, but found '?' (syntax)
If you have any syntax errors (for example spacing issues) yamllint will give you the line numbers of your problems.
Hopefully this has made your netplan generate and netplan apply go smoothly!
The machine will lose connection temporarily when switching to the new IP. After rebooting the machine, access it using the new IP address. Then, try pinging the gateway or Google to test the connectivity.