Authenticating multiple GitHub accounts using SSH
If you have multiple GitHub accounts [Org and Personal] , then only headache is authenticating with multiple GitHub accounts. To authenticate multiple GitHub accounts using SSH, you’ll need to configure SSH to use different SSH keys for each GitHub account. Here’s how you can do it:
Generate SSH Keys:
Generate SSH keys for each GitHub account you want to authenticate. You can use thessh-keygen
command to generate SSH keys.
- Now open your terminal and use below command to go inside .ssh folder
cd ~/.ssh
2. Create a ssh key for GitHub account 1 with below command and hit enter key:
ssh-keygen
- Leave the default path to store the ssh key by hitting the enter key.
- Please provide any passphrase for security & hit enter:
- Now it will create private key [id_rsa] and public key [id_rsa.pub] in your ~/.ssh folder as below:
3. Copy the entire public key and paste it in your GitHub account 1 as below:
- Login to your GitHub account 1 > click on your profile picture on top right side corner > click on settings >
- Click on SSH and GPG keys under “access”:
- Click on “New ssh key” > Give a name > paste the public key > add ssh key:
4. Create a config file in ~/.ssh folder using your favourite IDE, I will choose “VI” to create a config file and add the below:
#ACCOUNT-1
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
5. Let’s try to access a GitHub account 1 repo by cloning to your local machine using ssh clone link:
6. Now let’s create another ssh key for account 2:
- Create a ssh key for account 2, but this time dont use the same command “ssh-keygen”. It will overwrite the existing id_rsa pub key. So we can use below command to create a new key for account 2 without overwriting the existing key:
ssh-keygen -f org_id_rsa
- We can see 2 sets of id_rsa public and private key in ~/.ssh folder as below:
7. Now update the config file with account 2 configuration as below:
#ACCOUNT-1
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
#ACCOUNT-2
Host github.com-org
HostName github.com
IdentityFile ~/.ssh/org_id_rsa
8. Now let’s clone the GitHub account 2 repo:
- Clone the repo from GitHub account 2, but make sure to replace the link as below:
Dont - git clone git@github.com:<YOUR ACCOUNT>/<YOUR REPO>.git
DO - git clone git@github.com-org:<YOUR ACCOUNT>/<YOUR REPO>.git
We have to add as same as “Host” that we created in config file :)
That’s It folks ;)