Saturday, February 27, 2010

linux resolv.conf nameserver rotation

This post is about linux resolver. It’s the one that reads /etc/resolv.conf and translates names into IPs. I had this problem with my linux at work. I wanted to resolve both internet hostnames and internal hostnames. For that i needed 2 nameservers. But the lame default resolver configuration works like this. Let’s say that /etc/resolv.conf looks like this:
nameserver 192.168.1.1 #for internal hosts
nameserver 194.102.1.1 #for external hosts
So, linux resolver tries the first nameserver. If the nameserver responds quickly it doesn’t try the second one. It ONLY tries the second one if the first times out. So if i try with this configuration to resolve internet hosts, it fails. Because it only tries the first nameserver, it replies in 1 ms so if i ask for yahoo.com, it tells me “unknown host”. Great. If i switch them, then it will only resolve internet hosts, but no internal ones.
After googling around, i found out that you can balance dns queries between resolv.conf entries. So, there’s a big chance that it will rotate just the way you need. So, to make it rotate, just add options rotate on the first line of resolv.conf, before other nameserver lines, it will then work beautifully.

No comments:

Post a Comment