Skip to content

Instantly share code, notes, and snippets.

@JimmyPesto
Forked from jexchan/multiple_ssh_setting.md
Last active September 23, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimmyPesto/03c0b51d7286df8e6848d67d673d607c to your computer and use it in GitHub Desktop.
Save JimmyPesto/03c0b51d7286df8e6848d67d673d607c to your computer and use it in GitHub Desktop.
Multiple SSH keys for different github accounts

Multiple SSH Keys settings for different git accounts

This is working for different hosts like github.com & bitbucket.org.

If it is required to manage multiple accounts from the same host like github.com activate the key that is currently required by the following commands (eg. for username_2):

$ ssh-add -D            		//removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa_username_2      // Adds the relevant ssh key

Found here (step 5): https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/

create different public key

create ssh key with non default location

$ ssh-keygen -t rsa -C "your_email@youremail.com"

for example, 2 keys created at:

~/.ssh/id_rsa_username_1
~/.ssh/id_rsa_username_2

then, add these two keys to the ssh agent as following

$ ssh-add ~/.ssh/id_rsa_username_1
$ ssh-add ~/.ssh/id_rsa_username_2

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Note: If ~/.ssh/does not exisit. Run ssh for the first time and it will be created.
https://linuxize.com/post/using-the-ssh-config-file/

Then add

#username_1 account
Host github.com:username_1
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_username_1

#username_2 account
Host github.com:username_2
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_username_2

When working on a repository located in another users git account, this should also be registered in the ssh config file. When the ssh URL of the repo looks like this

git@bitbucket.org:different_username/repository_name.git

and your Git account "username_1" is allowed to edit this, it should be configured like this

#different_username account
Host github.com:different_username
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_username_1

Update SSH keys on GitHub account settings

Visit: https://github.com/settings/keys
Help: https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

Update SSH keys on Bitbucket account settings

Visit: https://bitbucket.org/account/settings/ssh-keys/
Help: https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/

Clone your repo and adept your Git config

clone your repo

$ git clone git@github.com:username_1/gfs.git gfs_username_2  

cd into repo and modify the local git config

$ git config user.name "username_2"
$ git config user.email "username_2@gmail.com" 

or you can change the global git config

$ git config --global user.name "username_2"
$ git config --global user.email "username_2@gmail.com"

Update origin URLs of repos (optional)

$ git remote set-url origin git@<host>:<username>/<repo>

This is the same as editing your .git/config file.

host-in-ssh-config -> "github.com-username_1"

Then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment