Anyone who SSHes to more than a handful of hosts eventually accumulates a tangle of bash aliases, three or four different identity files, and command lines that take fifteen seconds to type. The fix is a well-structured ~/.ssh/config. This article documents the patterns we put on every operator workstation so the SSH tooling stops being the bottleneck.
How to verify
Inventory what the workstation already has.
ls -l ~/.ssh/
cat ~/.ssh/config 2>/dev/null
ssh -G web-01.prod.example.net | head -20
ssh -G shortname | head -20
ssh -G <host> prints the resolved config for that host — all aliases applied, all Match blocks expanded. It is the equivalent of sshd -T on the server side. If ssh -G shortname resolves to the right hostname and identity, the alias works.
What’s happening
~/.ssh/config is read in order, top to bottom. For each setting, the first match wins (same first-match semantics as sshd). A typical block looks like:
Host web-01
HostName web-01.prod.example.net
User ops
IdentityFile ~/.ssh/id_ed25519_ops
Port 22
The Host pattern can be a glob (Host *.prod.example.net), a comma-separated list (Host web-01,web-02), or * as a catch-all at the bottom. Settings cascade: more-specific blocks at the top, more-general blocks below. The first matching value for each setting is used.
Two newer features change the picture for fleet-scale: Include (which lets you split config across multiple files) and Match (which can branch on hostname, user, exec, etc.). Used together, they make a config that scales past dozens of hosts without becoming unreadable.
The procedure
-
Set the directory and file permissions. sshd will refuse a key whose authorized_keys is too permissive, and ssh will silently ignore a config file that is world-readable.
chmod 0700 ~/.ssh touch ~/.ssh/config chmod 0600 ~/.ssh/config -
Write a baseline config with sensible defaults at the bottom. Defaults at the bottom because of first-match semantics — specific blocks above, defaults below.
# ~/.ssh/config Host web-01 HostName web-01.prod.example.net User ops IdentityFile ~/.ssh/id_ed25519_ops Host web-*.prod User ops IdentityFile ~/.ssh/id_ed25519_ops ForwardAgent no Host *.staging.example.net User deploy IdentityFile ~/.ssh/id_ed25519_deploy StrictHostKeyChecking accept-new Host bastion HostName bastion.example.net User ops IdentityFile ~/.ssh/id_ed25519_ops ControlMaster auto ControlPath ~/.ssh/sockets/%C ControlPersist 10m # Defaults — most general, last Host * ServerAliveInterval 30 ServerAliveCountMax 6 HashKnownHosts no VisualHostKey no UpdateHostKeys yes AddKeysToAgent yes IdentitiesOnly yesIdentitiesOnly yesmatters more than it looks — without it, the agent offers every loaded key to every server, which can trip per-IP rate-limiting on jump hosts. -
Create the control-socket directory if you use connection multiplexing. Without it, ControlPath fails silently.
install -d -m 0700 ~/.ssh/socketsMultiplexing means the first
ssh bastionopens a TCP connection, and subsequentssh bastioncalls reuse it for 10 minutes. Subsequent connections are near-instant. -
Use
Includeto keep work and personal hosts separate. Useful for multi-employer workstations.# ~/.ssh/config (top of file) Include ~/.ssh/config.d/work Include ~/.ssh/config.d/personalEach included file is its own
Hostnamespace.Includepaths are relative to~/.ssh/if not absolute. -
Use
Matchfor conditional logic thatHostcannot express. The most useful pattern is “use a different identity for hosts whose hostname contains ‘prod’”:Match host=*.prod.example.net IdentityFile ~/.ssh/id_ed25519_prod PreferredAuthentications publickey Match host=*.dev.example.net IdentityFile ~/.ssh/id_ed25519_devMatchis evaluated afterHost, so it can refine a generic block.Match execlets you run a command to decide; useful for “if the VPN is up, use the internal hostname.” -
Test the resolution. Whenever you change the config, verify the values for the host you care about.
ssh -G web-01 | grep -iE 'hostname|user|identityfile|port' ssh -G web-02.prod | grep -iE 'hostname|user|identityfile|port'The output is the fully resolved config that ssh will actually use.
-
For ProxyJump (bastion) hosts, see the dedicated article. Pattern:
Host internal-* ProxyJump bastion User opsThis is covered in depth in ssh-proxyjump-bastion.
-
Document team conventions in a shared file. When the team standardizes on aliases (
web-01instead ofweb-01.prod.example.net), put the working~/.ssh/configsnippet in the runbook. The standard is only useful if everyone has it.
Common pitfalls
- File mode wrong:
~/.ssh/configshould be0600. World-readable configs are silently ignored on some OpenSSH builds. - Putting
Host *at the top of the file instead of the bottom. First-match semantics mean the catch-all swallows everything. - Forgetting
IdentitiesOnly yesand offering 12 keys to every server — bastions with rate-limiting will refuse you after the third one. - Using
ControlMasterwithout creating the socket directory; you get errors that look like routing problems but are filesystem permissions. - Forgetting that
Match execruns every time SSH evaluates the host; a slow command makes every SSH invocation slow.
In our fleet, every operator workstation ships with a baseline ~/.ssh/config from the team’s ops repo, and per-engagement aliases come from a managed include. The result is that any operator can SSH into any client environment with the conventional shortname, with the right key, through the right jump host, without thinking about it. That uniformity is part of how Managed Operations avoids the “where do you keep the bastion address” question at 03:00.