Connect to GitLab via SSH

| Created | Modified

Start an SSH Agent

If you haven’t already done so, add the following command to your shell’s RC file (such as .bashrc or .zshrc) to start the ssh-agent:

$ eval $(ssh-agent)

Add Your Generated Key

Use the ssh-add command to add your private SSH key (assuming it is the default id_rsa file) to the agent:

$ ssh-add ~/.ssh/id_rsa

List Keys

You can list the keys currently loaded by the ssh-agent using the following command:

$ ssh-add -l

Testing the GitShell Connection

When connecting to GitLab, the server should respond with a git-shell interface. You should only be able to log in as the git user, not get a regular interactive shell.

Test the connection using the -T flag, which prevents the allocation of a pseudo-terminal:

$ ssh -T git@myserver.lab
Welcome to GitLab, Markus Geiger!

Important: You Should Not Get an Interactive Shell like sh!

Debugging SSH Connections

If you encounter connection issues, you can use verbose output to debug the SSH connection.

For Git versions greater than or equal to 2.2:

$ GIT_SSH_COMMAND="ssh -vvv" git clone git@gitserver/foo.git

For Git versions less than 2.2:

$ GIT_SSH="sshwrapper-script" GIT_TRACE=2 git clone git@gitserver/foo.git

General Debugging

You can also test the raw SSH connection with verbose output and specify your private key:

$ ssh -vv -i privatekey -l git $@

Other Options: Using Credential Caching

To securely cache your credentials globally for a period, you can configure Git to use the credential helper:

$ git config --global credential.helper cache

I've converted the HTML to Markdown, changed the front matter to YAML, ensured all headings start at level 2, and corrected some formatting/spelling for clarity.

Would you like to post the next Hugo article for conversion?