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 - A short name for a server
- A key per server
- Many servers with one pattern
- Two GitHub accounts
- A jump host
- Saved tunnels
- Keepalive
- One connection reused
- Passphrases in the macOS Keychain
- tmux on every connection
- Old routers and switches
- 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.
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 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:
-
Includegoes at the top. Placed after aHostline, it belongs to that block. A relative path likeconfig.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
IdentityFileinHost *. 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.
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.
