
How to Set up multiple SSH keys on one System
In this guide, you’ll learn how to create separate SSH key pairs (using secure algorithms like Ed25519), lock down their permissions, and assign each one to a custom host alias in your ~/.ssh/config. You’ll then load these keys into the SSH agent for password-free authentication, register the public keys with Git and server accounts, and verify connections. This setup keeps personal, work, and open-source access neatly separated and secure.
Configuring multiple SSH keys was challenging because the required information was scattered across various sources. I’ve gathered it all here in one place to make the process easier and save you time.
Why Use Multiple SSH Keys?
Developers often juggle more than one Git or server account—personal repositories vs. work projects, open-source contributions vs. private servers, etc. Assigning a unique SSH key pair to each account helps:
- Keep access separate (so you don’t accidentally push to the wrong repo)
- Maintain security (if one key is compromised, others remain safe)
- Automate connections via simple host aliases
Choose Your Algorithms and File Names
SSH keys can be generated with different algorithms; Ed25519 is recommended for new keys due to its security and speed. For each identity, decide on a clear file name:
# Personal (e.g. GitHub)
ssh-keygen -t ed25519 -C "you@personal.email" -f ~/.ssh/id_ed25519_personal
# Work (e.g. Bitbucket)
ssh-keygen -t ed25519 -C "you@work.email" -f ~/.ssh/id_ed25519_work- The -C flag adds a comment (your email) for easy identification.
- The -f flag specifies the output filename, so you end up with two pairs. - ~/.ssh/id_ed25519_personal & ~/.ssh/id_ed25519_personal.pub
- ~/.ssh/id_ed25519_work & ~/.ssh/id_ed25519_work.pub
Secure Your Private Keys
By default, SSH will warn if your private key is too exposed. Tighten permissions immediately:
chmod 600 ~/.ssh/id_ed25519_personal
chmod 600 ~/.ssh/id_ed25519_workThis ensures only your user account can read/write the private key files.
Create or Update Your SSH Config
The SSH config file (~/.ssh/config) lets you assign host aliases that map to specific keys:
# ~/.ssh/config
# Personal GitHub account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
# Work Bitbucket account
Host bitbucket-work
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes- Host: the alias you’ll use in Git commands (e.g. github-personal).
- HostName: the real server address (e.g. github.com).
- IdentityFile: the path to the private key you generated.
- IdentitiesOnly yes: forces SSH to use only the specified key, avoiding unintended key tried
Add Keys to the SSH Agent
If you use an SSH agent to cache decrypted keys, load them now so SSH can pick the right one automatically:
# Start agent (if not already running)
eval "$(ssh-agent -s)"
# Add both keys
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_workYou’ll be prompted for each key’s passphrase (if you set one). After this, the agent will serve the decrypted keys to SSH sessions.
Register Your Public Keys with Remote Accounts
For each account, copy the corresponding .pub file and paste it into the SSH key settings on the website:
# Copy to clipboard on macOS/Linux
pbcopy < ~/.ssh/id_ed25519_personal.pub- Go to GitHub → Settings → SSH and GPG keys and add the personal key.
- Repeat for Bitbucket → Personal settings → SSH keys with the work key.
Cloning and Pushing via Host Aliases
Use your custom aliases when interacting with repos:
# Clone your personal repo
git clone git@github-personal:yourusername/your-repo.git
# Clone your work repo
git clone git@bitbucket-work:yourusername/work-project.gitUnder the hood, SSH looks up github-personal in your ~/.ssh/config and uses the right key automatically.
Verification and Troubleshooting
- List loaded keys: ssh-add -l
- Test connection:
ssh -T git@github-personal
ssh -T git@bitbucket-work- “Permission denied” errors often mean either the wrong key is being offered or file permissions are too loose—double-check your config aliases and chmod settings.
Conclusion
With separate SSH key pairs, a well-configured ~/.ssh/config, and the SSH agent handling your keys, you can seamlessly switch between personal and work Git accounts—or any other SSH-based service—without conflict. This setup keeps your workflows clean, secure, and highly productive.
