I have a small home server and I'd like programs on my laptop to be able to access it from both LAN and WAN. I think that the cleanest way to achieve this is to define a custom hostname and resolve it differently based on the network it's connected to.

In other words, is it possible to resolve hostname X as ip address A if connected to network Foo, and as ip address B otherwise?

I'm using Arch Linux and netctl as my network manager. I assume the way to achieve what I want is to set up hooks in the network profile for Foo to somehow change the hosts file, but I'm not sure on how to do the latter.

Of course, I'd like to hear about better solutions, too.

  • 3
    Search for "split DNS" or "split-horizon DNS". – Satō Katsura Sep 14 '16 at 10:15
  • @SatoKatsura, thanks for putting me on the right track. For various reasons I can't set up a DNS server for the whole LAN, but I found a nice compromise. – A.P. Sep 14 '16 at 12:57

I solved this problem with dnsmasq. Following the example in the question, here's what I did after setting up dnsmasq to act as a DNS cache on my laptop:

  1. Create a directory /etc/hosts.d

  2. Put the following scripts in /etc/netctl/X/ and make them executable

 # /etc/netctl/X/X_local.sh
 echo 'A X' > /etc/hosts.d/X
 chmod 744 /etc/hosts.d/X
 # /etc/netctl/X/X_remote.sh
 echo 'B X' > /etc/hosts.d/X
 chmod 744 /etc/hosts.d/X
  1. Add the following lines to the network profile /etc/netctl/Foo
  1. Append addn-hosts=/etc/hosts.d to /etc/dnsmasq.conf

This works because the addn-hosts option instructs dnsmasq to look in the /etc/hosts.d directory for additional hostname associations.

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.