How To Add Multiple SSH Keys
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
With the increase use of git-based version control systems like GitHub or GitLab, its quite evident that you may require multiple ssh keys to maintain different git repository for different accounts. In this blog post, we will discuss how to add multiple SSH keys in Windows or Linux system for multiple GitHub account.
The example given in this blog is with GitHub, but the same approach (with some minute changes) can be used for other providers like GitLab or Bitbucket etc.
Prerequisite:
- Multiple GitHub account
- Git installed in your local system
How To Add Multiple SSH Keys In Windows:
Step 1: Generate SSH keys for target systems
For this demo we will generate three keys, two for GitHub (for two separate account) and one for GitLab. This will give cover multiple providers, multiple account scenarios.
1 2 3 4 5 6 7 8 9 10 11 |
# Generate key for GitHub - Account A (cloudaffaire@gmail.com) ssh-keygen -t ed25519 -C "cloudaffaire@gmail.com" -f $Home/.ssh/cloudaffaire_github -q -N """" # Generate key for GitHub – Account B (debjeettoni@gmail.com) ssh-keygen -t ed25519 -C "debjeettoni@gmail.com" -f $Home/.ssh/debjeet_github -q -N """" # Generate key for GitLab - Account A (cloudaffaire@gmail.com) ssh-keygen -t ed25519 -C "cloudaffaire@gmail.com" -f $Home/.ssh/cloudaffaire_gitlab -q -N """" # You should have 3 key pairs now dir $home/.ssh |
Note: It’s recommended to use key-phrase during ssh key generation.
Step 2: Add the public keys (.pub extension) to respective GitHub and GitLab accounts.
Login to GitHub with account A credentials and setup SSH keys.
Copy the content of $Home/.ssh/cloudaffaire_github.pub file and paste in the Key section below.
Provide your GitHub password if prompted.
Next, login to GitHub again with Account B credentials and repeat the same step to add the key.
Note: This time you have to copy the content of $Home/.ssh/debjeet_github.pub
Finally add the key for your GitLab account as well. You can follow step 2 and 3 below blog for same.
https://cloudaffaire.com/how-to-add-an-ssh-key-in-your-gitlab-profile/
Step 3: Create a ssh config file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$config = @' # Account- github - cloudaffaire Host github.com HostName github.com User cloudaffaire@gmail.com IdentityFile ~/.ssh/cloudaffaire_github # Account- github - debjeet Host github.com HostName github.com User debjeettoni@gmail.com IdentityFile ~/.ssh/debjeet_github # Account- github - cloudaffaire Host gitlab.com HostName gitlab.com User cloudaffaire@gmail.com IdentityFile ~/.ssh/cloudaffaire_gitlab '@ echo $config | Add-Content -Path $Home/.ssh/config |
Step 4: Add SSH private keys into the SSH authentication agent
1 2 3 4 5 6 7 8 |
# start ssh agent Get-Service -Name ssh-agent | Set-Service -StartupType Automatic Start-Service ssh-agent # add private keys into ssh authentication agent ssh-add $Home/.ssh/cloudaffaire_github ssh-add $Home/.ssh/debjeet_github ssh-add $Home/.ssh/cloudaffaire_gitlab |
Step 5: Finally, we reach the point to test if everything works
1 2 3 4 5 6 7 8 |
git clone git@github.com:debjeettoni/gitTest.git Remove-Item 'gitTest' -Recurse -Force git clone git@github.com:CloudAffaire/gitTest.git Remove-Item 'gitTest' -Recurse -Force git clone git@gitlab.com:cloudaffaire/gittest.git Remove-Item 'gitTest' -Recurse -Force |
Note: You might get a warning as below, type yes
RSA key fingerprint is SHA256:ngfsdfsfd723hvhsdvbksghTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
How To Add Multiple SSH Keys In Linux:
Step 1: Same as step 1 for Windows.
Step 2: Same as step 2 for Windows.
Step 3: Create a ssh config file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
cat < # Account- github - cloudaffaire Host github.com HostName github.com User cloudaffaire@gmail.com IdentityFile ~/.ssh/cloudaffaire_github # Account- github - debjeet Host github.com HostName github.com User debjeettoni@gmail.com IdentityFile ~/.ssh/debjeet_github # Account- github - cloudaffaire Host gitlab.com HostName gitlab.com User cloudaffaire@gmail.com IdentityFile ~/.ssh/cloudaffaire_gitlab EOT |
Step 4: Add SSH private keys into the SSH authentication agent
1 2 3 4 5 6 7 |
# start ssh agent eval `ssh-agent -s` # add private keys into ssh authentication agent ssh-add ~/.ssh/cloudaffaire_github ssh-add ~/.ssh/debjeet_github ssh-add ~/.ssh/cloudaffaire_gitlab |
Step 5: Finally, we reach the point to test if everything works
1 2 3 4 5 6 7 8 |
git clone git@github.com:debjeettoni/gitTest.git rm -r gitTest git clone git@github.com:CloudAffaire/gitTest.git rm -r gitTest git clone git@gitlab.com:cloudaffaire/gittest.git rm -r gittest |
Note: You might get a warning as below, type yes
RSA key fingerprint is SHA256:nThbshdfy674vshdvjhsbdjgllasddsKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Hope you have enjoyed this article.