I have a linux server connected to a VPN.

At home, I use the same VPN on my laptop, while connected to the server directly through a wireless router.

To do this, I have two entries in my /etc/hosts file: myserver myserver.vpn

This way I can choose how I connect manually by using a different hostname.

My question is: how can I connect dynamically using the same hostname? Ideally, the laptop would know which IP to use based on which network interfaces it's connected through, prioritizing a real interface over a virtual one. And if an address wasn't available, I want it to try another one!

I've attempted to set this up using dnsmasq on the laptop with multiple host records and the localise-queries option. In my dnsmasq.conf I have:


When I use dig myserver +short I get:

the first time around and

the second time, and so on...

But if I change the static address of myserver to, and do ping myserver from the laptop (, I get:

PING myserver ( 56(84) bytes of data.
From localhost ( icmp_seq=1 Destination Host Unreachable
From localhost ( icmp_seq=2 Destination Host Unreachable
From localhost ( icmp_seq=3 Destination Host Unreachable

So a couple of questions:

(1) why doesn't the laptop try another route, even though dnsmasq is providing one? (2) how can I prioritize routes based on which network connection is being used?

By the way, I tried using NetworkManager and customizing the DNS entries for each connection (e.g. to use my router's DNS server to resolve hostnames on the LAN, and the laptop's local dnsmasq to look for hostnames on the VPN). Well, this seemed like a good idea, but every time I activated an interface, it would override the DNS settings of the other interfaces. That's not what I wanted!

Question (3): Is there a better way of going about this?

  • Can you provide a diagram, or at minimum advise where your server is hosted (is it hosted on your home connection?) IP ranges in use would be useful as well. Where does the VPN connect to and what type of VPN is it. [ You are asking some pretty hard questions - some of this can be done, some can't - but none of it is trivial. You can, for example, use Split DNS so your DNS servers provide different answers depending on where the query originated, and you probably need to modify how and where routes are advertised on the LAN and VPN. For IP lookups, DNS does not provide IP's. – davidgo Apr 26 '16 at 22:07

I might be missing something here but, how is your computer suppossed to know that myserver does no longer have IP after you change it?

When you do ping myserverafter changing the IP, it says:

PING myserver ( 56(84) bytes of data.

So, it is trying to ping the address you have set before.

|improve this answer|||||
  • I only changed the address to see if my computer would use the other address given for that hostname, rather than just timing up and giving up... so to simulate an environment where that IP wasn't available. – micahscopes May 3 '16 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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