Safeguard
Security Guides•

Why does ssh offer all your keys to every server?

With no IdentityFile set, ssh tries default keys and your agent keys. IdentitiesOnly limits it to the key you name. Config example and steps inside.

Safeguard Security Team
3 min read

Because that is the default. If a host has no IdentityFile configured, ssh tries your default key files and also any identities held by your ssh-agent. Setting IdentitiesOnly yes with an explicit IdentityFile for each host makes ssh offer only the key you chose.

What the manual says

The ssh_config manual says that without an IdentityFile option, ssh tries default key files such as ~/.ssh/id_rsa, ~/.ssh/id_ecdsa and ~/.ssh/id_ed25519. It also says identities held by the authentication agent are used too, unless IdentitiesOnly is set. IdentitiesOnly accepts yes or no, defaults to no, and restricts ssh to the configured identity and certificate files, even when the agent offers more. Repeating IdentityFile adds to the list of identities tried.

Why this matters

Each key you offer tells the server something: that a key with that fingerprint exists on your machine. A server you do not fully trust learns your public keys, which can link your accounts across services. Many servers also limit failed attempts, so offering many keys first can lock you out before the right one is tried. The more serious point is the neighbor risk covered in is ssh agent forwarding safe: the fewer places your agent is usable, the better.

A host block that behaves

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

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

You then clone with the alias, for example git clone git@git-work:org/repo.git. Each alias offers exactly one key.

Steps

  1. List what your agent holds with ssh-add -l.
  2. Run ssh -v host and read which identities are offered, in order.
  3. Add a Host block per service or account with an explicit IdentityFile.
  4. Add IdentitiesOnly yes to each block.
  5. Retest with ssh -v and confirm only one key is offered.
  6. Use a passphrase on each key. See ssh key passphrase and ssh-agent.

Common mistakes

MistakeEffect
Setting IdentityFile without IdentitiesOnlyAgent keys are still offered
One shared key for every serviceOne theft opens everything
Wildcard Host * with a personal keyEvery server sees it
Forgetting IdentityFile none for hosts that need password or other authDefault keys still tried

Safeguard scans repositories and dependencies; for the code you pull over ssh, a free scan is at gold.safeguard.sh/scan.

Quick check

Does IdentitiesOnly stop the agent from running?

No. The agent keeps running. ssh simply ignores agent keys that are not the configured identity files for that host.

Should I use one key per service?

It limits the damage from one stolen key and keeps accounts unlinkable, at the cost of a few extra config lines.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.