SSH login to GitHub and GitLab
SSH login to GitHub and GitLab
March 23, 2024
Generate a pair of SSH keys
ssh-keygen -t ed25519 -C "your_email@example.com"The SSH agent will ask you to enter a location to save the keys. e.g. /home/user/.ssh/id_ed25519. Passphrase is optional.
Then there will be two SSH key files:
~/.ssh/id_ed25519is the private key. Protect it at all costs.~/.ssh/id_ed25519.pubis the public key.
Using different keys for GitHub and GitLab access is more secure. However, the same pair of keys is used for this demonstration purposes.
Add remote to the SSH settings
Edit ~/.ssh/config
mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/configAdd the following content to set the private key as the IdentityFile.
~/.ssh/config
Host GitHub
HostName github.com
IdentityFile ~/.ssh/id_ed25519
Host GitLab
HostName gitlab.com
IdentityFile ~/.ssh/id_ed25519Add the SSH key to your GitHub account
According to 📖 Github docs, add the SSH key here
- Paste the content of the public key,
~/.ssh/id_ed25519.pubto the key field. - Add a descriptive label for the new key in the “Title” field.
- Finally, click the
Add SSH keygreen button. If prompted, confirm your GitHub password.
Add the SSH key to your GitLab account
Add the SSH key here
- Paste the content of the public key,
~/.ssh/id_ed25519.pubto the key field. - Add a descriptive label for the new key in the “Title” field.
- Select an expiration date.
- Finally, click the
Add keybutton.
Test your setup
GitHub:
ssh -vT git@github.comAccept its fingerprint if prompted. If you see “Hi user! You’ve successfully authenticated, but GitHub does not provide shell access” that means login is successful.
GitLab:
ssh -vT git@gitlab.comAccept its fingerprint if prompted.
Last updated on