Vinh Le
Posted on September 11, 2022
🗒️ Content
- Before SSH Keys Setup
- Connecting to GitLab with SSH
- Connecting to GitHub with SSH
- A Better Keys Organization
1. Before SSH Keys Setup
- Checking for existing SSH keys file
$ ls ~/.ssh
id_rsa id_rsa.pub known_hosts
- Confirm active public SSH keys (
*.pub
) in ssh-agent
$ ssh-add -L
- Remove all old keys
$ rm -r ~/.ssh
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:
- 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
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>!
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:
- 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
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.
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
💖 💪 🙅 🚩
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.