Change DNS Servers
The syntax is as follows for the /etc/resolv.conf. Use the cat/bat or more/less command to view the file:
Outputs:
You can add maximum three DNS name servers.
Change your DNS servers
Login as the root, enter:
Modify or enter nameserver as follows:
Save and close the file. The 1.1.1.1 (IPv4) and 2606:4700:4700::1111 (IPv6) are Cloudflare DNS and 8.8.8.8 is Google DNS IP address. Of course you can add your ISP’s or your corporate DNS IPv4/IPv6 servers too. To test DNS configuration type any one of the following dig command or host command:
Sample outputs:
Suppose you see valid output such as an actual IP address or can ping to a remote server via hostname using the ping command, which means that the DNS is working for you. Also, make sure you have valid default gateway setup if you see the timeout error.
Modern Linux distros
Most modern Linux distros use systemd and NetworkManager. For example, Linux distros such as Red Hat Enterprise Linux (RHEL), Debian, Ubuntu, Fedora, Arch, and many other distros use the nmcli command to configure networking, including the DNS. NetworkManager on these distros dynamically updates the /etc/resolv.conf file with the DNS nameservers settings. In other words, if you set DNS using the above method, they will vanish after reboot. Hence, you need to use the nmcli command.
Step 1: Get device list
Try:
Filter output using the grep/egrep command #
The following outputs indicate that Ethernet device named “enp0s31f6” connected using “Wired connection 1” profile.
Step 2: Setting up IPv4 or IPv6 DNS
Set the IPv4 and IPv6 DNS server addresses for “Wired connection 1” profile as follows:
Want to set up multiple IPV4 and IPV6 dns address in a single go? Try:
Step 3: Activate DNS changes
After that up the device again to reload changes:
Step 4: Verification
Use the host command or dig command utility to verify that name resolution works. For instance:
Again, use the ping command:
You can use the resolvectl command as follows to view status. For example:
And that is how you set DNS on Linux.
Summing up
BSD family of the operating system will use the /etc/resolv.conf including non-systemd Linux distros such as Alpine Linux. However, a systemd-based Linux distro comes with NetworkManager, and you need to use the nmcli command to set up the correct IPv4 or IPv6. For more info read the documentation using the man command or --help option command as follows:
Bibliography: https://www.cyberciti.biz/faq/howto-linux-bsd-unix-set-dns-nameserver/