Set up multiple ssh keys for multiple github accounts

By phuongkt, at: Jan. 25, 2024, 7:31 p.m.

Estimated Reading Time: 8 min read

Set up multiple ssh keys for multiple github accounts
Set up multiple ssh keys for multiple github accounts

Set up 2 ssh keys for 2 github accounts


Since August 13, 2021, Github has removed support for password authentication. Hence, creating and using and SSH key is an excellent authentication option when working with Github.

Except, sometimes you might work with two or more accounts other than one for accessing to Github or Gitlab and similar tools. For example you can have one account for your works at your company and one for the projects at school. To accomplish this, you will need to create multiple SSH key for each account.

With the problem being, in this tutorial you will learn how to configure and manage different accounts & keys for Github authentication.

First if you are new to Github or SSH keys, you can generate a new SSH key by openup your terminal and type:

$ ssh-keygen -t rsa -C "[email protected]"

The -t is for type of encryption, here we use rsa encryption. -C is for comment, we use this to differentiate with the other key that we are going to create afterward.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/tphuong_k/.ssh/id_rsa):

Leave a blank to save the newly generated key in /.ssh/id_rsa

push to git

To check your generated key use:

$ ls -al ~/.ssh

All ssh file on the system

total 16
drwx------ 2 tphuong_k tphuong_k 4096 Jan 14 16:11 .
drwxr-x--- 45 tphuong_k tphuong_k 4096 Jan 14 16:07 ..
-rw------- 1 tphuong_k tphuong_k 2610 Jan 14 16:11 id_rsa
-rw-r--r-- 1 tphuong_k tphuong_k 578 Jan 14 16:11 id_rsa.pub

There you go, the new SSH key has been created

Next, add public SSH key to your github account, go to https://github.com/settings/keys

push to git

Here, click on New SSH key

push to git

To see your public key use:

$ cat ~/.ssh/id_rsa.pub

Your added SSH key should look something like this

push to git

Continue, we add new SSH key to the SSH agent

$ ssh-add ~/.ssh/id_rsa

Success:

Identity added: /home/tphuong_k/.ssh/id_rsa ([email protected])

Now you can try to add a new repository

push to git

Go to your terminal

# Make a new folder then go to the new folder
$ mkdir work-repo
$ cd work-repo
# Initialize git
$ git init
# Create a new file to push
$ touch text.txt
# Git add and commit
$ git add text.txt
$ git commit -m "first commit"
#Then add remote origin for local folder
$ git remote add origin [email protected]:TuanPhuongKieu/work-repo.git
# Now you can push to that repository
$ git push origin master
Push success message:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 219 bytes | 219.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:TuanPhuongKieu/work-repo.git
* [new branch]     master -> master

 

Now for the second SSH key, generate another key:

$ ssh-keygen -t rsa -C "[email protected]"

This time, we don’t want to leave a blank or typing random thing in there, otherwise it would orverwrite the id_rsa file that we have generated earlier

push to git

Instead, we include the path to the new file, like:

push to git

Carry on the next steps on creating new SSH key:

push to git

Use $ ls -al ~/.ssh you can see the key we have just created:

total 32
drwx------ 2 tphuong_k tphuong_k 4096 Jan 14 16:51 .
drwxr-x--- 46 tphuong_k tphuong_k 4096 Jan 14 16:39 ..
-rw------- 1 tphuong_k tphuong_k 2610 Jan 14 16:11 id_rsa
-rw------- 1 tphuong_k tphuong_k 2602 Jan 14 16:51 id_rsa_personal
-rw-r--r-- 1 tphuong_k tphuong_k 572 Jan 14 16:51 id_rsa_personal.pub
-rw-r--r-- 1 tphuong_k tphuong_k 578 Jan 14 16:11 id_rsa.pub
-rw------- 1 tphuong_k tphuong_k 978 Jan 14 16:41 known_hosts
-rw-r--r-- 1 tphuong_k tphuong_k 142 Jan 14 16:41 known_hosts.old

Continue to add the id_rsa_personal to your other github account

And add the new to ssh agent:

$ ssh-add /home/tphuong_k/.ssh/id_rsa_personal

Next go to your /.ssh folder, and create config file, just a file named config with no file type:

push to git

We will need this config file to separate the two keys, open config file in text editor and add:

# Work Account
Host github.com
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa

# Personal Account
Host github-personal-account
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal

In this config file we separate two blocks for two account, Host github.com and Host github-personal-account , you can name them however you prefer, and these are how you can identify where you going to add git remote origin in the future.

Saved them. Now go to the second git account and create a new repo, do the same steps as the first repo, but for the remote origin we don’t want add:

git remote add origin [email protected]:KieuTuanPhuong/personal-repo.git

Replace [email protected] with git@github-personal-account:

git remote add origin git@github-personal-account:KieuTuanPhuong/personal-repo.git

After adding remote origin, we can use git config --list to see current remote origin for the new folder

push to git

Then you should be able to push to the second account.

push to git

And there you go, we have finish setup two identical SSH keys for two distinctive Github accounts, you can further extend to add more key pairs following this tutorial as your need.

 


Subscribe

Subscribe to our newsletter and never miss out lastest news.