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/
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"
The key generator will ask you for a file name.
Enter a unique name like this for example:
id_rsa_personal
id_rsa_work
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
List of the following files:
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
Copy the SSH key then login to your GitHub account.
Follow the steps below to add an SSH key to your GitHub account:
- On your GitHub, navigate to Settings.
- Select SSH and GPG Keys.
- Hit the New SSH Key, give it a significant Title and paste the Key.
- Finally, click the Add SSH key button.
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
before creating the config file, make sure the config file is not in the ssh directory.
if using CMD, can:
type null >> "config"
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
or
code config
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
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
Clone office repository
When cloning your working repository, we will use this command:
git clone git@github.myorganization.com-work:/company-project-repo.git
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)