Bishesh Bhattarai

Crafting Secure and full-stack solutions 💻

How to manage multiple ssh keys of different GitHub/Gitlab account ? 

Throughout our journey as software engineers, many of us have encountered the challenge of managing multiple GitHub accounts. This situation often arises when we have either multiple GitHub accounts within the same company or a combination of personal and company accounts. It becomes crucial to effectively handle our SSH keys to prevent permission conflicts, as each GitHub account requires its own set of SSH keys.

As a first step, it is necessary to generate SSH keys for each GitHub account. GitHub provides documentation on creating SSH keys, but if you are lazy like me to read whole docs, here is the essential code to generate your SSH keys:

ssh-keygen -t ed25519 -C "personal@example.com"  //For personal account
ssh-keygen -t ed25519 -C "c1@example.com"        //For company account 1
ssh-keygen -t ed25519 -C "c2@example.com"        //For company account 2

After executing these commands, SSH files will be created at specific paths on your machine:

~/.ssh/personal (Personal account)

~/.ssh/c1 (Company account 1)

~/.ssh/c2 (Company account 2)

Note: You can choose any desired name for these files during the SSH key generation process.

Next, you need to add these SSH keys to your SSH agent by running the following commands:

ssh-add ~/.ssh/personal
ssh-add ~/.ssh/c1
ssh-add ~/.ssh/c2

To ensure a clean setup, it is advisable to remove any previously cached keys:

ssh-add -D

Additionally, don’t forget to add the public keys of each SSH key to their respective GitHub accounts. You can do so by following this documentation, it is pretty straightforward.

Now that you have added all the SSH keys to their respective GitHub accounts, it’s time to handle these keys locally on your machine.

Navigate to the ~/.ssh folder and create a configuration file named config. You can use any editor you prefer to open this file. The config file allows you to customize various SSH client settings on your computer, including default options for SSH connections. Here’s how you can create and open the config file:

cd ~/.ssh
touch config 
nano config

Inside the config file, you should add the following configuration settings for each GitHub account. Replace <company1-acc-name> and <company2-acc-name> with the actual names associated with your company accounts:

#personal account
Host github.com-sparkcode
 HostName github.com
 User git
 IdentityFile ~/.ssh/personal

#company 1 account
Host github.com-<company1-acc-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/c1

#company 2 account
Host github.com-<company2-acc-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/c2

With the configuration file in place, you can now go to the repository to perform regular Git actions. However, there is one thing to consider: since the Git remote origin is set to github.com, you have to change it to map to the one in the host configured in the config file. For example:

git remote set-url origin git@github.com-sparkcode:sparkcode/gittut.git

Alternatively, if you haven’t yet cloned the repository, you can directly clone it using the modified URL format:

git clone git@github.com-sparkcode:sparkcode/<your_project_name>.git

Following these steps, you will be able to perform Git actions across multiple GitHub accounts without encountering any issues related to permissions.

Thanks for reading. Don’t hesitate to share this valuable knowledge with your fellow developers.

Happy coding !

 

Leave a Reply

Your email address will not be published. Required fields are marked *