Key-based Authentication on GitLab#
Key-based authentication allows GitLab users to identify their device once on their account and avoid having to log in via their credentials every time. GitLab uses SSH protocol to communicate with Git.
SSH uses a public and a private key:
- The public key must be registered in GitLab, where it “identifies” your device.
- The private key must stay secret and preferably you should only keep it on a single device. The private key is the only key that can be used to authenticate your identity against the public key uploaded to GitLab – if it becomes public, anyone can impersonate your identity and thus use your GitLab account. Make sure you do NOT upload the private key anywhere by accident, not even to GitLab.
Prerequisites#
To use SSH, one of the following must be installed on your device:
- The OpenSSH client, which comes pre-installed on GNU/Linux, macOS, and Windows 10 via Powershell or WSL.
- SSH version 6.5 or later. Earlier versions used an MD5 signature, which is not secure.
To view the version of SSH installed on your system, run the following command in your terminal (on Linux or macOS) or Git Bash (on Windows):
ssh -V
Key Generation#
Please note that the steps below describe SSH key generation
If you already have one, your existing key may be overwritten.
Start a terminal
Type the following command at the command prompt and run it after replacing your firstname and lastname:
ssh-keygen -t ed25519 -C firstname.lastname@uni.lu
Accept the suggested filename and directory
Press
Enter
to continue. Defining a password is not necessary and can be skipped.To copy the contents of your public key file into the clipboard, run the following command:
- For macOS:
tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy
- For Windows:
cat ~/.ssh/id_ed25519.pub | clip
Saving the SSH Key on GitLab#
Once the content of the public key is copied in the clipboard, you need to register it with your GitLab account.
To save your key to your account:
Log in to GitLab using your LUMS account
Navigate to the
Preferences
menuUser settings will be displayed on the left-hand side menu. Click on the
SSH Keys
optionOn the
SSH Keys
page, click onAdd new key
button to add your public keyPaste your new key into the
Key
box, name the key and save it by clicking theAdd key
button
Troubleshooting#
Please refer to the GitLab Docs or create a ticket for further assistance regarding the key-based authentication.