Skip to content

SSH client config — host aliases and per-host settings

A well-structured ~/.ssh/config replaces the tangle of bash aliases and long ssh command lines. Here is the pattern we use for fleet-scale workstations.

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

  1. 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
  2. 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 yes

    IdentitiesOnly yes matters 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.

  3. Create the control-socket directory if you use connection multiplexing. Without it, ControlPath fails silently.

    install -d -m 0700 ~/.ssh/sockets

    Multiplexing means the first ssh bastion opens a TCP connection, and subsequent ssh bastion calls reuse it for 10 minutes. Subsequent connections are near-instant.

  4. Use Include to 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/personal

    Each included file is its own Host namespace. Include paths are relative to ~/.ssh/ if not absolute.

  5. Use Match for conditional logic that Host cannot 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_dev

    Match is evaluated after Host, so it can refine a generic block. Match exec lets you run a command to decide; useful for “if the VPN is up, use the internal hostname.”

  6. 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.

  7. For ProxyJump (bastion) hosts, see the dedicated article. Pattern:

    Host internal-*
        ProxyJump bastion
        User ops

    This is covered in depth in ssh-proxyjump-bastion.

  8. Document team conventions in a shared file. When the team standardizes on aliases (web-01 instead of web-01.prod.example.net), put the working ~/.ssh/config snippet in the runbook. The standard is only useful if everyone has it.

Common pitfalls

  • File mode wrong: ~/.ssh/config should be 0600. 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 yes and offering 12 keys to every server — bastions with rate-limiting will refuse you after the third one.
  • Using ControlMaster without creating the socket directory; you get errors that look like routing problems but are filesystem permissions.
  • Forgetting that Match exec runs 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.