When you manage more than one server or SSH key, a connection can fail even though the right private key is on your machine. OpenSSH may be using a different identity, or the host settings you expected may not match the destination.
IdentityFile lets you associate a private key with a host in your SSH client configuration. Once set up, you can connect with a short alias instead of repeating -i, the username, and the server address each time.
Turn a working command into a host alias
Start with a connection that already works. For example:
ssh -i ~/.ssh/id_ed25519_prod deploy@server.example.com Add a matching host block to ~/.ssh/config:
Host production HostName server.example.com User deploy IdentityFile ~/.ssh/id_ed25519_prod IdentitiesOnly yes Now connect with:
ssh production Host is the shortcut you type; HostName is the actual server address. The block applies when you connect using the matching alias. If you instead run ssh server.example.com, the Host production block does not match.
IdentityFile normally points to the private key, not its .pub file. Keep the private key on your own machine and do not share its contents. The corresponding public key must be authorized for the remote account.
For more examples of aliases, ports, and other per-host settings, see this collection of practical SSH config patterns.
Keep unrelated agent keys out of the connection
A common surprise: setting IdentityFile does not necessarily mean SSH will offer only that identity. The SSH agent may have other keys loaded, too.
IdentitiesOnly yes tells SSH to use configured identities rather than offering additional identities just because they are available from the agent. It does not turn off the agent: the agent can still help authenticate a configured identity.
Host production HostName server.example.com User deploy IdentityFile ~/.ssh/id_ed25519_prod IdentitiesOnly yes If you’re unsure what your agent contains, ssh-add -l lists its identities when an agent is available. For a deeper explanation of loading and inspecting keys, see the guide to using ssh-agent across operating systems.
Use IdentityAgent none only when you specifically need to disable agent use for that connection. It is different from IdentitiesOnly yes.
Verify the settings before debugging the server
First, ask OpenSSH what configuration it resolved for the alias:
ssh -G production | grep -E '^(hostname|user|port|identityfile|identitiesonly) ' ssh -G prints the effective settings and exits; it does not connect to the server. Check that the hostname, user, key path, and identitiesonly value are what you intended. If there are multiple identityfile lines, more than one identity is configured.
Then inspect an actual connection attempt:
ssh -v production Look for Offering public key: and check that the path refers to the expected key. If login succeeds, the debug output also reports public-key authentication. The exact wording can vary between OpenSSH versions.
A key passphrase prompt is not the same as a request for the remote account password: the passphrase unlocks your local private key.
When a config entry seems ignored
Check these in order:
- Does the host block match? Connect with the alias named after
Host, or adjust the pattern to match the name you actually use. - Does the file exist? Verify the key path rather than changing it to an example filename. A path in
IdentityFiledoes not create a key. - Are other blocks adding identities? Matching
Host,Match, andIncludeconfiguration can affect the result. In particular, repeatedIdentityFiledirectives are cumulative; a generalHost *entry can add another key. - Is the intended public key authorized on the server? If verbose output shows the expected key being offered but authentication fails, the path may be correct. Check the remote username and whether its public key is installed in
authorized_keys.
For most scalar SSH settings, the first value obtained takes precedence, so putting specific host blocks before general defaults is a useful pattern. IdentityFile is an important exception because multiple entries can accumulate. Use ssh -G to see the result instead of guessing.
Use -i for a quick test
For an occasional connection, the command-line option is convenient:
ssh -i ~/.ssh/id_ed25519_prod deploy@server.example.com For repeat connections, a host block makes the choice reusable. If ssh -i works but the alias does not, compare the resolved alias settings with ssh -G before changing server-side SSH configuration.
On Linux and macOS, the per-user config is usually ~/.ssh/config. On Windows OpenSSH, it is typically %USERPROFILE%\.ssh\config; in PowerShell, $HOME\.ssh\config is a convenient path. Make sure the file is named config, not config.txt.
On Linux and macOS, the SSH directory is commonly restricted with chmod 700 ~/.ssh, and the config and private key with chmod 600 when needed. Windows manages file access using Windows ACLs rather than Unix chmod.
The goal is to make key selection predictable: use a host-specific IdentityFile, add IdentitiesOnly yes when unrelated agent keys are getting in the way, and confirm the result with ssh -G and ssh -v.
I originally published a more detailed version of this guide on the ShadeTerm blog.
I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
