How to Setup SSH for GitHub and GitLab

Ruman
4 min readJan 12, 2025

--

Create and add SSH to your GitHub and Gitlab account in simple steps.

Photo by RealToughCandy.com

I’ve had these steps saved in my personal “Keep” notes for last few years and have use that every time I set up a new SSH key. I’m sharing this simple guide to help fellow developers, especially those setting up SSH keys for the first time or re-doing after a long time, to make the ssh setup more easier and straightforward.

Content Outline

  • Generating a New SSH Key
  • Adding the SSH Key to the SSH Agent
  • Adding the SSH Key to GitLab or GitHub
  • Testing the SSH Connection
  • Configuring Multiple Keys with an SSH Config File

Generate a New SSH Key

i. Open Terminal on your Mac

ii. Run the following command to generate a new SSH key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

No need to stress about how the key is being created. To put it simply, RSA is the algorithm used to generate the key, and -b 4096 specifies the key size or length of the RSA key pair being created.

iii. Replace "your_email@example.com" with the email address associated with your GitLab/GitHub account.

iv. When prompted to “Enter a file in which to save the key”, type a unique name for the key (e.g., ~/.ssh/id_rsa_gitlab) to avoid overwriting your existing GitHub or other SSH keys:

/Users/your_username/.ssh/id_rsa_gitlab

‼️⚠️ Always ensure you specify a key name to avoid overwriting the default key. For example, if you’re creating a key for GitHub, use id_rsa_github as the key name.

v. When prompted for a passphrase, press Enter to skip or set a passphrase for added security.

This creates two files:

  • ~/.ssh/id_rsa_gitlab (your private key)
  • ~/.ssh/id_rsa_gitlab.pub (your public key)

Add the SSH Key to the SSH Agent

Ensure the SSH agent is running:

eval "$(ssh-agent -s)"

Add your new SSH key to the agent:

ssh-add -K ~/.ssh/id_rsa_gitlab

✅ The command ssh-add -K ~/.ssh/id_rsa_gitlab adds your SSH private key (in this case, ~/.ssh/id_rsa_gitlab) to the SSH agent, which manages your SSH keys. This step is essential for enabling seamless authentication using the added key.

Copy the Public Key

To copy the public key to your clipboard, run:

pbcopy < ~/.ssh/id_rsa_gitlab.pub

Add the SSH Key to GitLab or GitHub

Adding an SSH Key to GitLab: To add your SSH key, go to your profile, click on Preferences, then select SSH Keys. From there, you’ll find an option to add a new SSH key.

Picture by Author

Adding an SSH Key to GitHub: Click on your profile, select Settings, then navigate to SSH and GPG Keys. There, you’ll find an option to add a new SSH key.

Picture by Author

Test the SSH Connection

To confirm the SSH key works with GitLab, run

ssh -T git@gitlab.com

You should see a message like this:

Welcome to GitLab, @your_username!

And to confirm the SSH key works with GitHub, run

ssh -T git@github.com

You should see a message like this:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

If you don’t see this message when testing the connection, it’s likely that there was an error in the setup process.

Additional Configuration (Optional): Use config file for multiple keys

To ensure that the right SSH key is used for the correct service (GitHub vs GitLab), you can create an SSH config file.

Open your ~/.ssh/config file (create it if it doesn’t exist):

nano ~/.ssh/config

Add the following configurations:

# GitHub
Host github.com
User git
HostName github.com
IdentityFile ~/.ssh/id_rsa_github

# GitLab (assuming this was already set up)
Host gitlab.com
User git
HostName gitlab.com
IdentityFile ~/.ssh/id_rsa

Save and close the file (CTRL + X, then press Y and Enter).

With this setup, SSH will automatically use the correct key when you interact with GitHub or GitLab.

Cocnlusion

We covered how to set up an SSH key for GitLab and GitHub in a few simple steps. Remember to add the key to the SSH agent so it can be used for authentication.

If you enjoyed this article, your applause would be greatly appreciated!

--

--

Ruman
Ruman

Written by Ruman

Senior ML Engineer | Sharing what I know, work on, learn and come across :)

No responses yet