dnsmasq, serve different ip addresses based on interface used
Asked Answered
A

5

30

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.

Annabelle answered 17/2, 2012 at 10:18 Comment(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?Carducci
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.Annabelle
C
43

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.

Carducci answered 8/11, 2012 at 19:48 Comment(5)
Is there really no way to have 2 networks without running multiple instances? This really complicates thingsMelodie
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.Carducci
ensure that configuration option listen-address=0.0.0.0 is commented out in config-file. e.g. #listen-address=0.0.0.0Inwardly
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.Lorgnon
Nice solution but, how to enable these two instances on boot?Immaculate
N
35

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
Neurology answered 17/11, 2014 at 1:26 Comment(4)
Works for me too when using dhcp-range instead of dhcp-host. This should be the accepted solution for current dnsmasq versions.Furthermore
@Dyna It works, but it is not documented or did i miss something?Vigue
@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.Amiamiable
Best solution for serving multiple DHCP clients using a single instance of dnsmasq.Tichonn
L
28

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.

Lorgnon answered 30/5, 2014 at 9:58 Comment(0)
I
13

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.

Inconsiderable answered 11/11, 2016 at 16:0 Comment(3)
Can you help me with a similar scenario? I am not able to crack a solution. serverfault.com/questions/825601/…Whelk
this will not work, as dnsmasq just reads the files sequentially and all the files in /etc/dnsmasq.d/ are considered single configuration.Primordium
This is distro specific behavior.Inflammable
C
2

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.

Corvette answered 13/10, 2022 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.