DEV Community

Rifki Andriyanto
Rifki Andriyanto

Posted on • Updated on

Using Multiple SSH Git In Terminal

Image description

Indonesian version

For most Developers, there may be a need to run multiple GitHub, GitLab, Bitbucket accounts on one computer. For example, you could run your work GitHub account and another account for your personal projects on the same computer.

In this article, you will learn how to use multiple SSH keys for different GitHub accounts

What is SSH key

In order to understand what this article is about, it is very important to have a good understanding of how Git works.

What is an SSH key?
SSH (Secure Shell) is a cryptographic network protocol that allows one computer to connect to a server via the internet securely. SSH is best used to access remote servers.

SSH is designed for encryption, verification, and secure communication between computers. It provides a secure way to execute commands and configure services remotely.

"From the explanation above, do you understand what SSH is?" "Lessgooo"

Manage SSH

If you are new to Git, I suggest deleting all the files in the directory

~/.ssh and start over.
Okay all the files have been deleted, now we will create a new SSH key.

Create a new SSH key

navigate to the ssh directory, run the following command.
cd .ssh/

cd .ssh/
Enter fullscreen mode Exit fullscreen mode

Generate SSH keys for each GitHub account:

ssh-keygen -t rsa -C "your_name@email.com"
ssh-keygen -t rsa -C "your_name@work_email.com"
Enter fullscreen mode Exit fullscreen mode

The key generator will ask you for a file name.

Enter a unique name like this for example:

id_rsa_personal
id_rsa_work
Enter fullscreen mode Exit fullscreen mode

Image description

Check the SSH key on your machine account

Generate SSH keys for your personal account and SSH for your work account

After generating the keys, use the following command to check if all keys have been generated:

ls ~/.ssh
Enter fullscreen mode Exit fullscreen mode

List of the following files:

Image description

Add SSH key to Github account

Now that we have the SSH key, let's link it with a Github account.

To get the SSH key, run this command:

cat id_rsa_personal.pub
Enter fullscreen mode Exit fullscreen mode

Image description

Copy the SSH key then login to your GitHub account.

Follow the steps below to add an SSH key to your GitHub account:

  1. On your GitHub, navigate to Settings.
  2. Select SSH and GPG Keys.
  3. Hit the New SSH Key, give it a significant Title and paste the Key.
  4. Finally, click the Add SSH key button.

Image description

Create a config file to set SSH keys

Next, let's combine everything in a configuration file. There are two GitHub accounts - personal and work accounts. So, we'll create two rules to associate SSH keys with the appropriate GitHub accounts.

Go back into the ssh directory, run the following command:

cd ~/.ssh
touchconfig
Enter fullscreen mode Exit fullscreen mode

before creating the config file, make sure the config file is not in the ssh directory.

if using CMD, can:

type null >> "config"
Enter fullscreen mode Exit fullscreen mode

or

open in the folder then create a new file with the name config

If so, you can edit it, or you can make it using your IDE or text editor, you can use nano or code by:

nano config
Enter fullscreen mode Exit fullscreen mode

or

code config
Enter fullscreen mode Exit fullscreen mode

Update the configuration file by adding the following rules:

# Personal account - default configuration

Host github.com
HostName github.com
git users
IdentityFile ~/.ssh/id_rsa_personal

# Work account

Host github.com-work
HostName github.com
git users
IdentityFile ~/.ssh/id_rsa_work

# Personal account - Gitlab host - bonus

Host gitlab.com
HostName gitlab.com
git users
IdentityFile ~/.ssh/id_rsa_user_gitlab
Enter fullscreen mode Exit fullscreen mode

Save the file by pressing CTRL + X then pressing Y then pressing ENTER in nano, if using code just press CTRL + S then press ENTER

Check the config file by:
cat config in terminal or CMD

In the code above, we have two different values. One is working repository and other is for user repository. Values allow you to add and update SSH configurations from the GitHub repository.

Test SSH key

Next, we have to test by cloning the private repository from private GitHub and work GitHub

Clone the private repository

To clone your personal project we can use this command:

git clone git@github.com:username/private-project-repo.git
Enter fullscreen mode Exit fullscreen mode

Clone office repository

When cloning your working repository, we will use this command:

git clone git@github.myorganization.com-work:/company-project-repo.git
Enter fullscreen mode Exit fullscreen mode

The difference between git cloning personal remote repository and work remote repository is that we use the host that we have created in the config file. For personal remote repositories we use host github.com, while for remote work repositories we use host github.com-work

Conclusion

In conclusion, the point is to create your SSH, then create a config file in the .ssh folder, and finally, use the hostname we created in the config file for the remote repository.
Github Repository

Greetings Hello World 👋

Top comments (0)