Last update on .

Image credit: craftsmany.com 14-Jun-2017 Use SSH with SSH keys
Image credit: craftsmany.com 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:

    ssh-keygen
  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 (id_rsa.pub).

  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/id_rsa.pub (using the name you assigned). This displays the contents of the file in your terminal.

    ssh-rsa
    AAAAB3NzaC1yc2EAAAADAQABAAACAQCqql6MzstZYh1TmWWv11q5O3pISj2ZFl9Hg
    H1JLknLLx44+tXfJ7mIrKNxOOwxIxvcBF8PXSYvobFYEZjGIVCEAjrUzLiIxbyCoxVyle7Q+b
    qgZ8SeeM8wzytsY+dVGcBxF6N4JS+zVk5eMcV385gG3Y6ON3EG112n6d+SMXY0OEBIcO6x+Pn
    USGHrSgpBgX7Ks1r7xqFa7heJLLt2wWwkARptX7udSq05paBhcpB0pHtA1Rfz3K2B+ZVIpSDf
    ki9UVKzT8JUmwW6NNzSgxUfQHGwnW7kj4jp4AT0VZk3ADw497M2G/12N0PPB5CnhHf7ovgy6n
    L1ikrygTKRFmNZISvAcywB9GVqNAVE+ZHDSCuURNsAInVzgYo9xgJDW8wUw2o8U77+xiFxgI5
    QSZX3Iq7YLMgeksaO4rBJEa54k8m5wEiEE1nUhLuJ0X/vh2xPff6SQ1BL/zkOhvJCACK6Vb15
    mDOeCSq54Cr7kvS46itMosi/uS66+PujOO+xt/2FWYepz6ZlN70bRly57Q06J+ZJoc9FfBCbC
    yYH7U/ASsmY095ywPsBo1XQ9PqhnN1/YOorJ068foQDNVpm146mUpILVxmq41Cj55YKHEazXG
    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 git@gitlab.com:<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

Comments

No comments yet.

Post your comment