Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitLab without supplying your username and personal access token at each visit.
1) Checking for existing SSH keys
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
1: Open Terminal
2: Enter the cmd to see if existing SSH keys are present:
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
3: 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:
2) Generating a new SSH key and adding it to the ssh-agent
If you don’t already have an SSH key, you must generate a new SSH key. If you’re unsure whether you already have an SSH key, check for existing keys.
If you don’t want to reenter your passphrase every time you use your SSH key, you can add your key to the SSH agent, which manages your SSH keys and remembers your passphrase.
1: Open Terminal
2: Paste the text below, substituting in your GitLab email address.
$ ssh-keygen -t ed25519 -C "firstname.lastname@example.org"
Note: If you are using a legacy system that doesn’t support the Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C "email@example.com"
This creates a new ssh key, using the provided email as a label.
> Generating public/private ed25519 key pair.
3: When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
> Enter a file in which to save the key (/home/you/.ssh/id_ed25519): [Press enter]
3) Adding a new SSH key to your GitLab account
To configure your GitLab account to use your new (or existing) SSH key, you’ll also need to add it to your GitLab account.
After adding a new SSH key to your GitLab account, you can reconfigure any local repositories to use SSH
1: 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.
$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)$ xclip -selection clipboard < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboard
xclipisn't working, you can locate the hidden
.sshfolder, open the file in your favorite text editor, and copy it to your clipboard.
2: Now login form Gitlab and move inside your project, In the left navigation menu, click on the Settings then click Repository.
3: Click Expend button for Deploy keys
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 key button