DEV Community

Cover image for How To Set Up GitLab SSH Key on macOS
Muhammad Ilham hidayat
Muhammad Ilham hidayat

Posted on • Updated on

How To Set Up GitLab SSH Key on macOS

Originally posted at blog.milhamh.dev

Prerequisite

  • GitLab account
  • macOS

If you want to clone a repository from GitLab, there are 2 ways to do it.

The first choice is using HTTPS, but you need to insert your GitLab username and password every time you clone a repository.

The second choice is using SSH key. By using SSH key, you can clone a repository from GitLab without inserting username and password. Because, SSH key will handle the authentication.

Today, we will learn how to set GitLab SSH Key on macOS.

Generate SSH Key

  • Open your terminal, go to .ssh directory
cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode
  • Create your SSH Key. We will use RSA SSH key configuration.
ssh-keygen -t rsa -C "your_email_address"
Enter fullscreen mode Exit fullscreen mode
  • You'll see a message to insert file name for SSH key. For example, I insert: id_rsa_gitlab_key
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode
  • Next, you'll be asked to enter passphrase. Just leave it empty
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Enter fullscreen mode Exit fullscreen mode
  • Congratulations, you just create your own SSH Key. To check SSH key exist or not, run:
ls -al
Enter fullscreen mode Exit fullscreen mode
  • You will find two files:
id_rsa_gitlab_key // private key
id_rsa_gitlab_key.pub // public key
Enter fullscreen mode Exit fullscreen mode

Register SSH Key

To make sure your mac able to authenticate automatically with GitLab, you need to register your SSH key to SSH agent in your mac.

  • Start the ssh-agent.
eval $(ssh-agent -s)
Enter fullscreen mode Exit fullscreen mode
  • Add your SSH key to SSH agent.
ssh-add -K ~/.ssh/id_rsa_gitlab_key
Enter fullscreen mode Exit fullscreen mode
  • Open SSH config file.
nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  • To make sure your SSH key added automatically to SSH agent after a reboot (or logout/login), we need to set config file.
Host gitlab.com
   AddKeysToAgent yes
   UseKeychain yes   
   # IdentityFile is your ssh key file location
   IdentityFile ~/.ssh/id_rsa_gitlab_key
Enter fullscreen mode Exit fullscreen mode
  • Save config file by using ctrl + x.

  • Check your SSH key in SSH agent.

ssh-add -l
Enter fullscreen mode Exit fullscreen mode
  • You will see your SSH key has been registered to SSH agent.

Insert SSH Key to GitLab Account

  • Copy your SSH Key. This key will be copied to your GitLab account.
pbcopy < ~/.ssh/id_rsa_gitlab_key.pub
Enter fullscreen mode Exit fullscreen mode
  • Go to gitlab.com, then go Profile >> SSH Keys menu (https://gitlab.com/profile/keys).

Alt Text

  • Paste your SSH Key to Key input, and also add the Title.

  • Press Add Key button.

  • To test whether your SSH key was added correctly, run:

ssh -T git@gitlab.com
Enter fullscreen mode Exit fullscreen mode
  • It should return:
Welcome to GitLab, @your_gitlab_username!
Enter fullscreen mode Exit fullscreen mode

Great, now you can clone and push a repository in GitLab, getting work done and have a time with your family 😁 .

Source

Latest comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.