preface

Today, I am using the company computer, and I want to configure SSH key for my Github personal warehouse. However, because the company computer has configured SSH key, it is connected to the company’s Gitlab address. How to configure multiple SSH keys at the same time

Add a single ssh-key

The following operations are performed within GitBash

Generate the SSH Key

SSH keys are stored in the ~/. SSH directory by default. If there is no ~/. SSH directory, the ssh-keygen command creates one with the correct permissions.

ssh-keygen -t ed25519 -C "[email protected]"  -f ~/.ssh/gitlab_id_rsa
ssh-keygen -o -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/gitlab_id_rsa
Copy the code

-t Specifies the key type to be created. “Rsa1 “(ssh-1) “rsa”(ssh-2) “dsa”(ssh-2); You can omit the default to specify RSA

-b Specifies the key length. For the RSA key, the minimum value is 768 bits. The default value is 2048 bits. DSA key must be exactly 1024 bits (required by FIPS 186-2 standard)

-c provides a new comment. Here you need to fill in your registered github or Gitlab email number

-f Specifies the key file name. The default is ID_rsa when you leave the file name blank

SSH file where id_rsa.pub is the public key and ID_rsa is the private key

Add the SSH key

  1. Command line copy cat ~ /. SSH/SSH file name | clip

  2. The editing tool opens the.pub file copy directly

  3. Add the Key to the ssh-key of the corresponding platform

  4. Take Github for example

Test the

ssh -T [email protected]
Copy the code

Enter the above command, if the following is displayed, it means that the test is successful. It should be noted that when you configure the gitlab key, you need to enter the IP address or domain name of gitlab after git@ to correctly test.

Multi-key configuration

If multiple keys are configured, you need to configure different file names during key generation. For example

ssh-keygen -t ed25519 -C "[email protected]"  -f ~/.ssh/gitlab_id_rsa_1
ssh-keygen -t ed25519 -C "[email protected]"  -f ~/.ssh/github_id_rsa_2
Copy the code

Then manually configure the key information in the./ SSH/folder config file. If you do not run the config command, the default key will be used when connecting to GitHub. As a result, the connection will be rejected

The config configuration

# gitlab
Host gitlab.com
HostName gitlab.com
User root
IdentityFile ~/.ssh/id_rsa

# github
Host github.com
HostName github.com
User root
IdentityFile ~/.ssh/iD_rsa_github Host indicates the keyword HostName indicates the Host address. User indicates the User name. IdentityFile Indicates the authentication fileCopy the code

Error message

The following errors may occur when testing

Then when the connection to specify their own renamed private key name is good

ssh -T -i git_test [email protected]
Copy the code