Mac configures multiple ssh

In the usual development process, our ssh public key is configured on the company's git, but if we want to pass the ssh clone code from github, what should we do? You can't directly configure the public key configured in the company on git. This is too insecure. There is no good way. The answer must be yes, that is to configure two sets of ssh public keys. No more nonsense, let's start directly.

Step 1: Create ssh-key

If you create an ssh public key locally, you will be prompted to overwrite the existing public key with the same name. This is because I have created a public key with the same name before, but I regenerated it after it expired, so it is directly overwritten. You can see that there are already two pairs of public and private keys in my second picture.

ssh-keygen -t rsa -f ~/.ssh/id_rsa.qlh -C "自己的邮箱"

Step 2: Modify the configuration

Configuration file path: config under /Users/liluyang/.ssh. According to the format configuration below, the git address of the company is hidden in my second screenshot. You can check the format by comparison.

# second user([email protected])
# 建一个github别名,新建的帐号使用这个别名做克隆和更新
  Host github
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa.github

# second user([email protected])
# 建一个gitee别名,新建的帐号使用这个别名做克隆和更新
  Host gitee
  HostName gitee.com
  User git
  IdentityFile ~/.ssh/id_rsa.gitee

Step 3: Reference by alias

Establish a connection through an alias. The format used is as follows. It should be noted here that there may be an error message, reminding that the public key is expired. In fact, the configuration in the git warehouse has expired, and it can be replaced with the new one just generated.

ssh -T 这里是你的git地址

By modifying the ssh key of the git warehouse, the specific path is as follows. I will not map the added process, and wait for the last step after adding.

Step 4: Add key to ssh-agent

After the third step is configured, you may also encounter the following errors. Then just add the public key to ssh-agent. The following command can be executed directly if the error is reported.

Warning: Permanently added 'e.coding.net,175.24.250.178' (RSA) to the list of known hosts.
[email protected]: Permission denied (publickey).
ssh-add -K ~/.ssh/id_rsa.qlh

Execute again, ssh -T [email protected] can see the success message.

I hope it can help everyone, and I also reserve the operation method for myself, in case I forget to check it in the future.

Guess you like

Origin blog.csdn.net/lly576403061/article/details/129628193