Resolving DNS Resolution Issues in Ubuntu and Ubuntu Derived Distros

2015-09-24T20:00:00Z

Using Ubuntu 12.04/12.10 and later versions/derivatives (including my current desktop distro of choice, Linux Mint), I have had periodic issues with programs resolving the IP addresses of hosts on the local network. These are easily resolved with just a handful of configuration tweaks.

dnsmasq

The first problem I noticed, was an occasional one. If I wanted to SSH into a remote host on my network, occasionally I'd see:

ssh cookie@biscuit.ninja
ssh: Could not resolve hostname biscuit.ninja: Name or service not known

This seems to be a problem with dnsmasq. When I ran:

dig biscuit.ninja

or

nslookup biscuit.ninja

I'd see in the results that the DNS query was resolved by 127.0.0.1. In other words, my desktop running Linux Mint was itself resolving the DNS queries. I could confirm this with:

netstat -anp | grep ":53"

Which returns:

$ sudo netstat -anp | grep ":53"
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1096/dnsmasq
udp        0      0 127.0.1.1:53            0.0.0.0:*                           1096/dnsmasq
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           804/avahi-daemon: r
udp6       0      0 :::5353                 :::*                                804/avahi-daemon: r

This clearly shows dnsmasq is listening on port 53 (both TCP and UDP). dnsmasq is a lightweight DHCP and caching DNS server.

dnsmasq is configured by default to use the dns servers specified in /etc/resolv.conf to try and resolve DNS queries. Why it intermittently fails, I'm not sure. The easiest course of action seems to be disabling it, which can be achieved by editing network manager.conf

 sudo vi /etc/NetworkManager/NetworkManager.conf

Find the following line:

dns=dnsmasq

And comment it out as follows:

#dns=dnsmasq

Save the file and close your editor. The change won't be effective until either NetworkManager or the workstation itself are restarted. You can use the following command to restart NetworkManager:

sudo service network-manager restart

Avahi

Avahi is a network service that allows hosts to publish services and also discover other hosts and services available in the local network.

By default, Ubuntu and it's derivatives are configured to, in effect, use Avahi for DNS query resolution. This can conflict with resolving DNS queries for .local top level domains. I don't personally use .local, but unfortunately our AD domain at work does. So this causes problems when I'm remotely accessing hosts on my company domain via VPN.

My personal choice is to not rely on Avahi (and in turn, mDNS, DNS-SD). If I set-up a service on my local network, I'll create the DNS entries required on my DNS server.

Initially, I'll prevent the avahi-demon from running at start-up with the following command:

sudo echo "manual" > /etc/init/avahi-daemon.override

This simply stops Ubuntu's upstart from running the avahi-deamon. In order to make this change effective straight away, you will need to stop the avahi-deamon with:

sudo service avahi-daemon stop

The next step is editing the Name Service Switch configuration file and remove the reference to mdns4_minimal. I tend to edit mine as follows:

sudo vi /etc/nsswitch.conf

Change:

hosts:          files mdns4_minimal dns [NOTFOUND=return]

To:

hosts:          dns files [NOTFOUND=return] 

That pretty much concludes the changes I make to get Linux Mint's resolution of DNS queries to be rock solid reliable.