Skip to content

Instantly share code, notes, and snippets.

@marcoandre1
Last active June 10, 2024 04:03
Show Gist options
  • Save marcoandre1/4b0fbca83104e08d3e729a25a0cba4eb to your computer and use it in GitHub Desktop.
Save marcoandre1/4b0fbca83104e08d3e729a25a0cba4eb to your computer and use it in GitHub Desktop.
Managing SSH keys for Github and Gitlab
# GITHUB
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_hub
# GITLAB
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_lab

Managing SSH keys for Github and Gitlab

NOTICE: This guide will help you set ssh keys for GitHub and GitLab. However, this is not going to change your commit user.name or user.email. If you need to change those for specific repositories, just run the following commands while in your repository:

git config user.name "Your Name Here"
git config user.email your@email.com

For more info, see this answer. Also, keep in mind this only changes the .git folder inside your repository which never gets added/committed/pushed/uploaded.

I recently had to manage two ssh keys (one for Github and one for Gitlab). I did some research to find the best solution. I am justing putting the pieces together here.

The first question you can ask yourself is can you have the same ssh key for both Github and Gitlab? The answer is yes but it is not advisable.

The best answer is that you should set one ssh key for Github and another one for Gitlab. The first thing to do is install Git if you haven't. Next, you should check for existing ssh-keys on your system:

  1. Open Git Bash.
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present:
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
  1. Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key:

  1. Open Git Bash.
  2. Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -f ~/.ssh/id_ed25519_hub -t ed25519 -C "your_email@example.com"

Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

$ ssh-keygen -f ~/.ssh/id_rsa_hub -t rsa -b 4096 -C "your_email@example.com"

This creates a new ssh key, using the provided email as a label.

> Generating public/private ed25519 key pair.
  1. Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_hub in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_hub
  1. Open Git Bash.
  2. Copy the SSH public key to your clipboard.

If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.

$ clip < ~/.ssh/id_ed25519_hub.pub
# Copies the contents of the id_ed25519_hub.pub file to your clipboard

Tip: If clip isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  1. In the upper-right corner of any page, click your profile photo, then click Settings.
  2. In the user settings sidebar, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal Mac, you might call this key "Personal MacBook Air".
  5. Paste your key into the "Key" field.
  6. Click Add SSH key.
  7. If prompted, confirm your GitHub password.
  1. Open Git Bash.
  2. Paste the text below, substituting in your GitLab email address.
$ ssh-keygen -f ~/.ssh/id_ed25519_lab -t ed25519 -C "your_email@example.com"
  1. Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_lab in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_lab

To use SSH with GitLab, copy your public key to your GitLab account.

  1. Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:

Git Bash on Windows:

cat ~/.ssh/id_ed25519_lab.pub | clip
  1. Sign in to GitLab.
  2. In the top right corner, select your avatar.
  3. Select Settings.
  4. From the left sidebar, select SSH Keys.
  5. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-ed25519 or ssh-rsa, and may end with a comment.
  6. In the Title text box, type a description, like Work Laptop or Home Workstation.
  7. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only, and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys.
  8. Select Add key.

Generally, in Windows machine, the SSH config file stored in the following location: /c/Users/PC_USER_NAME/.ssh/

Just follow the steps in below (if you're using the Git Bash):

  1. Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here".
  2. Create a file named "config" with the following command:
$ touch config
  1. Now open the config file with the command:
$ nano config
  1. Now write the following lines inside the config file:

Let's assume you've created two files named id_ed25519_hub for Github and id_ed25519_lab for GitLab.

# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_hub

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_lab

How can I save the file?

F3 will let you save without exiting. Otherwise, Ctrl + X will prompt you if you've made changes. Press Y when it asks, and Enter to confirm the filename.

How can I quit the editor without saving the changes?

Ctrl + X, then N when it asks if you want to save.

@lazytownfan
Copy link

lazytownfan commented Dec 18, 2021

Thank you for this guide!

This will also work if you manage 2 or more different accounts on the same site, such as GitHub - which is what I needed.

@lazytownfan
Copy link

By the way, am I remembering the following correctly?
I thought Windows file paths used back slashes, i.e. \, when using its Command Prompt - since there's a little mnemonic to remembering this:

"Windows thinks backwards and is old fashioned, so it uses backslashes. On the other hand, Linux and macOS/OS X (both based on Unix) are forward thinking, so they use forward slashes."

@marcoandre1
Copy link
Author

Hello @lazytownfan! Glad to hear that this Gist was helpful.

To answer your question about back slashes \, I do remember having problems with back/forward slashes in the past when using Windows, but it has been a while. I tested PowerShell, cmd and File Explorer and they all accepted forward slashes. However, they do show the paths with back slashes (even when you enter a path with forward slashes).

Some users reports forward slashes work for some Windows tools since 2003, and it seems to be a consensus that you can use both slashes for Windows at least since 2018.

@abuabdirohman4
Copy link

thank you, this is very helpful, stars this repo 😁

@Klopotek0
Copy link

I did everyting like you, but got: git@gitlab.com: Permission denied (publickey).
Github key works well. Why is that ?

@marcoandre1
Copy link
Author

Hello @Klopotek0, I googled your error and found a lot of possible problems/solutions. Could you give more information? When does this happen? Is it when you are trying to push or pull? Is there an additional error/warning?

Can you connect to GitLab using ssh? Here is how from the official documentation (Verify that you can connect):

  1. Open a terminal and run this command:
ssh -T git@gitlab.com
  1. It should prompt for your passphrase (if you have one). Enter the passphrase and press Enter.
Enter passphrase for key 'C:\Users\<user>\.ssh\id_ed25519_lab':

Note: If it’s your first time connecting, you are probably going to get prompted if you want to connect, tap yes and press Enter.

  1. You should get the following message:
Welcome to GitLab, @username!

If the welcome message doesn’t appear, you can troubleshoot by running the following command:

ssh -Tvvv git@gitlab.com

@Klopotek0
Copy link

It's actually really strange, because
ssh -T git@gitlab.com
raises this error, but I worked on my repo and everything works as it should. I can push and pull. No idea why it denies me by above command.

@fedeartia
Copy link

Thank you, kind sir, for this tutorial.

@RashJrEdmund
Copy link

thank you sir, for this guidlines

@KrauserHuang
Copy link

Hi,
When I type ssh -T git@gitlab.com
It comes up with below information,
anyone know what to do?

The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

@Vitor0liveira
Copy link

Hi @marcoandre1, thank you for sharing this tutorial! It was helpful. I just wanted to point out a silly fix regarding the copy command. You can replace clip with pbcopy.

@xChAmeLIoNz
Copy link

Hi, When I type ssh -T git@gitlab.com It comes up with below information, anyone know what to do?

The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

You have to type 'yes' to proceed or 'no' to cancel.

@ajaygohil728
Copy link

Thank you, sir for this informative tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment