Multiple SSH Key - biswajitsundara/gitdoc GitHub Wiki

We can't add same ssh public key to multiple git accounts.

  • So we will have to maintain different ssh keys for different accounts
  • Means different sets of private, public keys

Initial Cheks

See how many ssh keys we have currently

  • If the ssh key is created we will see the default id_rsa & id_rsa.pub
ls -l ~/.ssh

-rw-r--r-- 1 biswa 197609 3434 Sep  7  2022 id_rsa
-rw-r--r-- 1 biswa 197609  751 Sep  7  2022 id_rsa.pub
-rw-r--r-- 1 biswa 197609  828 Mar 30  2023 known_hosts

Generate SSH key

Now we will create another new ssh key

  • Keep everything default
  • This will create two files bisdocs_rsa, bisdocs_rsa.pub
//syntax
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/repo1_rsa

//command
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/bisdocs_rsa

Start the SSH agent

//command
eval $(ssh-agent -s)

//console output
Agent pid 1685

Add the key to SSH agent

//command
ssh-add ~/.ssh/bisdocs_rsa

//console output
Identity added: /c/Users/biswa/.ssh/bisdocs_rsa ([email protected])

Create/Edit the ssh config file

vi ~/.ssh/config
or open the file from C:\Users\biswa\.ssh in windows

The config file needs to have like this

# github account
Host bisdocs.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/bisdocs_rsa
  • Copy this block for as many ssh keys we want to add
  • Host actually doesn't impact anything, we have added bisdoc.github.com to differentiate
  • HostName is important, don't change it
  • Identity file path is mandatory
  • Save this file.

Update the git configuration

  • Go to the folder
  • Open the .git folder and then config file
  • or execute the command vi .git/config
  • Update the url parameter
url = [email protected]:bisdocs/hello-react.git
  • Update it to use bisdocs.github.com

Add the public key to the git repo

//command
cat ~/.ssh/bisdocs_rsa.pub

add this to the github ssh key.

Key Points

  • Whenever we need to work on the multiple ssh and it doesn't work
  • Kill the ssh agent eval $(ssh-agent -k)
  • Restart the agent eval $(ssh-agent -s)
  • Add the ssh key ssh-add ~/.ssh/bisdocs_rsa
  • Verify the ssh is added to the agent ssh-add -l

Tha's it! now we can use multiple ssh keys.