With a reasonably modern OpenSSH, you can run a shell command to select a Match
block in ~/.ssh/config
. Assuming you have a script am-on-home-network
that returns 0 when executed on your home network and 1 when executed outside:
Match Host myserver exec "am-on-home-network"
HostName myserver
User iago-lito
Port 22
Host myserver
HostName myserver.ddns.net
User iago-lito
Port 22
For am-on-home-network
, you can use arp
to explore the local network. Look for your home router's MAC address. (Looking for IP addresses is unreliable because many private networks use the same ranges of private IP addresses.)
#!/bin/sh
timeout 0.2 arping -f -q -I eth0 12:34:56:78:9a:bc
Adjust the MAC address to the MAC address of your router that your computer sees when it's at home. Adjust eth0
to the network interface on your computer that is used to connect to your home router.
The pure SSH approach has the advantage that it can be done in userland, but it only works for SSH, and it increases the connection establishment delay noticeably. A better solution is to run a DNS server at the system level, and configure it to serve the local IP address the global name myserver.ddns.net
when on the local network. Dnsmasq is a small, simple DNS cache and server, suitable for running on an endpoint machine or a small network. If you aren't already running a DNS cache on your machine, it will make general Internet usage a bit faster. Ubuntu runs dnsmasq by default.
In dnsmasq, create a file /etc/dnsmasq.d/home-server
containing
host-record=myserver.ddns.net,192.168.2.1
Add the following script to your network startup scripts (whatever they are on your distribution):
#!/bin/sh
comment=\#
if timeout 0.2 arping -f -q -I eth0 12:34:56:78:9a:bc; then
comment=
fi
sed -i "\$s/^#*/$comment/" /etc/dnsmasq.d/home-server
service dnsmasq restart
If your system sets up Dnsmasq through D-Bus, editing the configuration file isn't the best option, and I don't even know if it'll work. You would need to call dbus-send
to add or remove the host record based on the output of arping. Or, if you're using NetworkManager, configure it to set the host entry on the connection corresponding to your home network.