Skip to content

Instantly share code, notes, and snippets.

@tappoz
Last active March 29, 2022 17:15
Show Gist options
  • Save tappoz/a24744fd907d5b7983fc9a469d4075ee to your computer and use it in GitHub Desktop.
Save tappoz/a24744fd907d5b7983fc9a469d4075ee to your computer and use it in GitHub Desktop.
Setup an SSH pub/priv key pair for a website e.g. Github/Gitlab, deal with Go modules and private repositories

Generate an public/private key pair

Check the docs tutorial in here: https://docs.gitlab.com/ee/user/ssh.html

These are the main steps:

# the "-C" parameter is a comment to remind you about this key
ssh-keygen -t ed25519 -C "<YOUR_NAME> at gitlab" -f $HOME/.ssh/my_gitlab_eddsa

As an alternative for e.g. RSA (both -P and -N are for password and new password):

ssh-keygen \
  -q \
  -t rsa \
  -b 3072 \
  -f ~/.ssh/id_gitlab_rsa_3072 \
  -P "" -N "" -C ""

You should now have the key pair as:

  • $HOME/.ssh/my_gitlab_eddsa
  • $HOME/.ssh/my_gitlab_eddsa.pub

Reference: https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair

Make sure the SSH agent is permanently aware of this SSH key

eval $(ssh-agent -s)
ssh-add ~/.ssh/my_gitlab_eddsa
touch ~/.ssh/config
echo "Host gitlab.com" >> ~/.ssh/config
echo "  Preferredauthentications publickey" >> ~/.ssh/config
echo "  IdentityFile ~/.ssh/my_gitlab_eddsa" >> ~/.ssh/config

Reference: https://gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths

Check everything works

Add the public key to the website (Github, Gitlab, etc.).

# copy to the clipboard the public key, 
# then go to the website with the browser to manually add what you just copied.
xclip -sel clip < ~/.ssh/my_gitlab_eddsa.pub

Then you can check the SSH key pair works as expected:

ReferenceL https://gitlab.com/help/ssh/README#adding-an-ssh-key-to-your-gitlab-account

git global config

When using private repositories (e.g. on GitLab) this extra global configuration for git needs to be added. This is going to be used by Go (Golang) when running go get and go mod for Go Modules.

git config \
      --global \
      url."https://${GITLAB_TOKEN_KEY}:${GITLAB_TOKEN_VALUE}@gitlab.com".insteadOf \
      "https://gitlab.com"

If a global config entry needs to be removed, then use:

git config --global --unset url.https://${GITLAB_TOKEN_KEY}:${GITLAB_TOKEN_VALUE}@gitlab.com.insteadof

Go modules

When in need to update a Go module dependency for a private git repository targeting a specific git branch (not master) you can run this:

go get gitlab.com/${MY_PRIVATE_REPOSITORY}@${MY_BRANCH}

This will update go.mod and go.sum to the HEAD commit ID for that particular git branch (${MY_BRANCH}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment