6

What is the best way to edit the resolv.conf file?

I wasn't familiar with setting proxies until a task to configure a server came along. I manually edited my /etc/resolv.conf file and included the following input:

nameserver 192.168.115.147,192.178.116.168

Any attempt to download was met with errors. After much googling, I came across a post which led me to reformat the file - directly as follows:

nameserver 192.168.115.147
nameserver 192.178.116.168

That worked and file downloads were no longer interrupted with errors.

However, when I had to reboot the server the file reverted back to the single line format which again stopped my downloads. That's when I noticed the DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN at the top of resolv.conf.

So, how should resolv.conf be edited so the changes persist through server reboots?

  • 1
    This will depend on the specific vendor, and how their scripts mangle the network configuration. Without knowing the vendor, I'd fgrep -r 192.168.115.147,192.178.116.168 /etc to try to find where that specific comma-joined string came from, or look for the vendor's network configuration files (/etc/network/*, /etc/sysconfig/network*, etc.) to see if DHCP is set, etc. – thrig Sep 18 '15 at 15:56
4

You use entries in /etc/resolvconf/resolv.conf.d/ named head, base or tail (depending on where in resolv.conf you want your entries to come). From man resolvconf:

The dynamically generated resolver  configuration  file always  starts
with  the  contents of /etc/resolvconf/resolv.conf.d/head and ends with
the contents of /etc/resolvconf/resolv.conf.d/tail.  Between  head  and
tail  the  libc script inserts dynamic nameserver information compiled
from, first, information provided for  configured  interfaces;  second,
static  information  from /etc/resolvconf/resolv.conf.d/base.

Just put your entries in base and run resolvconf -u.

4

Short Answer

I think the most reliable way to do that is by setting your resolv.conf to be immutable. Once you set it the way you want, you simply run

sudo chattr +i /etc/resolv.conf

To edit it again, you need to remove the immutable flag:

sudo chattr -i /etc/resolv.conf

Alternate Answer

The application that puts that message in /etc/resolv.conf is called resolvconf. You can remove your resolvconf package with (I'm assuming debian or ubuntu here, but I think resolvconf is a debian thing):

sudo apt-get remove resolvconf

I remove it on my computers because I have found that I am more likely to be able to connect with proper /etc/resolv.conf settings in strange situations (hotel or airport wifi, specifically) when I do not have the resolvconf package installed. resolvconf also does other things like DNS caching, and probably some other things I feel I can live without. You might look into what all it does before you remove it.

Important notes

Removing resolvconf will not guarantee that your /etc/resolv.conf remains unchanged. Other programs, like dhclient will make edits to /etc/resolv.conf. If you run a network manager, that might also edit /etc/resolv.conf. I don't have much experience with network managers.

The chattr solution should prevent those other programs from making edits, and is probably right for your server situation. If you were using it on a laptop, situations may arise where important changes to your /etc/resolv.conf would be blocked. An example would be when accessing hotel wifi, where a modification to your /etc/resolv.conf is needed for access to a certain locally hosted url before gain access to the proper internet). If you can remember in those situations to chattr -i /etc/resolv.conf and reconnect to the wifi, then it shouldn't be a problem.

0

Unless you are using the network manager that comes with your window server, the simple and proper method to set a persistent DNS nameserver is in your networking config file:

For Debian (Ubuntu, Mint, Arch) edit /etc/network/interfaces :

auto eth0
iface eth0 inet dhcp

dns_nameserver 8.8.8.8

I believe in Red Hat, Fedora, CentOS, etc. changes to resolve.conf are persistent after reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.