IdentityFile tells the OpenSSH client which SSH identity to use when connecting to a host. Add it to ~/.ssh/config to stop typing ssh -i /path/to/key for every connection—especially when you manage more than one private key.
Already have a working ssh -i ~/.ssh/mykey user@host command? The steps below show how to turn it into a reusable alias, inspect the effective config, and verify which key SSH actually offers.
What does IdentityFile do in SSH?
IdentityFile is a client-side OpenSSH directive that identifies the authentication key for a connection. Usually you point it at a private key, such as ~/.ssh/id_ed25519 or ~/.ssh/id_ed25519_prod. The matching public key must already be authorized for the target account on the server.
Without an explicit selection, OpenSSH looks for default identity filenames and may offer identities from ssh-agent. A key with a custom filename is not necessarily selected automatically. This is why a direct command with -i can succeed while a plain ssh user@host asks for a password.
| File / setting | Purpose |
|---|---|
~/.ssh/config | Per-user SSH client settings, including Host, User and IdentityFile. |
~/.ssh/id_ed25519_prod | Your private key: keep it on your local machine and do not share it. |
~/.ssh/id_ed25519_prod.pub | The corresponding public key, typically installed in the remote user's authorized_keys. |
/etc/ssh/sshd_config | Server-side SSH daemon settings; IdentityFile does not belong here. |
If you need a new key pair, see how to generate an SSH key. If your public key is not installed on the server, see ssh-copy-id.
How to set IdentityFile in ~/.ssh/config (Linux and macOS)
These steps assume you already have a working private key on your computer. If ssh -i ~/.ssh/id_ed25519_prod [email protected] works, you can use that same key in a host block.
- 1Check the key filename. Run
ls -la ~/.sshand look for your private key. Do not open or paste the private key contents into a public page or screenshot. - 2Create the SSH folder if needed. Run
mkdir -p ~/.ssh && chmod 700 ~/.ssh, then open the config withnano ~/.ssh/config(or your preferred editor). - 3Add a per-host block. Replace the example hostname, user and key path with your own values.
Host production
HostName server.example.com
User deploy
Port 22
IdentityFile ~/.ssh/id_ed25519_prod
IdentitiesOnly yes
IdentityFile entry binds the host alias to the intended private key; IdentitiesOnly yes limits unrelated agent-offered identities.- 4Save the file and restrict permissions. Run
chmod 600 ~/.ssh/configand, for a locally stored private key,chmod 600 ~/.ssh/id_ed25519_prodif needed. - 5Connect using the alias. Run
ssh production; there is no need to repeat-i, username or port.
Here, production is only the shortcut you type, while HostName is the real address. If you use the real hostname instead of ssh production, an alias-only Host production block will not match.
IdentityFile should point at an actual file. A path like ~/.ssh/id_ed25519_prod is not a command to generate a key. Do not replace an existing key just to match an example; use your existing key's real filename.
Verify the selected key with ssh -G and ssh -v
A config file that looks right is not proof it is being used. Run these two checks in order: first resolve the settings without connecting, then inspect the authentication exchange.
Step 1: print the effective configuration
ssh -G production | grep -E '^(hostname|user|port|identityfile|identitiesonly) 'ssh -G evaluates matching Host and Match blocks, prints the resulting values and exits. It does not connect to the server. Check that the output includes your intended identityfile path. If there are multiple identityfile lines, SSH has more than one configured identity.
# Example output — values depend on your own config
user deploy
hostname server.example.com
port 22
identityfile ~/.ssh/id_ed25519_prod
identitiesonly yes
ssh -G prints the settings applied to the alias without opening a network connection.Step 2: inspect the key offered during authentication
ssh -v productionLook for a line containing Offering public key: followed by the expected key path, and—if login succeeds—a line containing Authenticated to with publickey. The wording varies by OpenSSH version. If SSH asks for a key passphrase, that is different from the remote account password.
# Illustrative debug messages, not a real captured session
debug1: Offering public key: /home/alex/.ssh/id_ed25519_prod ED25519 ...
Authenticated to server.example.com (...) using "publickey".
ssh -v reports offered keys and the authentication method used when the connection succeeds.To see why a connection failed, run ssh -vvv production. Unlike ssh -G, verbose mode attempts the actual connection and helps distinguish key selection from server-side rejection.
IdentityFile vs. ssh -i: which should you use?
Both select an SSH identity; they differ mainly in where you configure it. Neither command by itself guarantees that unrelated agent identities will never be offered.
| Method | Example | Best use |
|---|---|---|
ssh -i | ssh -i ~/.ssh/prod_key [email protected] | One-time connections and quick tests. |
IdentityFile | IdentityFile ~/.ssh/prod_key | Reusable settings in a matching Host block. |
IdentitiesOnly yes | ssh -i ~/.ssh/prod_key -o IdentitiesOnly=yes [email protected] | Limit offered identities to those configured, including those passed on the command line. |
If ssh -i works but the alias does not, inspect the alias with ssh -G rather than changing server settings immediately. For the one-off syntax, read how to SSH with a private key.
Use different SSH keys for different servers or GitHub accounts
One key per server
Create separate Host blocks so you do not need to remember which -i argument belongs to each destination:
Host personal-server
HostName 203.0.113.20
User alex
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host work-server
HostName 198.51.100.25
User deploy
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesUse ssh personal-server or ssh work-server to choose the matching settings.
Two GitHub accounts, two SSH aliases
Host github-personal
HostName 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 yesThe SSH-style remote git@github-work:organization/repository.git uses the work alias and key. You can check the account association with ssh -T github-work (GitHub's successful authentication message does not provide a shell).
Can one host have more than one IdentityFile?
Yes. Repeated IdentityFile directives are cumulative: OpenSSH adds them to the candidate list rather than replacing an earlier entry. This can help during migration, but if the server only accepts one identity, extra attempts can contribute to an authentication-attempt limit.
Host legacy
HostName legacy.example.com
User admin
IdentityFile ~/.ssh/id_ed25519
IdentityFile ~/.ssh/id_rsa_legacy
IdentitiesOnly yesIdentitiesOnly yes restricts extra agent identities; it does not remove the second key you explicitly configured.
IdentityFile vs. IdentitiesOnly vs. ssh-agent
Specifying IdentityFile does not necessarily prevent OpenSSH from offering additional identities loaded into ssh-agent. That matters when several accounts or developer tools have registered keys with the same agent.
Host production
HostName server.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_prod
IdentitiesOnly yesWith IdentitiesOnly yes, SSH considers the configured authentication identities and certificates rather than offering every extra identity made available by the agent, a PKCS#11 provider or a security-key provider. The agent can still sign authentication requests for a configured identity.
| Option | What it changes |
|---|---|
IdentityFile | Adds a key identity for this connection. |
IdentitiesOnly yes | Restricts authentication to configured identities rather than extra agent-offered identities. |
IdentityAgent none | Disables use of an SSH authentication agent for that connection; local private-key files can still be used. |
ssh-add -l | Lists identities currently held by your agent, when one is available. |
For a deeper explanation, see our ssh-agent guide and Too many authentication failures troubleshooting checks.
Why Host * or another config file may add the wrong key
OpenSSH reads command-line options and configuration files, then applies matching Host blocks. For most scalar options, the first obtained value wins, so put specific host blocks before a general Host * block. IdentityFile is an exception: multiple declarations can accumulate across matching blocks.
# Specific host first
Host production
HostName server.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_prod
IdentitiesOnly yes
# General defaults last
Host *
ServerAliveInterval 30Be careful about putting IdentityFile ~/.ssh/shared_key in Host *: it can add another identity to a more specific host's list even if you already configured IdentityFile ~/.ssh/id_ed25519_prod. Inspect the result with ssh -G production.
If the result is unexpected, check for matching Include files, other Host or Match blocks, and any -F custom-config option. Do not assume that changing a single IdentityFile line resets other matching entries.
How to set IdentityFile on Windows (PowerShell)
The built-in OpenSSH client on Windows reads its per-user settings from %USERPROFILE%\.ssh\config. On PowerShell, $HOME\.ssh\config refers to the same location for a typical interactive user.
Step 1: create and open the client config
New-Item -ItemType Directory -Force "$HOME\.ssh" | Out-Null
notepad "$HOME\.ssh\config"If the file does not exist, Notepad can create it. Make sure the filename is exactly config, not config.txt.
Step 2: add the host entry
Host production
HostName server.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_prod
IdentitiesOnly yesUsing ~/.ssh/... avoids hard-coding a Windows username. If you prefer an absolute path, forward slashes are convenient: IdentityFile C:/Users/Alex/.ssh/id_ed25519_prod. Quote paths that contain spaces.
Step 3: check the file and resolved configuration
Test-Path "$HOME\.ssh\id_ed25519_prod"
ssh -G production | Select-String '^(hostname|user|identityfile|identitiesonly) '
ssh production
Test-Path checks whether the local key file exists; ssh -G reports the resolved identity setting.The first command should return True if the private key file is stored there. If the connection fails, run ssh -v production in PowerShell and inspect which key is offered. Windows file access permissions are managed with Windows ACLs rather than Unix chmod.
Troubleshooting: IdentityFile is ignored or SSH still asks for a password
| Symptom | Check first | Likely fix |
|---|---|---|
ssh -i ... works, but ssh production prompts for a password. | ssh -G production and the alias in Host. | Match the correct host block, username and key path; add IdentitiesOnly yes if agent keys interfere. |
Warning: Identity file ... not accessible | Check that the key file actually exists at that path. | Fix the filename, expansion or path; do not point at a directory. |
Too many authentication failures | ssh-add -l, ssh -v production. | Specify the intended identity with IdentitiesOnly yes; remove unneeded entries from matching config blocks. |
WARNING: UNPROTECTED PRIVATE KEY FILE! | Permissions on the local private key. | On Linux/macOS, set an appropriate owner-only mode (commonly chmod 600). |
Permission denied (publickey) | Verbose log plus remote user's authorized_keys. | Confirm correct remote username, matching public key and server-side authorization. |
Unexpected extra identityfile entries. | Matching Host *, Include and Match blocks. | Remove unintended cumulative IdentityFile entries; verify again with ssh -G. |
Fast, repeatable diagnostic sequence
# 1. Does SSH resolve the correct config?
ssh -G production | grep -E '^(hostname|user|identityfile|identitiesonly) '
# 2. Does the private key exist?
ls -l ~/.ssh/id_ed25519_prod
# 3. Which public key is offered, and is it accepted?
ssh -vvv productionIf the expected key is offered but rejected, stop changing the IdentityFile path and investigate authorization on the remote server. See SSH Permission Denied (publickey) and authorized_keys.
IdentityFile quick reference
| Goal | Directive or command |
|---|---|
| Use a key for one host | IdentityFile ~/.ssh/id_ed25519_prod |
| Connect with a shortcut | ssh production |
| Limit extra agent identities | IdentitiesOnly yes |
| Use a key for a one-off command | ssh -i ~/.ssh/id_ed25519_prod user@host |
| Inspect resolved settings | ssh -G production |
| See offered keys and authentication errors | ssh -vvv production |
| Disable agent use for a host | IdentityAgent none |
| Suppress identity-file loading (advanced) | IdentityFile none (does not automatically disable the agent) |
SSH IdentityFile FAQ
Does IdentityFile point to the public or private key?
Normally the private key. OpenSSH can also use a public-key file as an identity reference when the corresponding private key is available through an agent; that is an advanced use case, not the standard starting point.
Can I put more than one IdentityFile in SSH config?
Yes. Unlike most client settings, multiple IdentityFile entries add identities to the candidate list. IdentitiesOnly yes does not collapse the list to one file.
Does IdentityFile override ssh-agent?
No. IdentitiesOnly yes prevents unrelated agent identities from being offered, while the agent can still help authenticate a configured identity. Use IdentityAgent none only if you actually need to turn off agent use for that host.
Why does SSH ask for a password if IdentityFile is set?
The Host block might not match, the private key might not be found or loaded, or the server might reject its matching public key. Check ssh -G first, then use ssh -v to locate the failed step.
Can I use ~ and spaces in IdentityFile paths?
Yes: IdentityFile ~/.ssh/id_ed25519 is valid. For paths containing spaces, quote the argument, such as IdentityFile "C:/Users/Alex Smith/.ssh/id_ed25519".
Where is SSH IdentityFile configured on Windows?
For the Windows OpenSSH client, use %USERPROFILE%\.ssh\config (typically C:\Users\YourName\.ssh\config). It is separate from the OpenSSH server's sshd_config.
Does IdentityFile none disable all public-key authentication?
No. The directive disables loading identity files; it does not necessarily suppress agent-provided identities. It is an advanced setting and should not be confused with IdentitiesOnly yes or IdentityAgent none.
Technical references
This guide follows the OpenSSH client manuals and Microsoft's documentation for Windows OpenSSH configuration:
- OpenBSD: ssh_config(5) —
IdentityFile,IdentitiesOnly,IdentityAgentand matching rules. - OpenBSD: ssh(1) —
-i,-Gand-vflags. - Microsoft Learn: OpenSSH configuration for Windows — Windows client config locations.
Make SSH key selection predictable
For a one-time login, ssh -i is convenient. For repeat connections, set IdentityFile in a per-host SSH config block. Pair it with IdentitiesOnly yes when your agent has other identities, and verify the actual settings with ssh -G before debugging with ssh -v.
If you regularly manage multiple servers, ShadeTerm brings terminal sessions, SFTP, code editing and server monitoring together in one desktop workspace.
