Question

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

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 setup 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 what ever 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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top