DEV Community

Cover image for How to manage multiple ssh keys
Kiolk
Kiolk

Posted on

How to manage multiple ssh keys

I prefer to use ssh when I work with remote storage for git, such as GitHub, GitLab, Bitbucket, etc. It is a simple, secure and quick way to upload new commits or grab the last changes.

When I started working for a new company, I created a work account on GitHub to contribute to the work projects. I had tried to add an existing public key to the account setting, then I recognized that GitHub doesn't allow you to use the same key for two different accounts. 

So, how do I solve this problem? SSH provides a mechanism of configuration that helps to use specific keys for certain connections. 

First, you should navigate into the .ssh folder

cd ~/.ssh

Enter fullscreen mode Exit fullscreen mode

By using the command ls -la you can see a list of existing files. If you used only one key before, it would look similar to this:

-rw-------   1 yauheni  staff   464 May  3  2023 id_ed25519

-rw-r--r--   1 yauheni  staff   107 May  3  2023 id_ed25519.pub

Enter fullscreen mode Exit fullscreen mode

Second, you should generate a new pair of keys that will be associated with your work account.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/github-work
Enter fullscreen mode Exit fullscreen mode

So, now we need to tell the OS to use this key when we want to access to our work GitHub repository. For this purpose, we have to create a config file inside the .ssh folder. By default, the system uses it to get instructions on how to resolve access to different destinations. How it looks like in our example:

# Personal GitHub account
 Host github.com
 HostName github.com
 User git
 AddKeysToAgent no
 UseKeychain yes
 IdentityFile ~/.ssh/id_ed25519
# Work GitHub account
 Host github.com-work
 HostName github.com
 User git
 AddKeysToAgent no
 UseKeychain yes
 IdentityFile ~/.ssh/github-work
Enter fullscreen mode Exit fullscreen mode

What are fields meaning in this configuration. IdentityFile is the path to the location of our private key, Host is an alias that the system will use to recognize that we are trying to get access from a specific account. HostName is the name of the server to which we are trying to get access.

After that, we are ready to get access to the repository from different accounts. Don't forget to add the public key github-work.pub to a second GitHub account.
For the ability to contribute to one repository from two different accounts, you should add a new remote, but now, you have to use the host name github.com-work. There is an example of my git remote -v

work git@github.com-work:YauhenDaresay/GitWatch.git (fetch)
work git@github.com-work:YauhenDaresay/GitWatch.git (push)
origin  git@github.com:Kiolk/GitWatch.git (fetch)
origin  git@github.com:Kiolk/GitWatch.git (push)
Enter fullscreen mode Exit fullscreen mode

Please, don't ask me why I need both :) Now, you can freely contribute from different GitHub accounts on one machine.

You can find me in X, GitHub, medium or LinkedIn. Thanks for your time and see you in next post.

Top comments (0)