I have a server at home, with internal IP 192.168.1.100
. I use a dynamic DNS service so that I can reach it at http://foo.dynu.com
when I am out. When I have my laptop at home, I know that I could directly connect to the server by adding the following line to /etc/hosts
.
192.168.1.100 foo.dynu.com
However, is there a way to automatically apply this redirect only when I'm on my home network? (I usually connect via a particular wifi connection, although I occasionally connect via ethernet. If this complicates matters, then I'm happy to only set it for the wifi connection.) I use Network Manager.
Also, I connect to the internet via a VPN, so presumably any configuration on my (OpenWRT) router is unlikely to work.
Best Answer
As per @garethTheRed's suggestion in the comments, I created a Network Manager Dispatcher hook.
Create the following file at
/etc/NetworkManager/dispatcher.d/99_foo.dynu.com.sh
. This progresses when a new network connection is detected (i.e. ethernet or wifi). It then identifies my "home network" in two ways: the BSSID/SSID and the static IP that my router assigns me. (At the moment it doesn't work when I connect via ethernet, since that's relatively rare.) It then appends the mapping to the hosts file if we are in the home network; if not, then it removes this line.