DEV Community

Wardterm
Wardterm

Posted on Originally published at wardterm.com

SSH config examples: 12 blocks ready to copy

The 12 SSH config examples on this page

Each block below works as written: copy it into ~/.ssh/config, change the names and addresses, and connect. If the file does not exist yet, create it with the right permissions:

mkdir -p ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/config && chmod 600 ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  1. A short name for a server
  2. A key per server
  3. Many servers with one pattern
  4. Two GitHub accounts
  5. A jump host
  6. Saved tunnels
  7. Keepalive
  8. One connection reused
  9. Passphrases in the macOS Keychain
  10. tmux on every connection
  11. Old routers and switches
  12. Separate files with Include

Every block was checked with ssh -G on OpenSSH 10. For how the file is read line by line, see the SSH config file guide.

SSH config examples for multiple hosts

1. A short name for a server

Host web
    HostName 203.0.113.10
    User deploy
    Port 2222
Enter fullscreen mode Exit fullscreen mode

ssh web now replaces ssh -p 2222 deploy@203.0.113.10. Host is the name you type; HostName is the real address.

2. A key per server

Host db
    HostName db.example.com
    User admin
    IdentityFile ~/.ssh/id_ed25519_db
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

IdentitiesOnly yes stops SSH from trying every key in your agent first. With five or six keys loaded, servers start refusing you with Too many authentication failures before they reach the right one.

3. Many servers with one pattern

Host app1 app2
    HostName %h.internal.example.com
    User deploy

Host *.prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_prod
Enter fullscreen mode Exit fullscreen mode

%h is the name you typed, so ssh app1 goes to app1.internal.example.com. The second block covers every production host at once.

Wardterm reads your ~/.ssh/config, with keys, jump hosts and tunnels, and turns each Host into a server you open with a tap. Free to start.

How do you use two GitHub accounts in SSH config?

4. Two GitHub accounts

Host github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Personal repositories keep their normal address. For work ones, swap the host in the clone URL:

git clone git@github-work:company/api.git
ssh -T git@github-work
Enter fullscreen mode Exit fullscreen mode

The second line asks GitHub which account the key belongs to. For an existing repository, change its remote with git remote set-url origin git@github-work:company/api.git. The full setup, with the commit email per account, is in multiple GitHub accounts with SSH.

SSH config examples for jump hosts and tunnels

5. A jump host with ProxyJump

Host bastion
    HostName bastion.example.com
    User jump

Host 10.0.1.*
    User deploy
    ProxyJump bastion
Enter fullscreen mode Exit fullscreen mode

ssh 10.0.1.5 now goes through the bastion on its own, and so do scp and rsync. Your keys stay on your computer, which is why ProxyJump is the safer choice compared with forwarding the agent. More in ProxyJump and jump hosts.

6. Saved tunnels

Host db-tunnel
    HostName db.example.com
    User admin
    LocalForward 5432 localhost:5432
    DynamicForward 1080
    SessionType none
    ExitOnForwardFailure yes
Enter fullscreen mode Exit fullscreen mode

ssh db-tunnel opens the database at localhost:5432 and a SOCKS proxy on port 1080, with no shell (SessionType none is the same as -N). ExitOnForwardFailure makes SSH stop if a port is taken, instead of connecting without the tunnel.

How do you keep SSH connections alive and fast?

7. Keepalive

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 3
Enter fullscreen mode Exit fullscreen mode

Home routers and cloud firewalls drop connections that stay quiet for a few minutes. A small message every 30 seconds keeps them open, and after three without an answer SSH closes the session instead of freezing. If sessions still drop when your laptop sleeps, that is a job for tmux or Mosh, not the config.

8. One connection reused

Host web db *.prod.example.com
    ControlMaster auto
    ControlPath ~/.ssh/cm-%C
    ControlPersist 10m
Enter fullscreen mode Exit fullscreen mode

The first connection opens a socket; every ssh, scp or git after it reuses it and starts at once. Name the hosts instead of using Host *: two aliases for the same address and user, such as the two GitHub accounts above, would share one socket and one key. ssh -O exit web closes it. See SSH multiplexing.

Which SSH config examples help on a Mac?

9. Passphrases in the macOS Keychain

Host *
    IgnoreUnknown UseKeychain
    AddKeysToAgent yes
    UseKeychain yes
Enter fullscreen mode Exit fullscreen mode

You type a key's passphrase once and macOS remembers it, even after a restart. UseKeychain exists only in the ssh that comes with macOS, so IgnoreUnknown keeps the same file working on Linux, where it would stop with Bad configuration option. Details in ssh-agent and the macOS Keychain.

10. tmux on every connection

Host dev
    HostName dev.example.com
    User me
    RequestTTY yes
    RemoteCommand tmux new-session -A -s main
Enter fullscreen mode Exit fullscreen mode

ssh dev lands in the tmux session called main, or creates it. When the connection drops, the next ssh dev brings back the same windows. To open a plain shell once, run ssh -o RemoteCommand=none dev.

Which SSH config example works for old routers and switches?

11. Old devices

Host switch
    HostName 192.168.1.2
    User admin
    KexAlgorithms +diffie-hellman-group14-sha1
    HostKeyAlgorithms +ssh-rsa
    PubkeyAcceptedAlgorithms +ssh-rsa
Enter fullscreen mode Exit fullscreen mode

Current OpenSSH turns off algorithms that old network gear still uses, and you see no matching key exchange method found. The + adds the old ones back for this host only, and leaves the defaults for everything else. The error message names the algorithm the device offers: use that one. See no matching key exchange method.

A complete SSH config file to start from

12. Separate files with Include

Include config.d/*

Host web
    HostName 203.0.113.10
    User deploy
    Port 2222

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 3
    IgnoreUnknown UseKeychain
    AddKeysToAgent yes
    UseKeychain yes
Enter fullscreen mode Exit fullscreen mode

Three rules make this layout work:

  • Include goes at the top. Placed after a Host line, it belongs to that block. A relative path like config.d/* means ~/.ssh/config.d/, so you can keep one file per client or project there.
  • Specific hosts come first. For each setting SSH keeps the first value it finds, so a Host * at the top would override everything below it.
  • Do not put IdentityFile in Host *. It adds that key to every host, on top of the ones you named.

How do you check an SSH config before you connect?

ssh -G prints the final settings for a name without connecting. Filter the lines you care about:

ssh -G github-work | grep -E '^(hostname|user|port|identityfile|proxyjump) '
Enter fullscreen mode Exit fullscreen mode

To try a new block without touching your real file, keep it apart and point SSH at it:

ssh -F ~/test-config -G web
Enter fullscreen mode Exit fullscreen mode

If a value is not the one you wrote, an earlier block set it first. Look for a Host * or a pattern above yours.

Use the same SSH config examples on iPhone and iPad

A config file lives on one computer. Your phone has never heard of web or github-work, and typing the real addresses on a small keyboard is where the mistakes start.

Wardterm imports the file instead of making you retype it. On Mac, choose ~/.ssh/config under Import; on iPhone and iPad, pick the file with SSH config file. Each Host becomes a server with its address, port, user and key. ProxyJump becomes the chain of jump servers, LocalForward, RemoteForward and DynamicForward become saved tunnels, and Include files and Host * options are read too. See Import hosts for every field.

With sync on, the same servers and keys reach your other devices in the encrypted vault, so the import happens once.

Your Host blocks become servers in Wardterm, with jump hosts and tunnels ready, on Mac, Windows, Linux, iPhone, iPad and Android. Start free.

Originally published on the Wardterm blog.

Top comments (0)