How to Set GitLab SSH Key
How to Set GitLab SSH Key - SSH (Secure Shell) is a remote login replacement application like telnet, rsh, and rlogin, which is much more secure. The main function of this application is to access the machine remotely. SSH Keys is a double authentication, like a key that can only be used by your identity.
SSH keys work with a pair of keys, namely the Public Key which will be placed on the remote system, and the Private Key which will be installed on the remote system.
Installing an SSH key serves as a substitute for a password, besides being more secure, we no longer need to type a password when running git commands that require login. For example, when doing a git push to the repository.
Creating SSH Key in Windows
- Open Command Prompt
- Type the command ssh-keygen to create an SSH key
- You are required to specify the location where the file will be saved, or you can leave it saved by default at C:\users\..ssh\id_rsa
- Next, there is a passphrase, you can skip it by pressing the enter key. Do the same for confirmation.
- Then the SSH key will be generated, where the algorithm used by default is RSA 2048.
- After the process is complete, you will find the id_rsa.pub file in the previously specified folder (default is C:\users\.ssh). You can share the key with clients who want to access SSH.
In addition to using the RSA algorithm, there are also other algorithm options such as DSA, ECDSA (with a size of 256, 384, or 521), and ED25519. The following is an example of a command in the Command Prompt if you want to create an SSH key with a custom algorithm.
ssh-keygen -t rsa -b 15360
ssh-keygen -t ecdsa -b 384
Creating SSH Key With PuTTY
First download putty then install
Download Putty
Run PuTTYgen to generate the key. PuTTYgen is a special application from PuTTY for generating public and private keys
Click the Generate button to generate a public key and a private key
For parameters, just select the default settings provided by PuTTY.
Move the mouse over the red box so PuTTY can generate a key. Keep moving until the progress bar is fully charged.
If you are finished, then save the public key and private key in the folder you want
Generating SSH Key on Linux
The first step that must be done is to generate (generate) an SSH key on our Linux PC.
ssh-keygen -C "akuhnet"
- Then run command.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
- Copy the contents of the public key generated by namakey.pub.
cat ~/.ssh/id_rsa.pub
Enter SSH Key into GitLab
Next, enter the SSH key that has been copied to the GitLab account.
Log in to GitLab, go to Settings->SSH Keys menu, paste in Key, then Add key.
Install Git
Then do the test, whether the key was successfully recognized.
- For Windows install git first so you can make git commands
Download Git
- Install git on ubuntu
sudo apt-get install git-core
- Install git on centos
sudo yum install git
After git is installed then enter the following command in the terminal
ssh -T git@gitlab.com
The next test is accessing the repository.
Create a new repository for testing
Then clone the local repository, add files, and push.
git clone git@gitlab.com:akuhnet/tutorialsakuhnet.git
cd tutorialsakuhnet
git config user.name "GitLab Account Name"
git config user.email "email@akugitlab.com"
touch README.md
git add README.md
git commit -m "add README"
git push -uf origin master
Multiple SSH Keys and GitLab Accounts
A developer may have multiple GitLab accounts where each GitLab account uses a different SSH key. When testing ssh -T git@gitlab.com it reads only one GitLan account. For cases like this, we have to create a config file in the .ssh directory.
Windows
C:\Users\akuhnet\.ssh create a config file here
Linux
nano ~/.ssh/config
Then enter the following command in the config file that was created:
Hosts *
IdentityOnly=yes
Hosted GitLab.com-accounts
HostName gitlab.com
IdentityFile /home/userpc/.ssh/key-account one
Git users
Hosted GitLab.com-account
HostName gitlab.com
IdentityFile /home/userpc/.ssh/key-account
Git users
Test
ssh GitLab.com-account
ssh GitLab.com-account
Also, make changes to the local repository config file.
nano .git/config
- Previous config
url = git@gitlab.com:username/repository.git
- Change to
[remote "origin"]
url = git@gitlab.com-account one:username/repository.git
Any question? write in comments
This tutorial is for educational purposes only
if this tutorial doesn't work please comment, akuh.net will update soon