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: 192.168.0.0 and 192.168.1.0. Its interfaces have addresses 192.168.0.3 and 192.168.1.1 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 192.168.0.3, 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:

localise-queries

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:

192.168.0.3 pi
192.168.1.1 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