Setup ssh with gitlab
Step 1: Generate ssh key pair
ssh-keygen -t rsa -b 2048 -C "nitinsharmacs@gitlab.com"
-t
can be any encryption you want to use. -C
is for comment that goes at the key end.
Step 2: Copy and paste the public key in user’s gitlab settings
tr -d '\n' < ~/.ssh/gitlab-access.pub | pbcopy
This removes the leading new line character from the key.
Paste the public key in the given input element.
Step 3: Config ssh config
We can config ssh for different hosts.
Add following in ~/.ssh/config
Host gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab-access
Step 4: Setup ssh agent
To avoid entering key passphrase again and again, we can set up ssh agent.
eval `ssh-agent -s`
Now, let’s add the key to the agent.
ssh-add ~/.ssh/gitlab-access
Listing all the keys added to the agent.
ssh-add -l
This agent specific to the shell. For the different shell, you have config it again.