Connecting to GitLab using SSH
When interacting with GitLab at EMBL using git
, two protocols are available, HTTPS and SSH.
The first is convenient for a quick interaction. However, it quickly gets annoying as it will ask for your credentials every time. A more convenient and versatile option is SSH but it requires going through a few steps to configure it.
If incorrectly configured, you will likely see the error message: Permission denied (public key). fatal: Could not read from remote repository
.
So lets get started with step-by-step instructions to create an SSH key and configure a new computer to clone or pull/push from git.embl.de using the SSH protocol:
-
Open your terminal application on your computer or
Git Bash
(notGit CMD
) if you are on Windows. -
Type the following command to generate a new SSH key.
ssh-keygen -t ed25519 -C "your@email.com"
-
The command will prompt you to enter a file name where the key will be saved. The default name is
id_ed25519
, and it will be saved in the~/.ssh
directory. Press Enter to accept the default name or enter a new name if you want. -
You will also be prompted to enter a passphrase. This is optional, so you can press
Enter
if you don’t want to use a passphrase. However, it is recommended to use a passphrase for added security.
Upon confirming the previous step, two files will be generated; id_ed25519
the private key, and id_ed25519.pub
the pub
lic key. The private key should never leave your computer. If anyone gains access to it they will be able to impersonate you when connecting to git.embl.de via SSH.
- After the key pair is generated, run the following command to add the private key to the SSH agent that should be running in your computer:
ssh-add ~/.ssh/id_ed25519
If you see a Identity added:
message, you were successful and can continue at step 6.
If instead you see the error:
Could not open a connection to your authentication agent.
You will need to start an SSH agent first and then repeat the previous command.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
You should then see a Identity added:
message indicating that you were successful.
-
Once the key is added to the agent, you need to copy the public key to your git.embl.de account. The easiest option is to open the file
~/.ssh/id_ed25519.pub
in your favourite text editor and copy its contents to the clipboard. -
Open the SSH keys page in git.embl.de in your favourite browser. Login to GitLab if necessary.
-
Paste the public key you copied earlier into the “Key” box.
-
Give your key a title/name so you can identify which computer generated and/or holds the key.
-
Click on the “Add Key” button to save the key to your git.embl.de account.
-
Now, on your local machine, navigate to the project directory where you want to connect using SSH.
-
Execute the next command to configure git to connect using SSH:
git remote set-url origin git@git.embl.de:your_username/your_repository.git
- Now you can clone the repository using the following command:
git clone git@git.embl.de:your_username/your_repository.git
- Finally, you can push and pull changes using the SSH protocol as well. Try it out with one of the two following commands:
git pull
git push
That’s it! You have successfully created an SSH key and configured a new computer to push/pull to/from GitLab using the SSH protocol.
Cheers,
Renato
Disclaimer
Chat logo by unDraw.