Multiple SSH keys for different Git accounts

vinhle

Vinh Le

Posted on September 11, 2022

Multiple SSH keys for different Git accounts

🗒️ Content

  1. Before SSH Keys Setup
  2. Connecting to GitLab with SSH
  3. Connecting to GitHub with SSH
  4. A Better Keys Organization

1. Before SSH Keys Setup

  • Checking for existing SSH keys file
$ ls ~/.ssh
id_rsa  id_rsa.pub  known_hosts
Enter fullscreen mode Exit fullscreen mode
  • Confirm active public SSH keys (*.pub) in ssh-agent
$ ssh-add -L
Enter fullscreen mode Exit fullscreen mode
  • Remove all old keys
$ rm -r ~/.ssh
Enter fullscreen mode Exit fullscreen mode

2. Connecting to GitLab with SSH

  • Step 1: Generate an SSH key pair
$ ssh-keygen -t ed25519 -C "<your_email_01>@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/<user>/.ssh/id_ed25519): /home/<user>/.ssh/id_ed25519_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Enter fullscreen mode Exit fullscreen mode
  • Step 2: Add an SSH key to your GitLab account
$ cat ~/.ssh/id_ed25519_gitlab.pub
# or
$ xclip -sel clip < ~/.ssh/id_ed25519_gitlab.pub
Enter fullscreen mode Exit fullscreen mode

Copy the contents of your public key file by manually or a script. Then coming to GitLab > Preferences > SSH Keys

  • Step 3: Verify that you can connect
$ ssh -T git@gitlab.com
...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Welcome to GitLab, <user_name>!
Enter fullscreen mode Exit fullscreen mode

3. Connecting to GitHub with SSH

  • Step 1: Generate an SSH key pair
$ ssh-keygen -t ed25519 -C "<your_email_02>@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/<user>/.ssh/id_ed25519): /home/<user>/.ssh/id_ed25519_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Enter fullscreen mode Exit fullscreen mode
  • Step 2: Add an SSH key to your GitLab account
$ cat ~/.ssh/id_ed25519_github.pub
# or
$ xclip -sel clip < ~/.ssh/id_ed25519_github.pub
Enter fullscreen mode Exit fullscreen mode

Copy the contents of your public key file by manually or a script. Then coming to GitHub > Settings > SSH and GPG keys

  • Step 3: Verify that you can connect
$ ssh -T git@github.com
...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Hi <user_name>! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

4. A Better Keys Organization

For more information on these settings, see the man ssh_config

$ cd ~/.ssh/
$ touch config
$ vi config
# GitLab
Host gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_gitlab

# GitHub
Host github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_github
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
vinhle
Vinh Le

Posted on September 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related