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.

2
  • 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 8, 2012 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. Nov 13, 2012 at 10:10

5 Answers 5

42
+250

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.

5
  • Is there really no way to have 2 networks without running multiple instances? This really complicates things
    – TheLQ
    Dec 11, 2012 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, 2012 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 Apr 24, 2016 at 1: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 for details. May 20, 2017 at 15:07
  • Nice solution but, how to enable these two instances on boot?
    – a7md0
    Nov 26, 2018 at 19:42
34

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
4
  • 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 6, 2015 at 10:17
  • 1
    @Dyna It works, but it is not documented or did i miss something?
    – Murmel
    Mar 18, 2016 at 9:05
  • 5
    @user1885518 I was struggling with the same question, till I found this in the man page: 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 1, 2016 at 19:51
  • Best solution for serving multiple DHCP clients using a single instance of dnsmasq.
    – user3091673
    Aug 7, 2020 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.

11

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.

3
  • Can you help me with a similar scenario? I am not able to crack a solution. serverfault.com/questions/825601/…
    – 55597
    Feb 15, 2017 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, 2017 at 1:00
  • This is distro specific behavior. Apr 12, 2021 at 6: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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.