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: CanonicalizeHostname lets you type short host names in ssh

Network and access
~/tips/tip-ssh-canonicalize-hostname

CanonicalizeHostname yes

Make ssh add your domain to short names and then apply the matching config block.

I like typing ssh web1 instead of ssh web1.example.com. A search domain in resolv.conf does that too, but ssh’s own canonicalization also makes the config match the full name.

At the top of ~/.ssh/config:

CanonicalizeHostname yes
CanonicalDomains example.com lab.example.com

Host *.example.com
    User admin

Now ssh web1 tries web1.example.com, then web1.lab.example.com, and uses the first one that resolves. After that, ssh reads the config again with the full name, so the Host *.example.com block applies. Without canonicalization that block would never match a short name.

It also helps known_hosts. The host key is stored under the full name, so you do not end up with separate entries for web1 and web1.example.com.

Some settings worth knowing: CanonicalizeMaxDots (default 1) means a name with a dot in it is left alone, and CanonicalizeFallbackLocal no makes ssh fail at once if no domain matches, instead of asking the system resolver.

The man page notes that hosts reached through ProxyJump or ProxyCommand are skipped unless you set it to always. If a jump host setup suddenly ignores your short names, that is why.

Further reading: ssh_config man page

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