Configure multiple SSH keys for different Github accounts

dim. 27 août 2017 by Mick Cherry

This is my first article, so I wanted to start with an easy one :)

You can not add the same SSH key on two different github accounts. So, if you want to work with multiple github accounts on the same computer, you can follow this article!

Creation of SSH keys

You have to create as many keys as you have github account:

ssh-keygen -t rsa -b 4096 -C "your@email.com"

When the following message appears:

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

You can create for example: /home/alex/.ssh/id_rsa_perso and /home/alex/.ssh/id_rsa_pro

Once your keys have been created you must give them to the SSH agent:

ssh-add ~/.ssh/id_rsa_perso
ssh-add ~/.ssh/id_rsa_pro

You can check if the keys are well saved:

$ ssh-add -l

You should get:

4096 SHA256:FPZNDY51DIegeRYTmMtQcyLFaDCfFHufI82VvFF4kA /home/alex/.ssh/id_rsa_perso (RSA)
4096 SHA256:kraCXGlm65gGDlemKeQTmun0cuwmGuyetHYYF7PL+/A /home/alex/.ssh/id_rsa_pro (RSA)

In my case this didn't work the first time, I got this message: Could not open a connection to your authentication agent. You have to reload SSH agent:

$ eval `ssh-agent` 

Add an SSH key on github account

You can follow this tutorial or copy directly your SSH public key

cat /home/alex/.ssh/id_rsa_perso.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDEyTFCv/2rPQZq36g0LdX0bAnzqvpw1hUtjjjthzraeaaeadqdqdRI1Bceeevfefgzzfzreeghtiz03uQ4P3GPB9fyjIAJiR2BFcFZlVyJINlMca6tjthJZtblrhNZJL4OlgJL1bsSr4cXG1RK6oKspNDoXoid/u5VnhCXUnyHmpSCb2+Yl9wSVKONzB6DFxHlbXySZj9auTkqrWlLuguPjyjtWfUJv6I7TE6+eyryht/YGNlKxIO0YvLxdVkqDS/gVQCF/uWFegegegetJiCwiQWKvnzUEZEB3nP+rdqfssfegr/1BbJrr67/mmNFBZayxFVVCpgmPQ1IVATdTnlsI1epHfRtV1IeA+girNPsqb8yu29YfUOLNcIBAsLG4pvMlozrrCu9pivabPGWcD//XW4h+rcmBRZliVJrqzPdGhEvQzIDyPIBOZThar94kPvEUDJNZNbvLy9WTvPTbMaHbNc914VhxxQdN4U/2XuHmZTn6T29Fx3hLlJtKP84g+QFIdEmcvBR9Giqz37ekNI7YiEldFOKbQhUKKzAbNbAxqQ== alex@gmail.com

and go to your github settings

github_setting

After go to github_ssh_keys_settings settings and clic on github_ssh_add_key2

Now, you can past your public SSH key

github_ssh_add_key_2

Configure SSH settings

To configure these keys with a github account, you have to create the config file:

cd ~/.ssh/ 
touch config
vi config 

and add:

#Professional account
Host github.com-pro
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_pro

#Personal account
Host github.com-perso
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_perso

Configuration of local git repository

If your git isn't already cloned

You can clone it, but be careful, don't forget to have the correct host github.com-perso specified in the SSH config file

git clone git@github.com-perso:user/name_of_directory.git

If the git repository is already cloned

cd name_of_directory
vi .git/config

and add the good hostname in the remote origin url

[remote "origin"]
    url = git@github.com-perso:user/name_of_directory.git

Configure name and email of your git repositories

You can put specific name and email in each repository:

cd ~/name_of_directory
git config user.name "username"
git config user.email "user@email.com" 

You can now push and pull in all your git repositories Like a boss! cat_sunglasses

sources : here,here,here and here