This article teaches git how to configure multiple SSH Keys (Github+Gitlab)

This article teaches git how to configure multiple SSH Keys (Github+Gitlab)

For working partners, we often configure multiple SSH Keys:

  1. The repository pulled by SSH will not repeatedly ask for the github username and password to verify your identity.
  2. It is necessary to distinguish the company's Gitlab and personal Github

Here I take a Mac computer as an example to demonstrate how to configure multiple SSH Keys. The steps for Windows are the same, except that the path to the ssh configuration file has changed.

.ssh file location: C:\Users\Administrator.ssh
hosts location: C:\Windows\System32\drivers\etc\hosts

1 Generate SSH Key

cd ~/.ssh
# 1. 生成一个 Github 用的 SSH-Key,其中 github_id_rsa 为密钥的文件名,~/.ssh/id_rsa_my_github 为密钥目录位置:
$ ssh-keygen -t rsa -C '[邮箱]' -f ~/.ssh/id_rsa_my_github

# 2. 生成一个 Gitlab 用的 SSH-Key,其中 id_rsa_gitlab 为密钥的文件名,~/.ssh/id_rsa_gitlab 为密钥目录位置:
$ ssh-keygen -t rsa -C '[邮箱]' -f ~/.ssh/id_rsa_gitlab

After entering the above command
Insert image description here

Finally, two files will be generated:

  • id_rsa_gitlab private key file
  • id_rsa_gitlab.pub public key file, the contents need to be copied to Github or Gitlab

2 Configure config file

# 新建git config文件
$ touch config
# 用 vim 编辑器打开 config 文件
$ vim config

Configuration file content:

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

# 如果生成多个 SSH-Key , 则按上面的格式继续往下写

Among them, Host and HostName fill in the domain name of the Git server, and IdentityFile specifies the path of the private key (when generating the SSH-Key in the second step, we have already specified the path, just copy it).

  • For example, our company has built its own github with the domain name github.ziyi.com , then just change the Host and HostName in the configuration file to github.ziyi.com .

3 Github configure SSH Key

Copy the contents of the id_rsa_github.pub file generated at the beginning to Github.
The configuration on Gitlab is the same operation step (copy the content of id_rsa_gitlab.pub to Gitlab, which will not be demonstrated later)

  • Create a new SSH Keys on github or gitlab, and then copy the .pub public key to it.
    Go to the ~/.ssh/ directory to view the generated file:
  • The generated file starts with [xxx]_rsa, [xxx]_rsa is the private key, and [xxx]_rsa.pub is the public key.
    Use the cat command to view the contents of the public key file, copy the contents and put it on Github for normal use:cat [xxx]_rsa.pub

3.1 Log in to Github and create SSH Key

①Log in to Github, click on the user avatar, and click on Settings
Insert image description here
②SSH and GPG Keys - New SSH Key
Insert image description here

3.2 View the local .pub public key file and copy the content to Github

Copy the .pub public key file generated by the local computer to the corresponding location on Github

# 将路径改为你自己的公钥文件所在位置
cat  ~/.ssh/id_rsa_my_github.pub

Insert image description here
Insert image description here

3.3 Verify whether the configuration is successful

Test whether the configuration is successful (if there are multiple, test them separately):

#这里以 Github 为例,成功会返回 Hi [用户名]!You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T [email protected]

If an error is found: github ssh -T error kex_exchange_identification: Connection closed by remote host Connection closed by 127.0.0.1 port 7890
① Check whether the private key location in the git config file is configured correctly
Insert image description here

②Check whether VPN is turned on

The configuration of Gitlab and Gitee is the same and will not be repeated here.

Reference article: https://blog.csdn.net/qq_42203909/article/details/120346639

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/135377042