How to fix slow DNS lookups on Ubuntu

If you're using a relatively recent version of Ubuntu, chances are that you have encountered spurious slowdowns that might be related to a very specific DNS failure. For me, it was the fact that ping to a host without a reverse DNS entry would only transmit a single ICMP request per second, even when a higher rate was specified via the -i option.

I've traced the DNS requests that are performed by ping by default (the effect does not occur when using the -n option which disables host name lookup), and didn't notice anything out of the ordinary. The NXDOMAIN responses were occuring almost instantly, but nevertheless, it took precisely one second for this response to actually propagate to the ping process.

To make a long story short: The reson for this is that Ubuntu (or more precisely, the Name Switching Service), will (by default) try to lookup DNS records not only via the regular DNS server configured via the network settings, but also using Zeroconf (a.k.a. Bonjour); a protocol that can be used to resolve hostnames locally by using multicast DNS requests and responses.

This is not an issue for DNS queries that can be answered positively by your regular DNS server (those will always take precedence over records received via Zeroconf), but it can be a problem for negative DNS responses (NXDOMAIN): When the resolving library receives one of those, it will try a Zeroconf lookup, and this can take a while – especially for a host that does not exist.

Since Zeroconf is only rarely used on Linux and is almost always limited to the .local top-level domain, this behavior seems useless at best, and can be pretty irritating.

To fix it, you can simply disable the Zeroconf DNS lookups in the configuration file /etc/nsswitch.conf by changing the line

hosts:          files myhostname mdns4_minimal [NOTFOUND=return] dns mdns4


hosts:          files myhostname mdns4_minimal [NOTFOUND=return] dns

This doesn't entirely disable Zeroconf – it only restricts the lookups to the .local domain, which is almost always the only place where they are useful anyway.

The effects should be immediately noticeable – just try to ping one of the previously slow to respond hosts and check if the ICMP requests are still limited to one per second.

If you think that this should be the default configuration for Ubuntu, you are not alone – there is a bug report on Ubuntu's bug tracker that describes the problem, but since it's been known since 2007, I wouldn't bet on the default changing anytime soon.

Comments !