Skip to content

Instantly share code, notes, and snippets.

@Hiweus
Last active March 25, 2024 03:07
Show Gist options
  • Save Hiweus/115a050769f6b9f65b3e30f5a57a2111 to your computer and use it in GitHub Desktop.
Save Hiweus/115a050769f6b9f65b3e30f5a57a2111 to your computer and use it in GitHub Desktop.
Configure computer to use multiple ssh keys for same service

Configure multiple ssh keys on machine

For this project i will use github as example. I have two keys id_rsa and personal_rsa.

First of all you need to create file ~/.ssh/config. Inside the file put content as follow and edit as your need

# Default configuration (ask for password if there's no key in config file)
Host *
IdentitiesOnly yes

# personal_rsa
Host personal.com
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/personal_rsa

# id_rsa
Host github.com
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa

In the repos where you want to use key personal_rsa edit the ssh remote url and replace Hostname by Host, for example:

Original Repo Url: git@github.com:my-repo.git
Url To use personal_rsa: git@personal.com:my-repo.git

When you want to use id_rsa key you can just keep the original repo url, because Host and HostName are the same in config.

you can do this for any ssh service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment