Search code examples
dnsvirtual-machinednsmasq

dnsmasq, serve different ip addresses based on interface used


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.


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.