SSH config examples: 12 blocks ready to copy

These SSH config examples go in ~/.ssh/config (on Windows %USERPROFILE%\.ssh\config): each Host block gives a server a short name with its HostName, User, Port and IdentityFile, so ssh web replaces a long command. SSH uses the first value it finds for each setting, so put specific hosts first and Host * last. Check any block with ssh -G name before you connect.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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) '

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

ssh -F ~/test-config -G web

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.

Questions

Where is the SSH config file location?

In ~/.ssh/config on macOS and Linux, and %USERPROFILE%\.ssh\config on Windows. It has no extension and does not exist until you create it. The system-wide file is /etc/ssh/ssh_config.

How do I set up SSH config for multiple hosts?

Write one Host block per server, each with its own HostName, User and key. For servers that share settings, use a pattern such as Host *.prod.example.com, or list several names on one line: Host app1 app2.

How do I use SSH config with multiple GitHub accounts?

Give the second account its own alias, such as Host github-work with HostName github.com, User git, its own IdentityFile and IdentitiesOnly yes. Then clone with git@github-work:company/repo.git.

What does IdentitiesOnly yes do?

It makes SSH offer only the keys named in the config, not every key in the agent. Without it, a server can refuse you with Too many authentication failures, or GitHub can sign you in as the wrong account.

How do I use ProxyJump in SSH config?

Add ProxyJump bastion to the block of the private server, where bastion is another Host in the same file. Several jumps go in order, separated by commas: ProxyJump hop1,hop2.

What is a good SSH config keepalive setting?

ServerAliveInterval 30 with ServerAliveCountMax 3. SSH sends a small message every 30 seconds, which keeps routers from dropping idle sessions, and closes the connection after about 90 seconds without an answer.

Why does my SSH config not work?

Usually a setting earlier in the file wins over yours, or the name you type does not match the Host line. ssh -G name shows the values SSH really uses. A typo in a keyword stops SSH with Bad configuration option and the line number.

Do scp, rsync and git use the SSH config?

Yes. They all run OpenSSH, so scp file.txt web:, rsync -avz site/ web:/var/www/ and git clone git@github-work:org/repo.git use the matching block.

Sources

Each page was read on the date shown. Prices and features change, so check the maker's page before you decide.

  1. ssh_config(5), OpenBSD manual pages, read on
  2. ssh(1), OpenBSD manual pages, read on
  3. OpenSSH release notes, read on
  4. OpenSSH Server configuration for Windows, Microsoft Learn, read on
  5. Testing your SSH connection, GitHub Docs, read on
  6. Wardterm docs, import hosts, read on

All posts