dnsmasq and hostnames with different IPs on different networks

dnsmasq hosts configuration I run a raspberry pi as a router, and also as a server for email, a few local web applications, and some other odds and ends. I want to be able to access these services from both networks, without having to change application settings or remember which network I’m on to figure out what IP or hostname to contact.

dnsmasq has a handy feature that allows you to specify multiple IPs for a hostname, and if it can it will resolve that hostname to an IP on the same network as incoming query. You can see the dnsmasq manpage under --localise-queries for a bit more information, but it’s really pretty simple.

My router exists on two networks: and Its interfaces have addresses and respectively. I want the hostname pi to resolve to the device regardless of which network I’m connected to. So, when dnsmasq receives a DNS request for hostname pi, if the request came on the .0.0 network, it returns, and if it arrives on the .1.0 network, it returns the .1.1 ip. Simple enough, and it’s really easy to set up too:

In /etc/dnsmasq.conf, simply add the line:


and then in your /etc/hosts file (or your addn-hosts file if that’s how you configured dnsmasq, like in the screenshot above), add one line for each IP address you want, like this: pi pi

These lines act like normal /etc/hosts definitions, so you can put multiple hostnames per IP if you like.

Restart dnsmasq, and that’s it.

comments powered by Disqus