Open Source

How to take back control of resolv.conf

If you're tired of your Linux system's resolv.conf file being overwritten, Jack Wallen has the solution for you.

Resolv

Long ago, you could setup a Linux box and edit the /etc/resolv.conf file knowing the changes would stick. That made it incredibly simple to manage what DNS servers would be used by the machine. Fast-forward to now, and a manual edit of that same file will only be overwritten anytime you restart networking or reboot the machine.

Fortunately, this is Linux, so taking back control of the resolv.conf file isn't much of a challenge... when you know what to look for. Let me serve as your guide in this quest. With a little work at the command line, you'll be able to dictate exactly what goes into that resolv.conf file without issue.

Let's take care of this.

Disable overwriting of resolv.conf

Modern Linux distributions use a system called dnsmasq to write the DNS address into resolv.conf. No matter how many times you write that file, if you don't disable dnsmasq, it'll always be overwritten with this familiar line:

nameserver 127.0.1.1

To avoid this, you must open up the file /etc/NetworkManager/NetworkManager.conf and comment out the line:

dns=dnsmasq

So the new line should look like:

# dns=dnsmasq

Now, kill the running dnsmasq with the command:

sudo killall -9 dnsmasq

At this point, you can hand edit that resolv.conf file all you want, knowing a reboot will not overwrite your changes.

Manually entering network information

If you want to take this one step farther, open up your /etc/network/interfaces file and enter the required information for your networking device. Let's create an entry for eth0 that will set it with a static IP address and OpenDNS DNS servers. Here's what the entry will look like:

iface eth0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.0 dns-search attlocal.net dns-nameservers 208.67.220.220, 208.67.222.222

Note: You'll want to set your dns-search to match your provider. The name of your network interface (in the example above, eth0) will depend on your setup.

If you're using a desktop distribution, you might be better served working with the graphical network configuration tool (usually resides in the panel on the desktop), instead of manually entering the interface information.

Now, restart networking and the changes should stick. If you're using Ubuntu, the command to restart networking is sudo /etc/init.d/networking restart. If you don't want to restart networking, you can tell resolv.conf to regenerate (to test the changes) with the command:

sudo resolvconf -u

You now have regained control of your /etc/resolv.conf file. It will no longer be overwritten when your network or system restarts.

Do you prefer to take manual control over your systems, or do you trust the platform to take care of business for you? Let us know in the discussion thread below.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox