brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Match localnetwork picks SSH settings based on the network you are on

Network and access
~/tips/tip-ssh-match-localnetwork

Match host nas localnetwork 192.0.2.0/24

Use a direct address at home and a jump host elsewhere, with one host name.

A laptop moves between networks, and the right way to reach a server often depends on where you sit. Since OpenSSH 9.4, ssh_config has a localnetwork criterion for exactly that.

It matches when one of your active network interfaces has an address inside the range you give. The first matching value wins in ssh_config, so I’d put the specific block before the general one:

Match host nas localnetwork 192.0.2.0/24
    HostName 192.0.2.20

Host nas
    HostName nas.example.com
    ProxyJump [email protected]

At home, ssh nas goes straight to the LAN address. On any other network, the first block does not match, so ssh falls through to the second one and hops via the jump host.

You can test both paths without moving: ssh -G nas | grep -E '^(hostname|proxyjump) ' shows what ssh would use right now.

Two things to know. The man page warns that a network address is not a trustworthy signal, since a hotel or café can hand out the same private range as your home router. So use it for convenience, like picking a route, and never to loosen something like host key checking. And older clients do not know the keyword at all, so a shared config breaks on a machine with an OpenSSH older than 9.4.

Further reading: ssh_config man page, OpenSSH 9.4 release notes

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link