How to configure multiple SSH keys in your computer

Ali S
NicaSource
Published in
5 min readSep 26, 2022
How to configure multiple SSH keys in your computer

SSH is one of the most used protocols for safe data exchange. SSH connections have been commonly used to secure different types of communications between your computer and a remote host.

Frequently, developers are involved in different personal or work-related projects and are required to manage different SSH keys. Managing various SSH keys to handle different projects can be a challenging task but not one impossible to accomplish. If you are having issues with SSH key management, I’ll teach you how to configure multiple SSH keys for different GitHub accounts in this article.

Basic Set Up

Before starting the project, create two directories: (1) One to host all your company’s projects. And (2) the other one is to have your personal projects.

Console command that make two directories

Next, you will need to check if you already have an existing SSH key. To do this, open your terminal, which should be located at the root directory of your computer ~. Then, enter ls -al ~/.ssh to see if existing SSH keys are present. Check the directory .ssh to see if you already have a public SSH key.

Console Command to list the element in ssh directory

If you receive an error that ~/.ssh does not exist, you do not have an existing SSH key pair in the default location. Don’t panic! In the next section, I’ll teach you how to create one. If you found the SSH keys, you can skip the next section and head to the config file section.

Create a new SSH key

If you don’t have a SSH key on your computer, and you don’t know how to generate one, here is how you can do it:

Still in the terminal of your computer’s root directory, create a .ssh directory by executing the following command:

Then, move inside the file and execute the following command — consider that in the command, “filename” is your key's name. Use a different file name for better management :

After hitting Enter, you should see a message: “Generating public/private ed25519 key pair”. Then, you will be asked to “Enter a file in which to save the key”. When that happens, press enter and the default location will be used.

PRO-TIP: You will be asked to enter a passphrase. It is always recommended to secure your key pair with a passphrase.

When everything is done, you should see something like this:

Create a config file

Now that we all have a .ssh/ directory, inside it, you should see a config file. If the file is not automatically generated, you can create a “config” file without adding an extension. For it, use the following structure as an example of how your file should look.

Note that the file is organized into sections (personal-projects and my-company-account). Each section starts with (1) the HOST directive (“yourGithubUserName” and “yourGithubCompanyAccountUserName”) and (2) it specifies the key used when establishing a connection with the remote server.

The SSH client reads the configuration file, and if more than one pattern matches, the options from the first matching take precedence.

Create a .gitconfig file

In your root directory ~, you need to create a .gitconfig file as it will allow git to understand which key should be used depending on the directory path.

Note that you will need to create a /.gitconfig file for each account you created a key. The file will contain the linked account’s email and username with the corresponding key pair. It should look like this:

Adding your SSH key to the ssh-agent

First, you need to make sure the ssh-agent is running:

Note that depending on your environment, you may need to use a different command. It may be: sudo -s -h before starting the ssh-agent, or you may need exec ssh-agent with bash or exec ssh-agent zsh.

Once your ssh-agent is running, you can add your ssh-key:

Last but not least…

To add your SSH key to your GitHub account, check the detailed instruction and guidance from the official documentation. It is important to mention that every time you want to clone a repo, you need to edit the link that the GitHub repo generates:

Instead of this:

You must do it like this:

And that’s all, folks!

I hope you have found this article useful. Comments and thoughts are appreciated. If you think I have skipped any steps or something went wrong for you, share it in the comments! And don’t forget to tell us what else you did to make it work.

--

--