How to Set Permanent resolv.conf on Ubuntu
The file /etc/resolv.conf is a file containing the DNS resolver or IP address of the DNS server (nameserver) which functions to translate from domain names to IP addresses.
On Ubuntu 20.04, the default nameserver is 127.0.0.53.
1 2 3 4 5 6 7 | 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 127.0.0.53 |
For example, if you want to use Cloudflare’s (1.1.1.1) and Google’s (8.8.8.8) DNS servers as primary and secondary nameservers, put the IP address in the first and second lines.
1 | sudo nano /etc/resolv.conf |
Update to be like below.
1 2 3 4 5 6 7 8 | # 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 1.1.1.1 nameserver 8.8.8.8 nameserver 127.0.0.53 |
Problem
The problem that occurs is the resolv.conf file will change back to how it was after the computer is restarted, which only contains the IP address 127.0.0.53. In the resolv.conf file there is also a comment DO NOT EDIT THIS FILE BY HAND – YOUR CHANGES WILL BE OVERWRITTEN , which means don’t change this file manually because the configuration will be overwritten again.
Solution
To solve the above problem is to use resolvconf service.
Install the resolvconf package.
1 | sudo apt install resolvconf |
Then activate and run the resolvconf service.
1 2 3 | sudo systemctl enable resolvconf sudo systemctl start resolvconf sudo systemctl status resolvconf |
The results are as below.
1 2 3 4 5 6 | ● resolvconf.service - Nameserver information manager Loaded: loaded (/lib/systemd/system/resolvconf.service; enabled; vendor preset: enabled) Active: active (exited) since Sun 2020-07-26 18:08:17 WIB; 14h ago Docs: man:resolvconf(8) Process: 329 ExecStart=/sbin/resolvconf --enable-updates (code=exited, status=0/SUCCESS) Main PID: 329 (code=exited, status=0/SUCCESS) |
Then enter the nameserver IP address in the head resolvconf configuration file.
1 | sudo nano /etc/resolvconf/resolv.conf.d/head |
Enter the nameservers under the comments.
1 2 | nameserver 1.1.1.1 nameserver 8.8.8.8 |
Then run the resolv.conf update.
1 2 | sudo resolvconf --enable-updates sudo resolvconf -u |
Verify by displaying the contents of the resolv.conf file.
1 2 3 4 5 6 7 8 9 10 | 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 1.1.1.1 nameserver 8.8.8.8 nameserver 127.0.0.53 |
If you want to change or add other nameserver IP addresses, change the head file again, and run the resolvconf update.
1 | sudo resolvconf -u |
If you found this article helpful and would like to support my work, consider making a donation through PayPal. Your support helps me continue creating useful content and tutorials. Thank you!
Donate via PayPal: https://paypal.me/musaamin
Many websites have a long intro to describe what they’re about to show the reader how to do something. Thank you for getting right to the point right away.
Works fine .
Tks