Multiple SSH Key Account Management


Posted on Sun, Nov 27, 2022 security ssh git

Working with the same Git or GitHub account can be fun, until the need for multiple accounts arises. As a developer, managing multiple accounts on one machine with SSH keys is almost inevitable.

As we often have to work on multiple projects, from personal to work related, we may not want to use the same GitHub profiles. Or we may wish to port our previous configurations to a new device with ease.

In this article, we will learn how to create SSH keys and how to manage multiple GitHub accounts with SSH keys. Interesting, right? Let’s get started !

What are SSH keys? 🤷

An SSH key, or Secure SHell key, is a pair of credentials used in connecting to the SSH network protocol. This network protocol is used for remote communication between machines over an unsecured open network.

The SSH protocol is used for remote file transfer, network management, and remote operating system access.

The working principle of this protocol is quite straightforward.

It employs the public-key and private-key encryption mechanisms. That’s to say that you will provide a private "key" to a public "lock" so as to facilitate secure connection and file sharing over an open network.

In the next section, we'll learn how to manage multiple GitHub accounts with SSH keys.

1. Create an SSH Key 🔐

The SSH protocol was developed for the Unix and Linux-based operating systems. If you’re running the Windows Operating System, please learn how to set up a Linux environment on Windows here.

Creating an SSH key is pretty easy, all you have to do is open your terminal and follow the steps below :

1. Create and enter ~/.ssh directory.

We'll be storing our SSH keys and their components in the ~/.ssh directory. To get started with SSH, you must first create one if one does not already exist. If it already exists, just cd into the directory.

mkdir .ssh 
cd ~/.ssh

2. Generate SSH Keys.

We're at the final stage of SSH key creation. All that is left is to generate the keys. This can be done by simply running the code below :

ssh-keygen -t rsa -C "email-address" -f "github-username"

The -C flag is used as a comment, which helps for easy identification of the ssh key we're creating.

The -f flag is used as a file name. So, we can name our ssh key with the help of the -f flag.

From the above explanation, we can now proceed to creating multiple accounts. We'll create two accounts, but you can create as many as you want as long as you follow the right procedures.

First account : Let's assume it's a personal account.

ssh-keygen -t rsa -C "personal@gmail.com" -f "personal"

Second account : Let's assume it's a work account.

ssh-keygen -t rsa -C "work@gmail.com" -f "work"

You’ll receive a prompt telling us to choose the location where we want our SSH keys to be saved and a passphrase. It's advisable to leave it in default mode. You can still alter the default settings as per your needs.

You can see the SSH keys created by running the ls -al /.ssh command. It lists out all the existing keys in your machine.

2. Register SSH Keys With SSH Agent ®️

SSH Agent is part of the SSH tool suite. It keeps track of the user's identity keys and their corresponding passphrases. That is, we cannot use SSH keys that have not been registered with SSH Agent.

Registering SSH keys with the SSH Agent is quite simple. Run the code below to register the keys we created initially :

# add personal ssh key
ssh-add -K ~/.ssh/personal

# add work ssh key
ssh-add -K ~/.ssh/work

3. Add SSH Public Keys to GitHub 🕸️

For us to be able to exchange files over an unsecured open network, we have to create a communication pathway between our SSH keys and our host. In our case, we're using GitHub. So we need to add our public key that we generated earlier to its corresponding GitHub accounts.

Adding SSH public keys to GitHub is very straightforward. It can be done by following the following steps :

1. Copy the public SSH key.

There are two ways to copy SSH keys. You can do it by opening the SSH file with the text editor and copying the public key manually.

In the code below, I'll be opening the SSH key we created with Vim.

vim ~/.ssh/personal.pub

vim ~/.ssh/work.pub

We can as well copy the content of the public key file directly to the clipboard.

You can do this by running the code below :

# step 1
pbcopy < ~/.ssh/personal.pub

# step 2
pbcopy < ~/.ssh/work.pub

2. Paste the public key on GitHub.

Follow the steps below to paste the public key on GitHub.

  1. Go to Settings
  2. Select “SSH and GPG keys” from the menu to the left.
  3. Click on "New SSH key" give it a title of your choice, and paste the key in the box below.
  4. Click "Add Key" and you’re done !

Add the SSH keys one after another.

4. Make the SSH configuration file.

We're done with the basic setup. It's now time to add SSH configuration rules for different hosts. Stating which identity file to use for which domain.

We can find these rules in the ~/.ssh/config file. If none exist, create a new one and configure it with the codes below :

cd ~/.ssh

# Creates the file if it doesn't exist. 
touch config

# Open the file in VS Code; alternatively, you can use any text editor or IDE of your choice. 
code config

Now, add SSH configuration entries for the relevant GitHub accounts similar to the one below in your ~/.ssh/config file :

# Personal account; - the default setting
Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/personal

# Work account
Host work.github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/work

The above configuration asks ssh-agent to :

With the above configuration, we can easily manage multiple GitHub accounts with SSH keys. You can add more configurations for more accounts by following the steps above.

Managing an Active SSH Key with ssh-agent 💢

We can run into some trouble if two SSH keys are active during a git operation. We can easily solve this problem with the following steps :

1. Run the command below to see the list of active keys.

ssh-add -l

2. Delete active keys and add the necessary ones.

Let's assume you want to push some codes. If it’s to a personal Git account that you are about to push, you can use the commands below :

# remove all ssh entries from the ssh-agent configuration.
ssh-add -D

# inserts the personal ssh key.
ssh-add ~/.ssh/personal

If it’s to a work Git account that you are about to push, you can as well do it like this :

ssh-add -D

ssh-add ~/.ssh/work

Cloning GitHub Repositories Using Different Accounts 🌧️

We are done with the SSH setup. We can now proceed to testing the setup to see it in action. We will clone a repository using one of the accounts we have added.

Make a new project folder where you want to clone your repository, and go to that directory from your terminal.

Syntax :

git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git

Example :

In this example, we're cloning Demo.git from my personal account to my personal git user account.

git clone git@github.com-personal: personal/Demo.git

Configuring Git Accounts

We are almost done. All that's left to do is ensure that our commits and pushes from each repository on the system use the correct GitHub user account. We will have to configure user.email and user.name in every repository, freshly cloned or existing, before carrying out the operation.

We can do this using the commands below :

# email
git config user.email "personal@gmail.com"

# username
git config user.name "[ENTER USERNAME]"

# email
git config user.email "work@gmail.com" 

# username
git config user.name "[ENTER USERNAME]"

To push or pull to the correct account, we need to add the remote origin to the project.

git remote add origin git@github.com-personal:personal

git remote add origin git@github.com-work:work

Congratulations 🎈 You can now pull and push with freedom !

Conclusion

We have so far covered what SSH keys are and how to configure multiple GitHub accounts on a single machine.

From the samples and examples we have covered so far, you can see that multiple GitHub accounts make our development life more organized and modular.

You can easily separate work and personal projects without worrying about them interfering with each other. That is the type of freedom that having multiple GitHub accounts brings!