Sub topic 10
Simplifying connections with ~/.ssh/config
This chapter assumes you can already log in with a key pair, as set up in the earlier chapters. You should have a working ~/.ssh/id_ed25519 key and an account on a remote Linux server. You should also be comfortable editing a text file with nano or vim and reading basic Unix permission bits.
By now a login command can get long: ssh -i ~/.ssh/id_ed25519 -p 2222 [email protected]. The client configuration file lets you replace that with ssh myserver. It also controls which keys ssh offers, which matters more as your key collection grows.
What the file does
When you run ssh, the client collects settings from three places, in this order:
- Options on the command line, such as
-p 2222or-o User=bob. - Your per-user file,
~/.ssh/config. - The system-wide file,
/etc/ssh/ssh_config.
For each setting, ssh uses the first value it finds. Later sources never override earlier ones. This one rule explains most of the surprises people hit with the file, and the "Ordering rules" section below returns to it.
The same file is read by scp, sftp, rsync -e ssh, and Git over SSH. A host alias you define once works in all of them.
Creating the file
The file may not exist yet. Create it with private permissions:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/configssh refuses to use a config file that other users can write. If you see Bad owner or permissions on /home/alice/.ssh/config, run chmod 600 ~/.ssh/config and check that you own the file.
A basic host entry
The file is a series of blocks. Each block starts with a Host line, and the indented lines under it apply only to connections whose name matches. Indentation is optional but makes the file readable.
Host myserver
HostName server.example.com
User alice
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesWith this block, ssh myserver connects to server.example.com as alice using your Ed25519 key.
Hostis the alias you type. It is only a label on your machine and does not need to exist in DNS.HostNameis the real name or IP address to connect to. If you leave it out, ssh uses the alias itself.Useris the remote account name. Without it, ssh uses your local username.IdentityFilenames a private key to try. You can repeat the line to name several keys.IdentitiesOnly yestells ssh to offer only the keys named in the config (or on the command line with-i). Without it, ssh also offers every key loaded in your ssh-agent.
The last option is the one people tend to omit. A server accepts only a few failed attempts per connection, and MaxAuthTries defaults to 6. If your agent holds many keys, ssh may use up those attempts before it reaches the right key. The server then drops the connection with Too many authentication failures. IdentitiesOnly yes prevents this.
Other options you will use
| Option | Purpose | Example |
|---|---|---|
| `Port` | Connect to a non-default SSH port | `Port 2222` |
| `ProxyJump` | Reach a host through a bastion (jump) host | `ProxyJump bastion` |
| `ServerAliveInterval` | Send a keepalive after this many idle seconds, to stop NAT or firewalls dropping quiet sessions | `ServerAliveInterval 60` |
| `ServerAliveCountMax` | Give up after this many unanswered keepalives | `ServerAliveCountMax 3` |
| `AddKeysToAgent` | Add a key to ssh-agent the first time you unlock it | `AddKeysToAgent yes` |
| `ForwardAgent` | Let the remote host use your local agent | `ForwardAgent no` |
| `Include` | Read more config files | `Include ~/.ssh/config.d/*` |
ProxyJump and Include need OpenSSH 7.3 or later, and AddKeysToAgent needs 7.2 or later. Run ssh -V if you are unsure which version you have.
On macOS, you can add UseKeychain yes to store the passphrase in the system keychain. Other platforms do not know this option and reject the file. If you share one config across systems, add IgnoreUnknown UseKeychain above it so non-macOS clients skip it.
A realistic multi-host file
This file defines a bastion host, a private database server reached through it, and defaults for everything else.
Host bastion
HostName bastion.example.com
User alice
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host db
HostName 10.0.12.5
User dbadmin
Port 2222
ProxyJump bastion
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Host *.example.com
User alice
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yesRunning ssh db now opens a connection to the bastion, then tunnels through it to 10.0.12.5 on port 2222. You never type either hostname. The bastion authenticates you with the same key, because ProxyJump bastion looks up the bastion block as well.
Ordering rules
A Host line takes one or more patterns. * matches any characters, ? matches one character, and a leading ! excludes a match. A connection can match several blocks, and ssh applies all of them. For each option, the first value read wins.
Put specific blocks first and Host * last. In the example above, a connection to bastion.example.com matches both Host *.example.com and Host *, and receives options from each.
Here is a mistake that looks harmless:
Host *
User bob
Host myserver
HostName server.example.com
User aliceHost * is read first and sets User bob. The later User alice is ignored, so ssh myserver tries to log in as bob. Moving Host * to the bottom fixes it.
A Host pattern is matched against the name you typed on the command line, not against the HostName value. Typing ssh server.example.com does not trigger the Host myserver block. If you want it to, use Host myserver server.example.com, which lists two patterns on one line.
Checking what ssh will actually do
You do not need to guess which blocks applied. ssh -G prints the final settings for a host and exits without connecting:
ssh -G myserver | grep -E '^(hostname|user|port|identityfile|identitiesonly) 'The output uses lowercase option names and shows expanded paths:
user alice
hostname server.example.com
port 22
identitiesonly yes
identityfile /home/alice/.ssh/id_ed25519If user shows the wrong name, a block earlier in the file is setting it first. Compare this output with the ssh -v output from the troubleshooting chapter. The -v lines identity file ... type and Offering public key show the keys ssh actually tried.
To test a different file without touching your real one, use ssh -F /path/to/testconfig myserver.
Using more than one key
Different services often need different keys, for example one for your servers and one for a Git host. Give each host its own block and name one key per block:
Host github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
IdentitiesOnly yes
Host myserver
HostName server.example.com
User alice
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesThe two blocks keep the keys separate. A compromised or retired key then affects only one service, and no host learns which other keys you hold. That is the reason to use IdentitiesOnly yes here and not only for the attempt limit.
Keeping the file manageable
Once the file grows past a screen or two, split it up. Put this line at the very top, before any Host or Match line, because an Include that follows a Host block is treated as part of that block:
Include ~/.ssh/config.d/*Then keep one file per project or employer inside ~/.ssh/config.d/. Included files follow the same first-value-wins rule, in the order they are read. Each one needs the same private permissions as the main file.
Comments start with #. Note which key belongs to which service, because six months later the key filename alone will not remind you.
Common errors
Bad owner or permissions: the file is writable by group or others. Runchmod 600 ~/.ssh/config.Bad configuration option: usekeychain: a Linux client read an option that only macOS understands. AddIgnoreUnknown UseKeychainbefore it.- Wrong user or port despite your block: an earlier block, often
Host *, set the value first. Runssh -Gto see which value won. - The alias is not recognized by another tool: some programs do not use the OpenSSH client. They ignore your config file entirely.
Too many authentication failures: addIdentitiesOnly yesto the host block, or clear stray keys from the agent withssh-add -D.- A typo in an option name: ssh stops with
Bad configuration optionand the line number. Fix that line and try again.
Exercise
You wrote this config, but ssh web tries to log in as deploy instead of alice. What is wrong, and how do you confirm it?
Host *
User deploy
Host web
HostName web.example.com
User alice
Show answer
The Host * block comes first, and ssh uses the first value it finds for each option, so User deploy wins. Move Host * below the Host web block (or remove its User line). Confirm with ssh -G web | grep '^user ', which prints the effective user before and after the change.
Exercise
Write a ~/.ssh/config block so that ssh app connects to 10.0.5.20 as deploy on port 2200, through the existing bastion host, offering only ~/.ssh/id_ed25519_work.
Show answer
Host app HostName 10.0.5.20 User deploy Port 2200 ProxyJump bastion IdentityFile ~/.ssh/ided25519work IdentitiesOnly yes