Multiple SSH keys for different GitHub accounts
Today weāll show you how to set up multiple SSH keys for different GitHub accounts. When you need to work with the two different GitHub accounts and those accounts need to be set up with an SSH key then you might need to do some settings in config of the SSH to manage multiple GitHub accounts on a single machine.
Manage multiple SSH keys for different GitHub accounts
- Generating a new SSH key and adding it to the ssh-agent
- Adding a new SSH key to your GitHub account
- Modify the SSH config
- Manage the remote URL of the repository
1. Generating a new SSH key and adding it to the ssh-agent
To clone a repository using SSH URL then we have to first generate a new SSH key and add it to the `ssh-agent`. Refer to the link below for more information.
Generating a new SSH key and adding it to the ssh-agent
You can set up the configuration based on the installed OS.
Note: Please enter a different key name based on the repository name so you can identify it later.
Just assume that we have created two different keys that are listed below.
~/.ssh/id_rsa_work
~/.ssh/id_rsa_personal
2. Adding a new SSH key to your GitHub account
Now, letās link those keys with the GitHub account. For that you have to copy the SSH key and add it in your account settings. Refer to this link for step by step information.
Adding a new SSH key to your GitHub account
3. Modify the SSH config
So now the most important part is to modify the SSH config to manage multiple GitHub accounts using SSH on a single machine.
Jump into the `.ssh` folder to create/modify a `config` file. If you canāt see the `config` file in the folder then simply create it.
cd ~/.ssh/
touch config
nano config
Check out the following lines that I have written into the config file.
config
#work repository
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
#personal repository
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
In the above code, we have set the two different values (`github.com-work` for work repository and `github.com-personal` for personal repository) for `Host` to be used to add/update the remote origin of the GitHub repositories.
4. Manage the remote URL of the repository
At last, we have to clone a repository using SSH URL but we need to slightly update the repository host name as mentioned above.
To clone work repository:
git clone [email protected]:xxxxxx/xxxx.git
To clone personal repository:
git clone [email protected]:xxxxxx/xxxx.git
If you have already cloned the repositories then run the following command to update the remote URL.
To change remote URL for work repository:
git remote set-url origin [email protected]:xxxxxx/xxxx.git
To change remote URL for personal repository:
git remote set-url origin [email protected]:xxxxxx/xxxx.git
Check out more articles on Git.
Thatās it for today.
Thank you for reading. Happy Coding..!!