Setting Up SSH Authentication for GitHub and GitLab

#ssh#ssh-keys#Git#GitHub
Setting Up SSH Authentication for GitHub and GitLab

SSH authentication makes working with GitHub and GitLab more convenient and secure by allowing Git to authenticate using an SSH key instead of repeatedly entering credentials.

This guide walks through generating an Ed25519 SSH key, configuring Git, adding the key to an SSH agent, and testing authentication with both GitHub and GitLab.

1. Configure Your Git Identity

First, configure the name and email Git will associate with your commits:

bash
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

To verify the configuration:

bash
git config --global --list

Common mistake

Do not use:

bash
git config --global list

The correct command is:

bash
git config --global --list

The

Code
--
before
Code
list
is important because
Code
--list
is a Git configuration option.


2. Generate an Ed25519 SSH Key

Ed25519 is a modern SSH key type that provides strong security with a relatively small key size.

Run:

bash
ssh-keygen -t ed25519 -C "your_email@example.com"

Alternatively, you can use a descriptive comment:

bash
ssh-keygen -t ed25519 -C "GitHub-GitLab"

When prompted:

text
Enter file in which to save the key:

Press Enter to use the default location:

text
~/.ssh/id_ed25519

You'll then be asked for a passphrase.

Using a passphrase is recommended because it provides additional protection if someone gains access to your private key file.


3. Start the SSH Agent

Start the SSH authentication agent:

bash
eval "$(ssh-agent -s)"

You should see something similar to:

text
Agent pid 123

The process ID will be different on every system.


4. Add Your SSH Key

Add the private key to the SSH agent:

bash
ssh-add ~/.ssh/id_ed25519

You should see a confirmation similar to:

text
Identity added: /c/Users/username/.ssh/id_ed25519

5. Copy Your Public Key

Display the public key:

bash
cat ~/.ssh/id_ed25519.pub

The output will look similar to:

text
ssh-ed25519 AAAA... your_comment

Important security note

The

Code
.pub
file contains your public key and can be added to GitHub or GitLab.

Never share your private key.

Do not publish or send:

text
~/.ssh/id_ed25519

Only the following is intended to be shared:

text
~/.ssh/id_ed25519.pub

6. Add the Key to GitHub

Sign in to GitHub and open:

Settings → SSH and GPG keys → New SSH key

Give the key a recognizable title, such as:

text
Windows PC

Paste the contents of:

bash
cat ~/.ssh/id_ed25519.pub

into the key field and save it.


7. Test GitHub Authentication

Run:

bash
ssh -T git@github.com

The first time you connect, SSH may ask you to confirm GitHub's host authenticity.

After successful authentication, GitHub should respond with a message indicating that authentication succeeded and that GitHub does not provide shell access.

That message is normal.


8. Add the Key to GitLab

Sign in to GitLab and go to:

Preferences/Settings → SSH Keys

Add the contents of:

bash
cat ~/.ssh/id_ed25519.pub

Give it a descriptive title, for example:

text
Windows PC

Then save the key.


9. Test GitLab Authentication

Run:

bash
ssh -T git@gitlab.com

After successful authentication, GitLab should display a welcome message confirming your account.

If you initially receive:

text
Permission denied (publickey).

check that:

  1. The public key was added to the correct GitLab account.

  2. The SSH agent is running.

  3. The key has been added with

    Code
    ssh-add
    .

  4. You're using the correct SSH key.

  5. Your SSH configuration isn't selecting another key.

You can check loaded keys with:

bash
ssh-add -l

10. Use SSH URLs for Git Repositories

Once SSH authentication is configured, clone repositories using their SSH URLs.

GitHub

bash
git clone git@github.com:USERNAME/REPOSITORY.git

GitLab

bash
git clone git@gitlab.com:USERNAME/REPOSITORY.git

For an existing repository, check the remote:

bash
git remote -v

If it is using HTTPS and you want to switch to SSH:

bash
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

For GitLab:

bash
git remote set-url origin git@gitlab.com:USERNAME/REPOSITORY.git

11. Verify Everything

You can verify your Git configuration:

bash
git config --global --list

Check the SSH agent:

bash
ssh-add -l

Test GitHub:

bash
ssh -T git@github.com

Test GitLab:

bash
ssh -T git@gitlab.com

If all three checks work, your Git environment is ready to use SSH authentication.


Troubleshooting

Code
Permission denied (publickey)

Run:

bash
ssh-add -l

If no identities are listed:

bash
ssh-add ~/.ssh/id_ed25519

Then test again:

bash
ssh -T git@gitlab.com

Multiple SSH keys

If you use different SSH keys for different accounts, create an SSH configuration file:

text
~/.ssh/config

Example:

sshconfig
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519

Then test each service again.


Final Checklist

  • [ ] Configure Git username

  • [ ] Configure Git email

  • [ ] Generate an Ed25519 SSH key

  • [ ] Start

    Code
    ssh-agent

  • [ ] Add the key with

    Code
    ssh-add

  • [ ] Add the public key to GitHub

  • [ ] Add the public key to GitLab

  • [ ] Test GitHub with

    Code
    ssh -T git@github.com

  • [ ] Test GitLab with

    Code
    ssh -T git@gitlab.com

  • [ ] Use SSH repository URLs for Git operations

Once configured, GitHub and GitLab can authenticate your Git operations through SSH without requiring you to enter your account password for every operation.