SSH Keys, the Agent, and a Safe SSH Config
November 2, 2026
I recently got a Permission denied (publickey) error pushing to a Git server. The key was on disk. It had worked a few weeks earlier. The cause was a key with a custom file name, no entry for the host in ~/.ssh/config, and an agent that had been cleared by a reboot.
This made me want to understand this process better. So I wrote this article about how SSH picks a key, what the agent does, and the SSH config settings.
The Order SSH Tries Keys
When you connect, the client builds a list of keys and offers them to the server one at a time until one is accepted. In OpenSSH the order is:
- Keys named by
IdentityFilethat are already loaded in the agent. - Every other key in the agent.
IdentitiesOnly yesskips this step. - Keys named by
IdentityFilethat are not in the agent, loaded from disk. If the key has a passphrase, you are prompted here.
If a host has no IdentityFile, SSH uses the default names: ~/.ssh/id_ed25519, ~/.ssh/id_ecdsa, ~/.ssh/id_rsa and a few others. A key named something like ~/.ssh/github is never tried automatically. It has to be named in the config or loaded into the agent.
Step 2 is where problems start. Without IdentitiesOnly, SSH offers every key in the agent to every server. Servers limit failed attempts (MaxAuthTries, default 6), so with enough keys loaded you get Too many authentication failures before the right key is offered. It also tells each server about every public key you hold.
To see what is actually offered:
ssh -v -T git@github.com 2>&1 | grep -E "Offering|Server accepts|Authenticated"
What the Agent Is
ssh-agent is a small background process that holds your decrypted private keys in memory. When SSH needs to authenticate, it asks the agent to sign a challenge. The private key never leaves the agent. Programs find the agent through the SSH_AUTH_SOCK environment variable, which points to a Unix socket.
The point of the agent is that you type a key’s passphrase once per session instead of on every connection. On macOS the agent is started for you by launchd. On Linux your desktop session or a line in your shell profile usually starts it.
Useful commands:
ssh-add -l # list loaded keys
ssh-add ~/.ssh/github # load a key
ssh-add -t 4h ~/.ssh/github # load a key for 4 hours
ssh-add -d ~/.ssh/github # remove one key
ssh-add -D # remove all keys
When to Use It
- Your keys have passphrases (they should), and you connect often: Git pushes, jumping between servers you trust.
- On your own workstation, where you are the only user.
When Not to Use It
- On a shared or untrusted server. Anyone with root on that machine can use the agent’s socket and sign as you while your keys are loaded. They can’t copy the key, but they don’t need to.
- Forwarding it to remote hosts by default.
ForwardAgent yesexposes your local agent to every server you log in to. I wrote about this in Using SSH Agent Forwarding. UseProxyJumpinstead where you can. - For automation. Scripts and CI should use their own dedicated, limited keys, not your personal agent.
Protecting Keys in the Agent
A loaded key can be used by anything running as your user, without a passphrase. A few ways to limit that:
- Put a passphrase on every key. The agent then protects the key in memory, and the passphrase protects it on disk. A key with no passphrase is readable by anyone who gets a copy of the file.
- Set a lifetime.
ssh-add -t 4horAddKeysToAgent 4hin the config removes the key after four hours. - Require confirmation.
ssh-add -corAddKeysToAgent confirmasks you to approve each use of the key. You need anssh-askpassprogram installed for the prompt. - Lock the agent when you step away:
ssh-add -xlocks it with a password,ssh-add -Xunlocks it. - Use one key per service. If one key leaks, you only revoke that one.
- Use a hardware key (YubiKey,
ed25519-skkeys) for the most important access. The private key can’t leave the device, and each use needs a touch.
A Safe ~/.ssh/config
SSH uses the first value it finds for each setting, reading the file from top to bottom. Put specific Host blocks first and the Host * defaults at the bottom.
Host github.com
User git
IdentityFile ~/.ssh/github
IdentitiesOnly yes
Host git.drupal.org
User git
IdentityFile ~/.ssh/drupal-gitlab
IdentitiesOnly yes
# Defaults for every host. Keep this block last.
Host *
AddKeysToAgent yes
ForwardAgent no
IgnoreUnknown UseKeychain
UseKeychain yes
What each setting does:
IdentityFile: the key to use for this host.IdentitiesOnly yes: offer only that key, never everything in the agent. I set it per host rather than inHost *, because some tools (Colima, Vagrant) generate their own SSH config and may rely on agent keys.AddKeysToAgent yes: the first time a key is loaded from disk (step 3 above), it is added to the agent. You type the passphrase once after a reboot, and later connections use the agent. Useconfirmor a time like4hinstead ofyesfor the protections above.ForwardAgent no: this is already the default. Writing it down makes the choice explicit. Enable it only in the oneHostblock that needs it.UseKeychain yes: macOS only. Stores the key’s passphrase in the Keychain, so the key loads after a reboot without a prompt.IgnoreUnknown UseKeychain: Linux OpenSSH doesn’t knowUseKeychainand refuses to read the config without this line. With it, the same file works on both.
Also check the file permissions. SSH refuses keys that other users can read:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/github ~/.ssh/drupal-gitlab
Testing Without Clearing the Agent
To check that the config works by itself, without removing the keys from your agent, ignore the agent for one connection:
ssh -o IdentityAgent=none -T git@github.com
If that succeeds, the IdentityFile line is doing its job, and the setup will still work after a reboot.
Summary
- SSH tries agent keys first, then keys from disk. Custom key names are only used if the config names them.
IdentitiesOnly yesper host stops SSH from offering every key you have.- The agent holds decrypted keys. Keep it on machines you control, don’t forward it by default, and limit loaded keys with passphrases, lifetimes or confirmation.
