This is guide about how to configure multiple SSH keys for some Git host websites such as Github, Gitlab, among others.
-
Create SSH directory:
mkdir ~/.ssh
-
Move to created directory:
cd ~/.ssh
-
To create a SSH key, type:
ssh-keygen -t rsa -C "EMAIL@HOST.com"
a message will be displayed:
Generating public/private rsa key pair. Enter file in which to save the key (/home/USER/.ssh/id_rsa):
You should type someething of the default name of the file to distinguish service, such as:
id_rsa_myaccount_github
,id_rsa_myaccount_gitlab
,id_rsa_mycompanyaccount_gitlab
.After this step a passphrase is needed for security, which can be empty.
-
Repeat previous step for every required account.
-
To see if the keys were successful created:
ls ~/.ssh
which it is going to print all key files, for example:
id_rsa_myaccount_github id_rsa_myaccount_gitlab.pub id_rsa_myaccount_github.pub id_rsa_mycompanyaccount_gitlab id_rsa_myaccount_gitlab id_rsa_mycompanyaccount_gitlab.pub
-
To create
config
file:touch ~/.ssh/config
-
Edit the file to configure domains for the keys:
nano ~/.ssh/config
for example, if three accounts were added, should look like this:
# github account Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_myaccount_github # gitlab account Host gitlab.com HostName gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_myaccount_gitlab # gitlab company account Host gitlab.my_company.com HostName gitlab.my_company.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_mycompanyaccount_gitlab
-
Save file and exit.
A new host has to be created.
-
Create a new entry on the
~/.ssh/config
file(example with Github):
Host other.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_otheraccount_github
where other.github.com
is the alias for the host, although the server(HostName
) is
github.com
.
The IdentityFile
option points to a (previously) created key file configured with
the required account.
After this, a custom url can be used to clone the project.
git clone git@other.github.com:USER/REPOSITORY.git
where other.github.com
is the previously created domain.
To configure ssh on each repository website:
-
Copy the content of the
id_rsa_X.pub
withxclip
command(may not be installed) to the clipboard(whereid_rsa_X.pub
is the wanted key file):xclip -sel clip < ~/.ssh/id_rsa_X.pub
(content also can also be manually copied from the
*.pub
file) -
Paste the content on the repository site, check next section.
-
Go to https://github.com.
-
Go to Profile Settings > SSH and GPG Keys > click on button New SSH Key.
-
On Title add a descriptive label, such as the hostname of the device…
-
On the Key field past the clip content with the key.
-
Finally click on Add SSH key and after that the site ask for the user password.
-
Go to https://gitlab.com.
-
Go to Profile Settings > SSH Keys.
-
On the Key field past the clip content with the key.
-
On Title add a descriptive label, such as the hostname of the device…
-
Finally click on Add key.
-
Type(substitute
HOST
with the desired one(github, gitlab, …)):ssh -T git@HOST.com
a warning will appear, accept it with
yes
:The authenticity of host 'HOST.com (IP ADDRESS)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)?
A successful message will appear:
-
For Github:
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
-
For Gitlab:
Welcome to GitLab, USERNAME!
-
If the SSH does not work, maybe the keys need to be added with ssh-add
command
-
First delete keys cache:
ssh-add -D
if a message appear:
Could not open a connection to your authentication agent.
use this command and after that retry:
eval `ssh-agent -s`
-
Add key file with
ssh-add
command:ssh-add ~/.ssh/id_rsa_file
-
To see added keys, type:
ssh-add -ld
and something such as this will be displayed:
2048 SHA256:DXlgYQo1o/65JQCSYQo/L4RRP4i+wTouyEetkOIcn/o EMAIL_1 (RSA) 2048 SHA256:4FPtZYDtHipZeHqP9KNB3Wslz9L5q/JoAGT3g/NW3O8 EMAIL_2 (RSA) 2048 SHA256:tXCoBI2dMtTFhUhE5oBT+XwwkrhkorkOHbSc1J22urQ EMAIL_3 (RSA)
-
Retry testing connection.
To use a git repository with the SSH, this url style has to be used for the repository:
git@HOST:USERNAME/REPOSITORY.git
where HOST
is the configured domain, which can be github, gitlab or a
personalized one.
If project origin is already configured with HTTPS, it has to be changed to the SSH url style (check next section).
Warning
|
If you want to use the HTTPS url, other steps will be required. |
-
List existing remotes in order to get the name of the repository:
git remote -v
-
Change remote url, substitute HOST for server domain or a previously create custom HOST:
git remote set-url origin git@HOST:USERNAME/REPOSITORY.git
Note
|
It can be used the same method to change from SSH to HTTPS. |
In spite SSH keys were configured for the access, the Git user name and email need to be configured, because these will be associated to the commits.
To see actual configuration, type:
git config --list
If global user name and email were configured will be displayed at the beginning, if not, these values will not appear.
Note
|
If the command was run on a repository and this one has a local user name and email configured, these values will be displayed at the end of configuration. |
Important
|
You may want to configure the most used user name and email globally, but watch out since every commit with not local configuration will use these values. |
-
Go to the root directory of the repository.
-
Type to configure user name:
git config --global user.name "YOUR NAME"
-
Type to configure user email:
git config --global user.email "email@HOST.com"
-
To see if the global fields were correctly configured, use the
git config --list
command or check global Git file.nano ~/.gitconfig
this will display a section like:
[user] name = YOUR NAME email = email@HOST.com
NoteIf the user name or the user email were not configured this section will not appear.
-
Go to the root directory of the repository.
-
Type to configure user name:
git config user.name "YOUR NAME"
-
Type to configure user email:
git config user.email "email@HOST.com"
-
To see if the global fields were correctly configured, use the
git config --list
command or check local Git file:nano ./.git/config
this will display a section like:
[user] name = YOUR NAME email = email@HOST.com
NoteIf the user name or the user email were not configured this section will not appear.