dnsmasq, serve different ip addresses based on interface used
You can run two instances of dnsmasq
, each with a different interface it listens on. You can use the --interface=X
and --bind-interfaces
options for that. By default, it also binds the loopback device lo
and will fail if two processes try to bind it. Use --except-interface=lo
to avoid that.
dnsmasq --interface=eth0 --except-interface=lo --bind-interfaces --dhcp-range=192.168.0.2,192.168.0.10,12h
dnsmasq --interface=eth1 --except-interface=lo --bind-interfaces --dhcp-range=10.0.0.2,10.0.0.10,12h
Make sure your configuration file is empty when you test this as it always overrides the command line. You can also use --conf-file=/dev/null
.
As I mentioned in the comment, I'm not too sure how this helps your situation, but it might help anyone else who tries to get two different address ranges on two different interfaces.
Adding the interface at the beginning of each parameter works fine for me. Example (in dnsmasq.conf) :
dhcp-host=eth0,00:22:43:4b:18:43,192.168.0.7
dhcp-host=eth1,00:22:43:4b:18:43,192.168.1.7
I am using the release :
$ dnsmasq --version
Version de Dnsmasq 2.68 Copyright (c) 2000-2013 Simon Kelley
While @kichik's answer may well work, a more elegant way to achieve the same might be to use the localise-queries
directive and a single dnsmasq
server instance.
I'll assume that you already configured your DHCP ranges for the different interfaces, and have bound dnsmasq
to those.
Add the (partially documented) localise-queries
option to your dnsmasq.conf
file.
# /etc/dnsmasq.conf
localise-queries
Then, make sure that one of the files that dnsmasq
reads for your hosts (such as /etc/hosts
) contains entries with the IP addresses for both networks, like this:
# /etc/hosts
127.0.0.1 dev-vm
192.168.1.1 dev-vm
10.0.0.1 dev-vm
An alternative to changing the /etc/hosts
file is to specify the addresses in your dnsmasq.conf
file instead:
# /etc/dnsmasq.conf
localise-queries
host-record=dev-vm,127.0.0.1
host-record=dev-vm,192.168.1.1
host-record=dev-vm,10.0.0.1
As a result in both cases, dnsmasq
will serve only the IP that matches the interface's IP and netmask for queries received on that particular interface.
According to the man page, this does the following:
-y, --localise-queries
Return answers to DNS queries from /etc/hosts which depend on the interface over which the query was received. If a name in /etc/hosts has more than one address associated with it, and at least one of those addresses is on the same subnet as the interface to which the query was sent, then return only the address(es) on that subnet. This allows for a server to have multiple addresses in /etc/hosts corresponding to each of its interfaces, and hosts will get the correct address based on which network they are attached to. Currently this facility is limited to IPv4.