Open Source

How to prevent Ubuntu from overwriting /etc/resolv.conf

If you've been trying in vain to get static DNS to stick in Ubuntu, Jack Wallen shows you how this can easily be done from within a single configuration file.

linuxnethero.jpg
Image: Jack Wallen

If you're like me, you prefer to use third-party DNS servers on your machines (such as Google or OpenDNS). To do that, you set up your network connections, via the network manager app, to use a static IP address (complete with gateway, netmask, and two DNS servers). Once you set that up, you check the /etc/resolv.conf file, only to find that it has contains only one entry:

nameserver 127.0.0.1

Although it works, you might find that DNS isn't resolving as quickly as you'd like or that you don't have the added controls that come with, say, an OpenDNS account.

So how do you prevent Ubuntu from overwriting that resolv.conf entry? Let me show you.

Go static

The first thing you must do is set up your network connection as static. This can be done in the Edit Connections gui. Click on the network icon in the system tray and then click Edit connections. In the Network Connections window, select the connection to be edited and then click the Edit button. In the resulting window, click on either IPv4 or IPv6 (whichever your network uses), select Manual, and fill out the necessary information (Figure A).

Figure A

Figure A

Setting a static IP address in Ubuntu.

Once you've filled out the necessary information, click Save. You've now configured your static IP address. There is one final step you must take.

dnsmasq

The dnsmasq system provides network infrastructure for small networks (such as DNS, DHCP, router advertisement and network boot). This is the tool that is responsible for overwriting your resolv.conf file. In order to prevent this from happening, you need to open a terminal window and issue the command sudo nano /etc/NetworkManager/NetworkManager.conf. In that file, look for the line:

dns=dnsmasq

Comment out that line so it now reads:

#dns=dnsmasq

Save and close the file.

Finally, you must restart the NetworkManager with the command:

sudo /etc/init.d/network-manager restart

If you check your /etc/resolv.conf file, you should see that it now retains the static DNS servers you set in the network connection editor (Figure B).

Figure B

Figure B

Your configured DNS servers now remain.

You can restart networking or reboot your machine and the static DNS addresses will finally stick.

Going forward

As someone who almost always sets static DNS addresses on Linux machines, I feel it's time the connection editor receives a clickable option to enable/disable dnsmasq. Instead of tucking away the option in a configuration file (one that is less-than-obvious), why not make this simple? When a user opts to set a static DNS address in the GUI, it should be assumed that static address will be honored, not overwritten.

See also

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