Can you have multiple SSH keys? How to use more than one

Yes, you can keep several SSH keys. Learn how to name them, pick the right one per server with ~/.ssh/config, and fix too many authentication failures.

6–8 minutes
Desk with a monitor showing code and small plants

Yes, you can have as many SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → keys as you like, on your computer and on a server. This guide is for anyone who wants separate keys for work and personal use, for two GitHub accounts, or for different servers, and needs SSH to pick the right one every time.

You can keep any number of SSH key pairs in your .ssh folder, as long as each has its own file name. Create each one with ssh-keygen -f ~/.ssh/name, then tell SSH which key belongs to which server with a Host block in **~/.ssh/config using IdentityFile and IdentitiesOnly yes**. A server can also accept several keys at once: add one public key per line to authorized_keys.

Why you might want more than one key

A single key works for every server and service, and for many people that is enough. Separate keys are useful when:

  • You have two accounts on the same service. GitHub, for example, will not let you add the same public key to two accounts, so a personal account and a work account each need their own key.
  • You want to limit the damage of a lost key. If the key for a client’s server leaks, you replace that one key without touching your other logins.
  • Someone else controls part of your access. An employer may ask you to use a key they can revoke without affecting your personal servers.
  • A script or deploy process needs its own key. A key used by an automated job, often without a passphrase, should not be the same key you use to log in as an administrator.

If SSH keys are new to you, start with that guide, then come back here.

How SSH chooses a key by default

When you run ssh without saying which key to use, the client tries two sources:

  1. Every key loaded into ssh-agent, the background program that holds unlocked keys in memory.
  2. Key files in ~/.ssh/ with the default names, such as id_ed25519, id_ecdsa and id_rsa.

It offers them to the server one at a time until one is accepted. Any key with a custom name, such as ~/.ssh/work_ed25519, is not tried automatically unless it is loaded into the agent or named in your config. That is why a new key “does nothing” until you tell SSH about it.

You can watch this happen with verbose output:

ssh -v exampleco@203.0.113.10

Look for lines starting Offering public key and the one that ends in Server accepts key. That shows which key actually logged you in.

Step by step: set up a second key

1. Create the key with its own file name

The -f option sets the file name, so you do not overwrite your existing id_ed25519. The -C comment is just a label to help you recognise the key later.

ssh-keygen -t ed25519 -f ~/.ssh/work_ed25519 -C "work laptop"

On Windows, run the same command in PowerShell. ~ points to C:\Users\yourname, so the files end up in C:\Users\yourname\.ssh\.

You now have work_ed25519 (the private key, never share it) and work_ed25519.pub (the public key). The guide on where SSH keys are stored explains the other files in this folder.

If ssh-keygen asks whether to overwrite an existing file, answer n. Overwriting a key you still use will lock you out of every server that trusted it.

2. Put the public key where it is needed

Add work_ed25519.pub to the server or service. For a Linux server you can use ssh-copy-id and name the key explicitly with -i:

ssh-copy-id -i ~/.ssh/work_ed25519.pub exampleco@203.0.113.10

For GitHub, paste the contents of the .pub file into your account settings. How to copy SSH keys covers PowerShell and clipboard methods.

3. Tell SSH which key to use for each host

Create or edit the file ~/.ssh/config (on Windows, C:\Users\yourname\.ssh\config, with no file extension). Each Host block is a set of rules for one name:

Host example-prod
    HostName 203.0.113.10
    User exampleco
    IdentityFile ~/.ssh/work_ed25519
    IdentitiesOnly yes

Host personal-vps
    HostName 198.51.100.25
    User deploy
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

What each line does:

  • Host is the short name you type. It can be anything.
  • HostName is the real address or domain of the server.
  • User is the account name on the server, so you do not have to type user@.
  • IdentityFile is the private key to use for this host.
  • IdentitiesOnly yes tells SSH to offer only that key, not every key in the agent. Without it, SSH may still try other keys first.

Now you connect with just the short name:

ssh example-prod

On Linux and macOS the config file must not be writable by other users, or SSH refuses to read it. If you see a “Bad owner or permissions” message, fix it with:

chmod 600 ~/.ssh/config

4. Test it

Run ssh -v example-prod and check that the only key offered is work_ed25519. If you see Authenticated with the right key, you are done.

Two GitHub accounts on one computer

Both accounts use the same address, github.com, so you cannot tell them apart by host name alone. The trick is to invent a second host name that points to the same place:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

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

Test each one. GitHub replies with the account name it recognised:

ssh -T git@github.com
ssh -T git@github-work

Then use the alias in the address of any work repository:

git clone git@github-work:exampleco/website.git

For a repository you have already cloned, change its remote address:

git remote set-url origin git@github-work:exampleco/website.git

The guide on generating SSH keys for GitHub covers adding the key to your account.

Many keys on one server

On the server side there is nothing special to set up. The file ~/.ssh/authorized_keys in the account you log in to can hold one public key per line, and any of them will be accepted. This is how a team gives each person their own key for the same account, and how you can allow both your laptop and your desktop.

Keep the comment at the end of each line meaningful (for example work laptop), so you know which line to remove when a device is retired. How to delete SSH keys shows how to remove one key without affecting the others.

Common problems

SymptomCauseFix
Received disconnect: Too many authentication failuresSSH offered several agent keys and the server gave up before reaching the right one. The server limit is set by MaxAuthTries, which defaults to 6.Add IdentitiesOnly yes and the correct IdentityFile to that host’s block.
Permission denied (publickey) with a new keyThe new key has a custom name, so SSH never offered it.Name it with IdentityFile in ~/.ssh/config, or test with ssh -i ~/.ssh/work_ed25519 user@host.
GitHub says Key is already in useThe same public key is already on another GitHub account.Create a separate key for the second account and use a host alias.
Pushes go to GitHub as the wrong accountThe default key was used because the remote still says github.com.Change the remote to the alias with git remote set-url.
Config settings seem ignoredSSH uses the first value it finds for most settings, so a broad Host * block above your specific block wins.Put specific Host blocks first and Host * at the end of the file.
Bad owner or permissions on ~/.ssh/configThe file is writable by other users.Run chmod 600 ~/.ssh/config.

IdentityFile is an exception to the “first value wins” rule: if several matching blocks each list one, SSH collects all of them and tries them in order. Another reason to keep a catch-all Host * block at the bottom.

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.