Last update on .

Image credit: 14-Jun-2017 Use SSH with SSH keys
Image credit: 14-Jun-2017 Use SSH with SSH keys

Recently I added two-factor authentication to my GitLab account. Doing this caused some issues with some of my local existing repositories that required me to get familiar with SSH keys. This article covers what I learned.

Secure shell (SSH) protocol is a secure protocol and the most common way of safely administering remote servers. Basically, the server and local machines use a pair of "keys" to verify that it is okay for the local machine to talk to the server. SSH keys work in pairs. Each SSH pair contains a public key and a private key.

The private key is kept on the client or local machine and should always be kept secret and never shared. The public key can be shared freely and is the one that is uploaded to the remote server to be logged into.

The steps below show you how to generate an SSH key pair on your local workstation and then how to connect it to your GitHub or GitLab account.

Generate the SSH Key on the Local Workstation.

  1. From the command line, generate an SSH key pair with the following command:

  2. When prompted for the file name in which to save the key, you can hit Enter to accept the default (id_rsa) or type a different name and then hit Enter. By default, the two keys will be stored in the ~/.ssh directory within your user’s home directory. The private key will use the name you entered (id_rsa) and the public key will use the same name with a .pub extension (

  3. Next, you will be prompted to enter a passphrase for the key. This is optional. If you enter one, you will have to provide it every time you use the key. (In other words, every time you push or pull to your repository.)

  4. Display the contents of your public SSH key with the command cat ~/.ssh/ (using the name you assigned). This displays the contents of the file in your terminal.

    sdBIbXWhcrRf4G2fJLRcGUr9q8/lERo9oxRm5JFX6TCmj6kmiFqv+Ow9gI0x8GvaQ== demo@test
  5. Copy these results to your clipboard and paste them to your service as needed.

Adding SSH Keys to a GitLab Account

Add your public SSH key to your GitLab account by doing the following:

  1. Click your avatar in the upper right corner and select Settings.
  2. Navigate to SSH Keys and paste your public key from the clipboard into the Key field.
    • If you created the key with a comment, this will appear in the Title field.
    • If you created the key without a comment, enter an identifiable title like Work Laptop or Home Workstation in the Title field.
  3. Click the Add key button.

Adding SSH Keys to a GitHub Account

Add your public SSH key to your GitHub account by doing the following:

  1. Click your profile photo in the upper right corner and select Settings.
  2. In the user settings sidebar, click SSH and GPG Keys.
  3. Click New SSH key or Add SSH key.
  4. In the Title field, add a descriptive label for the new key.
  5. Paste the public key into the Key field.
  6. Click the Add SSH key button.
  7. If prompted, confirm your GitHub password.

Updating Repos with SSH Key

Now that you have an SSH key assigned to your local machine, you can now use the link provided under the SSH option on your GitLab or GitHub repository (rather than the HTTPS link).

You can either modify the existing origin remote or you can configure a new one with the new SSH link.

$ git remote add gitlab-ssh<username>/<repo_name>.git

Now you should be able to push and pull your changes from and to your local and remote repositories using your SSH keys.

Happy Coding!

Additional Information about Entry


No comments yet.

Post your comment