Explanation
To fully understand what is happening here you need to have a bit of a background. This means it is perfectly fine and acceptable to just follow these steps not knowing what you are doing :) The main goal is to make everyone’s live easier! Don’t worry about the details.
The idea with Git is that we ‘push’ and ‘pull’ files from and to a server. Everytime we do this we need to tell the server “hey this is john, here are my files”. We do this by authenticating to the server. Essentially we will configure our computer in such a way that we won’t have to type our username/password everytime we do something with Git on our GitLab server. SSH-keys will help us: everyone will have their own key pair. The private one is, well… private (meaning: DO NOT SHARE THE PRIVATE KEY). If you sign a message with your private key, any other system can verify it’s you by checking if it fits your public key. The public key is public :).
Installation
- Open
Git Bashby pressing on the Windows (start icon) and searching forGit bash. A black screen should open. This is called a command prompt. We will need it to run commands. - Generate a SSH key pair. There are several underlying algorithms for generating a key pair. The most used ones are
RSAandED25519. We recommend to useED25519:- https://docs.gitlab.com/ee/ssh/#ed25519-ssh-keys
- https://docs.gitlab.com/ee/ssh/#common-steps-for-generating-an-ssh-key-pair
- Add your public key to GitLab, so GitLab can verify your private key: Now you can copy your public SSH key you created to your GitLab account. To do so, follow these steps: copy your public SSH key to a location that saves information in text format.
- The following options copies your public ED25519 key to the clipboard for the noted operating system (if you’re using an RSA key, substitute accordingly):
- MacOS:
pbcopy < ~/.ssh/id_ed25519.pub - Linux (requires the xclip package):
xclip -sel clip < ~/.ssh/id_ed25519.pub - Git Bash on Windows:
cat ~/.ssh/id_ed25519.pub | clip
- MacOS:
- Navigate to https://gitlab.com and sign in.
- Select your avatar in the upper right corner, and click
Settings - Click
Access > SSH Keys. - Click
Add new key - Paste the public key that you copied into the
Keytext box. - Make sure your key includes a descriptive name in the
Titletext box, such as Work Laptop or Home Workstation. - Include an (optional) expiry date for the key under
“Expires at”section. (Introduced in GitLab 12.9.) - Click the
Addkey button.
Testing setup
To test whether your SSH key was added correctly, run the following command in your terminal (replacing gitlab.com with your GitLab’s instance domain):
ssh -T git@gitlab.com
The first time you connect to GitLab via SSH, you should verify the authenticity of the GitLab host that you’re connecting to. For example, when connecting to gitlab.com, answer yes to add gitlab.com to the list of trusted hosts:
The authenticity of host 'gitlab.com (193.191.179.153)' can't be established.
ED25519 key fingerprint is SHA256:3rYn6X8aEo5dqIiM+59Bci3HwMozUTBQnHR95K/+Yck.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Once added to the list of known hosts, you should validate the authenticity of GitLab’s host again. Run the above command once more, and you should only receive a Welcome to GitLab, @username! message.
If the welcome message doesn’t appear, you can troubleshoot the problem by running ssh in verbose mode with the following command:
ssh -Tvvv git@gitlab.com