Connect Gitlab using SSH
Setup GitLab SSH keys¶
- To setup and configure GitLab
SSH keys
for secureGit fetch
,pull
,push
andclone
operations, follow these steps:
Generate a GitLab SSH key
pair on your personal computer.¶
- Both
Ubuntu
andWindows
machines have built-in SSH support through the inclusion of thessh-keygen
tool. - open
PowerShell
in Windows or a Linuxterminal
window on Ubuntu and issue the following command:
C:\gitlab\ssh\example> ssh-keygen -o -t rsa -C "ssh-EricYangGitlab"
ssh-keygen
: This command is used to generate SSH keys. SSH keys are a pair of cryptographic keys used to authenticate a client to an SSH server. They consist of apublic key
(which is shared with the server) and aprivate key
(which remains on the client).-o
: This option tellsssh-keygen
to use the new OpenSSH format when saving the key. The OpenSSH format was introduced to provide better security and features compared to the older formats.-t rsa
: This option specifies the type of key to generate. In this case, it specifiesRSA
as the key type.RSA
is a widely-used public-key cryptosystem for secure data transmission.
-C "ssh-EricYangGitlab"
: This option allows you to add a comment to the key. Comments are useful for identifying the key, especially when you have multiple keys.
- By default, all GitLab
SSH keys
must go in an.ssh
folder under the user’s home directory. Furthermore, a blank passphrase is acceptable unless your organization’s compliance rules state otherwise.
Add your public ssh key
to GitLab¶
Copy the value of public key
Log into GitLab and click on your account preferences.
Click the SSH Keys link and paste the copied value into the text field.
- Set an expiration date, and then click the blue button to persistently add the GitLab SSH key
Set config
under the .ssh
folder¶
Sometimes it will requires config setting to connect the gitlab
server, if encounter error in next step, we can build a config
file under the C:\User\yourname\.ssh
, and put the following material:
Host gitlab.com
Hostname gitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_rsa
Issue a git clone command with the SSH URL
¶
Once the connection is built, we can run any git
command to manipulate our repo