Skip to content

SSH

Check 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 1. Enter ls -al ~/.ssh to see if existing SSH keys are present:

ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
1. Check the directory listing to see if you already have a public SSH key.

By default, the filename of the public keys is the following: - id_rsa.pub

If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key.

Generating a new SSH key

  1. Open Terminal.
  2. Paste the text below, substituting in your GitHub email address.
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    
    This creates a new ssh key, using the provided email as a label.
    Generating public/private rsa 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 (/Users/you/.ssh/id_rsa): [Press enter]
    
  4. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
    Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
    

Setup a non standard port for our GitLab in ~/.ssh/config

  1. Run edit ~/.ssh/config and add the following
    # Qloud.io GitLab server
    Host gitlab.com
        Port  22
        User  git
    EOL
    

Adding your SSH key to the ssh-agent

Before adding a new SSH key to the ssh-agent, you should have checked for existing SSH keys and generated a new SSH key.

  1. Ensure ssh-agent is enabled:
      # start the ssh-agent in the background
      eval "$(ssh-agent -s)"
      Agent pid 59566
    
  2. Add your SSH key to the ssh-agent. If you used an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file.
    $ ssh-add ~/.ssh/id_rsa
    
  3. Add the SSH public key to your GitLab account.

Working with non-default SSH key pair paths

  1. If you used a non-default file path for your GitLab SSH key pair, you must configure your SSH client to find your GitLab SSH private key for connections to your GitLab server
  2. For OpenSSH clients this is configured in the ~/.ssh/config file. Below are two example host configurations using their own key:
    # Our GitLab server
    Host gitlab.com
        User git
        Port 22
        IdentityFile ~/.ssh/config/private-key-filename
    

Testing your SSH connection

After you've set up your SSH key and added it to your GitLab account, you can test your connection.

Before testing your SSH connection, you should have: - Checked for existing SSH keys - Generated a new SSH key - Added a new SSH key to your GitLab account

When you test your connection, you'll need to authenticate this action using your password, which is the SSH key passphrase you created earlier. 1. Open Terminal. 1. Enter the following:

ssh -T [email protected]
# Attempts to ssh to GitLab

You may see this warning:

The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)?

  1. Verify that the fingerprint in the message you see matches the following SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw, then type: yes
  2. You should then get a response like this:
    Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts.
    Welcome to GitLab, @USERNAME!
    
  3. On the next try you should get a message where you see your name and surname:
    Welcome to GitLab, @USERNAME!
    
  4. If you see this error message:
    Permission denied (publickey).
    
    You have probably missed the step to add the ssh key to GitLab