28

Basically my situation is that I'm running a VM for developing web sites.

The host machine has its dns pointing at the VM which, is running dnsmasq, which resolves the addresses of various dev sites; i.e. test.mysite.vm, etc.

The issue is, when I go from my work network to my home network, it all breaks because the IP of the VM changes. Is it possible to serve different IP addresses based on which interface the request came from? Or should I be trying to tackle this in a completely different way?

Thanks for your help!


Turns out there was a much easier approach to this after all...

I now set up 2 interfaces on the VM, and don't need to use dnsmasq.

The first is just a bridged/shared interface which allows the VM to use whatever internet connection is available to the host, with a restart of the network each time I move office.

The 2nd is a private connection to my VM host, which has a static IP address. This is the interface I use to connect and bind any services such as nginx, mysql, etc.

user2066657
  • 444
  • 1
  • 4
  • 23
Ian Chadwick
  • 1,547
  • 1
  • 19
  • 21
  • Where do the different interfaces come into play? Are you using two different interfaces for your home network and work network? Why does the IP of the VM changes? – kichik Nov 08 '12 at 18:59
  • Yes it was 2 different interfaces for home (192.168.0.*) and work (10.0.0.*). The change is between the work IP and home IP. – Ian Chadwick Nov 13 '12 at 10:10

5 Answers5

42

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.

kichik
  • 33,220
  • 7
  • 94
  • 114
  • Is there really no way to have 2 networks without running multiple instances? This really complicates things – TheLQ Dec 11 '12 at 15:43
  • 3
    You can actually use `--dhcp-range=` multiple times in the same command to get it to serve two networks. That might actually work for the original question too. It will probably deduct the correct IP range to serve based on the interface's IP address. – kichik Dec 11 '12 at 20:11
  • ensure that configuration option `listen-address=0.0.0.0` is commented out in config-file. e.g. `#listen-address=0.0.0.0` – Tobias Hochgürtel Apr 24 '16 at 01:02
  • 1
    If one of your interfaces is served by an outside DHCP server (such as your ISP), an alternative might be to use the `localise-queries` option. See [this answer](http://stackoverflow.com/a/23951798/1553481) for details. – likeitlikeit May 20 '17 at 15:07
  • Nice solution but, how to enable these two instances on boot? – a7md0 Nov 26 '18 at 19:42
33

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
Stéphane Millien
  • 3,238
  • 22
  • 36
  • 5
    Works for me too when using `dhcp-range` instead of `dhcp-host`. This should be the accepted solution for current dnsmasq versions. – Dynalon Apr 06 '15 at 10:17
  • 1
    @Dyna It works, but it is not documented or did i miss something? – Murmel Mar 18 '16 at 09:05
  • 4
    @user1885518 I was struggling with the same question, till I found this in [the man page](http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html): `The tag "bootp" is set for BOOTP requests, and a tag whose name is the name of the interface on which the request arrived is also set.`. So there's always a tag named after the interface where the request arrived. Very useful for multi-vlan dhcp. – GnP Aug 01 '16 at 19:51
  • Best solution for serving multiple DHCP clients using a single instance of dnsmasq. –  Aug 07 '20 at 23:23
26

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.

Community
  • 1
  • 1
likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
10

Alternatively you can also create multiple configuration files under /etc/dnsmasq.d/, one for each interface you want to serve dhcp.

For instance if you have two wireless interfaces named wlan0 and wlan1, and you want to serve dhcp on them thanks to dnsmasq, you can create two files under /etc/dnsmasq.d/ to configure each interface:

/etc/dnsmasq.d/dnsmasq-wlan0.conf:

interface=wlan0         # Use interface wlan0
listen-address=10.0.0.1 # Explicitly specify the address to listen on
bind-interfaces         # Bind to the interface to make sure we aren't sending things elsewhere
server=8.8.8.8          # Forward DNS requests to Google DNS
domain-needed           # Don't forward short names
bogus-priv              # Never forward addresses in the non-routed address spaces.
dhcp-range=10.0.0.50,10.0.0.150,12h # Assign IP addresses between 10.0.0.50 and 10.0.0.150 with a 12 hour lease time

/etc/dnsmasq.d/dnsmasq-wlan1.conf:

interface=wlan1         # Use interface wlan0
listen-address=20.0.0.1 # Explicitly specify the address to listen on
bind-interfaces         # Bind to the interface to make sure we aren't sending things elsewhere
server=8.8.8.8          # Forward DNS requests to Google DNS
domain-needed           # Don't forward short names
bogus-priv              # Never forward addresses in the non-routed address spaces.
dhcp-range=20.0.0.50,20.0.0.150,12h # Assign IP addresses between 20.0.0.50 and 20.0.0.150 with a 12 hour lease time

To me that is a very clean way to configure your system, and have the configuration persist in between reboots.

Quentin
  • 1,854
  • 1
  • 19
  • 19
  • Can you help me with a similar scenario? I am not able to crack a solution. http://serverfault.com/questions/825601/dnsmasq-config-for-captive-portal – 55597 Feb 15 '17 at 14:14
  • 6
    this will not work, as dnsmasq just reads the files sequentially and all the files in /etc/dnsmasq.d/ are considered single configuration. – Dingo Apr 20 '17 at 01:00
  • This is distro specific behavior. – John Leuenhagen Apr 12 '21 at 06:52
0

From here: https://palimpsest.minivi.com/dnsmasq/dhcp-server-on-multiple-network-interfaces/

To provide DHCP service on more than one network interface using dnsmasq, use this lines on /etc/dnsmasq.conf

dhcp-range=interface:eth0,192.168.1.128,192.168.1.254,24h
dhcp-range=interface:eth1,192.168.2.128,192.168.2.254,24h

So one dnsmasq instance can manage more than one segments on different interface.

Ben
  • 1,133
  • 1
  • 15
  • 30