Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

GitLab SSH Keys: Setup and Fix Common Git Errors

· 5 min read · Home Tech

In this article
  1. Why SSH Matters for GitLab
  2. How SSH Authentication Works
  3. Step-by-Step: Setting Up SSH Keys
  4. Common Git Errors and How to Fix Them
  5. Choosing Between SSH and HTTPS
  6. Security Best Practices for SSH Keys
  7. Frequently Asked Questions

To get GitLab SSH working, you generate an SSH key pair, add the public key to your GitLab account, and configure your local Git to use SSH URLs. This article walks through the complete setup and explains how to resolve the most frequent Git errors that occur when cloning, pushing, or pulling from GitLab.

Why SSH Matters for GitLab

GitLab supports two main protocols for Git operations: HTTPS and SSH. HTTPS requires you to enter your username and password (or a personal access token) each time you interact with the remote repository. SSH, on the other hand, uses cryptographic keys to authenticate you automatically. Once set up, you can push and pull without typing credentials, making it faster and more convenient for frequent work.

SSH also provides a secure channel between your computer and GitLab's servers. The key pair consists of a private key that stays on your machine and a public key that you upload to GitLab. When you connect, GitLab verifies that the private key matches the public one, granting access without exposing your password.

How SSH Authentication Works

When you run git clone git@gitlab.com:username/repo.git, Git uses the SSH protocol to connect to GitLab. The process involves:

  • Your SSH client sends a connection request to GitLab's SSH server (usually on port 22).
  • GitLab responds with a challenge that only the holder of the matching private key can answer.
  • Your client signs the challenge with your private key and sends it back.
  • GitLab verifies the signature against the public key you uploaded, and if it matches, grants access.

This handshake happens automatically, so you don't need to enter a password. If your private key has a passphrase (recommended), you may be prompted to enter it once per session, or you can use an SSH agent to remember it.

Step-by-Step: Setting Up SSH Keys

Here's the standard procedure for Linux, macOS, and Windows (with Git Bash or PowerShell).

  1. Generate a new SSH key pair (if you don't have one). Open a terminal and run:
    ssh-keygen -t ed25519 -C "your_email@example.com"
    Use your GitLab email address. Press Enter to accept the default file location (~/.ssh/id_ed25519). Set a strong passphrase when prompted.
  2. Start the SSH agent (optional but helpful) and add your key:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
  3. Copy the public key to your clipboard:
    cat ~/.ssh/id_ed25519.pub
    Select and copy the entire output (it starts with ssh-ed25519).
  4. Add the key to GitLab: Log in to GitLab, go to Preferences > SSH Keys (or Settings > SSH Keys), paste the key into the Key field, give it a descriptive title (like "My Laptop"), and click Add key.
  5. Test the connection: Run ssh -T git@gitlab.com (replace with your GitLab instance URL if self-hosted). You should see a welcome message like "Welcome to GitLab, @username!".
  6. Use SSH URLs: When cloning or setting a remote, use the SSH format: git@gitlab.com:username/repo.git instead of https://gitlab.com/username/repo.git.

Common Git Errors and How to Fix Them

Even with keys set up, you may encounter errors. Here are the most frequent ones and their solutions.

1. Permission denied (publickey)

This error means GitLab couldn't verify your key. Check that:

  • You added the correct public key to GitLab (not the private key).
  • The key file is in the default location and has correct permissions (chmod 600 ~/.ssh/id_ed25519).
  • You are using the right GitLab host (gitlab.com vs. your company's instance).

If you have multiple keys, you may need to configure ~/.ssh/config to specify which key to use for GitLab.

2. Host key verification failed

This happens when the remote host's key is not in your known_hosts file. Run ssh-keyscan gitlab.com >> ~/.ssh/known_hosts to add it, or connect manually and accept the prompt.

3. Repository not found

This error often means the repository doesn't exist, or you don't have access. Verify the URL and your permissions. If you're using a self-hosted GitLab, ensure the project path is correct.

4. The authenticity of host ... can't be established

This is a security prompt on the first connection. Type yes to continue. If it appears every time, your known_hosts file may be misconfigured.

5. Git push/pull asks for username/password

This usually means your remote URL is HTTPS, not SSH. Check with git remote -v and change to SSH with:
git remote set-url origin git@gitlab.com:username/repo.git

Choosing Between SSH and HTTPS

SSH is ideal for developers who work daily and want passwordless operations. HTTPS is easier for occasional users or those behind strict firewalls that block port 22. GitLab also supports HTTPS with personal access tokens, which is a good alternative if SSH is blocked. For most professional workflows, SSH is recommended due to its convenience and security.

Security Best Practices for SSH Keys

  • Use a strong passphrase for your private key. If someone steals the key file, they still need the passphrase.
  • Never share your private key or commit it to a repository.
  • Use different keys for different services (e.g., one for GitLab, one for GitHub).
  • Revoke keys you no longer use by removing them from GitLab.
  • Use ed25519 keys instead of RSA when possible; they are faster and more secure.

Frequently Asked Questions

Can I use the same SSH key for GitLab and GitHub?

Technically yes, but it's not recommended. If one service is compromised, the attacker could potentially use the key on the other. Using separate keys for each service is a safer practice.

What if I already have an SSH key but forgot its passphrase?

You cannot recover a lost passphrase. You'll need to generate a new key pair and add the new public key to GitLab. Remove the old key from GitLab to avoid confusion.

Why does Git still ask for a password after I set up SSH?

Check that your remote URL uses SSH (not HTTPS). Run git remote -v and update if necessary. Also ensure your SSH agent is running and your key is added.

How do I change my GitLab remote from HTTPS to SSH?

Use the command git remote set-url origin git@gitlab.com:username/repo.git. Then verify with git remote -v.

What is the difference between id_rsa and id_ed25519?

Both are key types. RSA is older and widely supported, while Ed25519 is newer, faster, and considered more secure. GitLab supports both, but Ed25519 is recommended for new keys.

Can I use SSH on Windows?

Yes. Windows 10 and later include OpenSSH client. You can use PowerShell or install Git for Windows, which includes SSH. The steps are the same as on Linux/macOS.