I like the OpenDns service, and want to use their DNS servers as the default ones in my ubuntu.
But I got some hard-time figuring out how to do it – so now that I found a mature way of doing I will share my failed attempts and the solution found to make it easy for others.
This will work at least for Ubuntu, Kubuntu, Xubuntu and any other debian based distros which use NetworkManager (and consequently dhclient)
The failed attempts to solve it:
First I found that linux will read the DNS servers from the file /etc/resolv.conf
Reading the man page of it (see “man resolv.conf”) I tried to change the file to add the OpenDnss IPs into it – it was as easy as inserting at the top of /etc/resolv.conf the lines:
and it should be done – DNSs configured for the entire system and for all programs! But no, it was not that easy… it only worked temporarily – after a while or after reboot the file resolv.conf was again without the OpenDnss servers I’ve added, and had to manually insert the OpenDnss again… and that is no solution.
What was happening behind the scene? Well, (K/L/X)ubuntus, they all use by default a program named NetworkManager to manage all the systems interfaces that connect to internet.
In turn, the NetworkManager will then use another program named dhclient to get the IP address and DNS servers from a DHCP server, everytime an interface is establishing a connection (when an interface is being put “up”).
And dhclient will itself overwritte the file /etc/resolv.conf with the DNS servers it gets from the DHCP, everytime it gets a DHCP lease for any interface.
So everytime an interface connects (for example, when the wireless interface changes ESSID), the file resolv.conf got overwritten with the DHCP DNSs and so the OpenDns servers manually inserted would not persist.
The good attempt that solved it:
Now dhclient, uses it’s own configuration file (/etc/dhcp3/dhclient.conf , see also “man dhclient.conf”) where it is possible to tell it to always prepend the OpenDnss before the DHCP DNSs when it overwrittes the file resolv.conf.
This way the OpenDns servers will always be included and get first priority over the DHCP DNSs (which also get included, but with a secondary priority)
And this was the solution that made the OpenDnss always present – it was necessary to change only /etc/dhcp3/dhclient.conf
The changes needed in /etc/dhcp3/dhclient.conf were to add these lines at the bottom of the file (but continue reading to use an automated script presented below!):
#:) Prepend the OpenDns servers into /etc/resolv.conf
prepend domain-name-servers 188.8.131.52, 184.108.40.206;
I’ve made a very basic script (you can see it in gist) to automate the adding of the OpenDns server in /etc/dhcp3/dhclient.conf , so that one can simply run the following in a console:
And from then on, the OpenDns server will be used permanently (surviving all the reboots and reconnects! )
This will work for all standard K/X/L ubuntus because they use NetworkManager by default.
If you use Wicd instead of NetworkManager, then this solution will not solve it – because Wicd runs dhclient using a different dhclient.conf file that is not the /etc/dhcp3/dhclient.conf file.
When I get clear about Wicd , I will post here how to do it. UPD 20120528: just stumbled upon the wicd different dhclient.conf file – it’s /etc/wicd/dhclient.conf.template (get more info about it with “man wicd”)