How to configure (and use) two ssh keys for Github
This blog is for all the fresh interns starting off contributing to a repository that’s not open source and the team won’t give you access to repository on your personal email but on your_name@corporate_domain.com
. So basically we’ll have to configure two ssh key’s on the same machine and here’s how :
How to setup two different ssh keys on same machine?
Firstly generate two ssh keys for corresponding accounts , here’s github guide for that
# Default github account: personal_username
Host github.com
HostName github.com
IdentityFile ~/.ssh/personal_key
IdentitiesOnly yes
# Other github account: professional_username
Host github-professional_username
HostName github.com
IdentityFile ~/.ssh/professional_key
IdentitiesOnly yes
Just add ssh private keys to you agent by :
$ ssh-add ~/.ssh/personal_key
$ ssh-add ~/.ssh/professional_key
Now git commands will default to use your personal key as identifier but to specify your professional key for work you could finally clone that repository by :
$ git clone git@github-professional_username:org/project.git /project/path
Alternatively if you want to use a specific key to run any github command just prepend you git command with GIT_SSH_COMMAND
environment variable like :
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/professional_key' git push origin master
Kidding about pushing to master 😂, however if you want to setup a specific key for a specific repository(say you have only 3–4 repository for work on your machine)
Make changes to that repository’s git config by
$ vi .git/config
Add the following configuration to that file
[user]
name = Your Name
email = your_name@corporate_domain.com
[core]
sshCommand = "ssh -i ~/.ssh/professional_key"
You can finally start contributing to the project now ✌️