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
root@toto:~# cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0
When you perform a ping to an IP or domain name, you will encounter a notification as shown below.
root@toto:~# ping dohoang.com
ping: dohoang.com: Temporary failure in name resolution
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.
apt -y install resolvconf
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.
service resolvconf restart
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.
root@toto:~# cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.53
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
.
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: yes
So the whole config would be:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: no
addresses: [192.168.122.10/24]
# default gateway
routes:
- to: default
via: 192.168.122.1
nameservers:
# name server this host refers
addresses: [8.8.8.8,8.8.4.4]
dhcp6: no
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.
sudo apt install yamllint
sudo dnf install yamllint
sudo pacman -S yamllint
For example, I introduced a small formatting error:
- to: default
via: 192.168.122.1
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!
netplan generate
netplan --debug apply
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.
root@toto:~# 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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:21:89:19 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.10/24 brd 192.168.122.255 scope global enp1s0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe21:8919/64 scope link
valid_lft forever preferred_lft forever