Dynamically resolve hostname to multiple, prioritized IPs

0

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:

192.168.1.1 myserver
10.1.1.1 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:

host-record=myserver,10.1.1.1
host-record=myserver,192.168.1.1

When I use dig myserver +short I get:

10.1.1.1
192.168.1.1

the first time around and

192.168.1.1
10.1.1.1

the second time, and so on...

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

PING myserver (192.168.1.1) 56(84) bytes of data.
From localhost (192.168.1.3) icmp_seq=1 Destination Host Unreachable
From localhost (192.168.1.3) icmp_seq=2 Destination Host Unreachable
From localhost (192.168.1.3) 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?

micahscopes

Posted 2016-04-26T18:37:53.673

Reputation: 101

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 – 2016-04-26T22:07:04.137

Answers

0

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

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

PING myserver (192.168.1.1) 56(84) bytes of data.

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

sysfiend

Posted 2016-04-26T18:37:53.673

Reputation: 417

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 – 2016-05-03T13:24:43.090