Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited on

     

How To Set Up GitLab SSH Key on macOS

Originally posted atblog.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 usingHTTPS, but you need to insert your GitLab username and password every time you clone a repository.

The second choice is usingSSH 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 modeExit fullscreen mode
  • Create your SSH Key. We will use RSA SSH key configuration.
ssh-keygen-t rsa-C"your_email_address"
Enter fullscreen modeExit 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 fileinwhich to save the key(/home/user/.ssh/id_rsa):
Enter fullscreen modeExit fullscreen mode
  • Next, you'll be asked to enter passphrase. Just leave it empty
Enter passphrase(emptyforno passphrase):Enter same passphrase again:
Enter fullscreen modeExit fullscreen mode
  • Congratulations, you just create your own SSH Key. To check SSH key exist or not, run:
ls-al
Enter fullscreen modeExit fullscreen mode
  • You will find two files:
id_rsa_gitlab_key // private keyid_rsa_gitlab_key.pub // public key
Enter fullscreen modeExit 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 modeExit fullscreen mode
  • Add your SSH key to SSH agent.
ssh-add-K ~/.ssh/id_rsa_gitlab_key
Enter fullscreen modeExit fullscreen mode
  • Open SSH config file.
nano ~/.ssh/config
Enter fullscreen modeExit 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 modeExit fullscreen mode
  • Save config file by usingctrl + x.

  • Check your SSH key in SSH agent.

ssh-add -l
Enter fullscreen modeExit 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 modeExit fullscreen mode
  • Go to gitlab.com, then goProfile >> SSH Keys menu (https://gitlab.com/profile/keys).

Alt Text

  • Paste your SSH Key toKey input, and also add theTitle.

  • PressAdd Key button.

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

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

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

Source

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer 💻 | Go
  • Location
    Yogyakarta, Indonesia
  • Joined

More fromMuhammad Ilham hidayat

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp