Keshav Malik
Keshav Malik

Keshav Malik

How to use Multiple SSH-Keys for Different Repositories on Server? ⚡⚡

How to use Multiple SSH-Keys for Different Repositories on Server? ⚡⚡

Keshav Malik
·Mar 27, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Have you ever faced a situation where you need to use multiple SSH Keys for different repositories on a single server? If yes, then you are indeed not alone. Many developers face this situation, and this blog is for them. This blog will give you a complete understanding of how multiple SSH-Key can be helpful for you and how you can use those.

Why Do You Need SSH Keys?

With the help of SSH keys, a developer can connect to a remote server using a secure network connection. SSH-keys also permits the developer to access their remote repositories without being asked to enter user names or passwords every time.

Step 1: Generating SSH Keys

You can quickly generate SSH Keys on your server using ssh-keygen by using the following command.

ssh-keygen -t rsa -c "first key"

After using this command, you'll be prompted to enter the output file location and the name. SSH keys are usually stored in the .ssh directory. Feel free to use any location and name of your choice.

Let's use this dummy location for example: ~/.ssh/key1

Here key1 is the name for SSH keys. Once done, two files (Private and Public Key) will be saved in the output directory.

Use the same steps to generate one more SSH Key and name it key2

Step 2: Adding SSH Keys to GitHub Repositories

After setting up SSH Keys, add the Public Key to the GitHub Repository. To add the SSH key in the GitHub Repo, Navigate to Repository < Setting < Deploy Keys.

Add both the keys to two different repositories (Public or Private).

Step 3: Setting up SSH Config

Now it's time to add SSH Keys in the SSH Config File on the server. This allows SSH to identify which key to use for the host.

Let's use the vi editor to edit the SSH Config (~/.ssh/config) file. Open the file using vi ~/.ssh/config and paste the following in the file.

Host private-repo-1
HostName github.com
User git
IdentityFile ~/.ssh/key1

Host private-repo-2
HostName github.com
User git
IdentityFile ~/.ssh/key2

Here Host represents the Repository's name, and IdentityFile means the file's location.

Step 4: Clone the Repository

That's all you need to do. Now to clone the repo, use the following command.

git clone <Host>:<Username-Org_Name>/<Repository_Name>

For example, I have a private repository with the name hashnode, and in the SSH config file (Step 3), I've set Host to hashnode, so the command will be:

git clone hashnode:myusername/hashnode

And that's just it!

I hope you found a solution to the problem you were looking for. If you have any queries regarding this topic, then please comment below. We will be happy to help you.

Did you find this article valuable?

Support Keshav Malik by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this