If ❤️ my article is helpful, welcome to like, follow. This is the biggest encouragement for me to continue my technical creation.

The cause of

Company a GitLab code repository; A personal Github code repository;

However, recently github has been slow, and the loss of bags has been more serious after the river crab. Therefore, in order not to affect the use, a national code cloud Gitee was added;

It happened to be reconfigured with the warehouse key, so I could make a record and then switch machines and reset the system.

To generate the secret key

For the sake of illustration, I assume that I need to reconfigure all the keys. If other keys are already configured on the machine, generate a new key (be careful not to overwrite the generated key)

Run the terminal SSH command to generate an RSA key

Ssh-keygen -t rsa -c “email address 1” -f ~/. SSH /id_rsa_github

-f is the address where the key is stored. The default path is the current one. Press enter, and the process is as follows:

Two files corresponding to the public key ID_rsa_github. Pub and private key id_rsa_github are generated in the ~/. SSH directory.

Tell the git server the public key

Open the public key file and copy the contents.

vim id_rsa_github.pub

Log in to the Git server (github is used as an example). Github account > Setting > SSH and GPG keys, paste the public key string into the key input box, save and exit.

Run the SSH -t [email protected] command to test

If the preceding information is displayed, the secret key is added successfully.

Configure multiple secret keys

We repeat the steps to generate the secret key and tell the git server to generate and configure the new secret key. In the.ssh directory, create a config file with the following contents:

Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
    user coderdao

Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitee
    user coderdao
Copy the code

HostName specifies the name of the key to use

After saving the Settings, test ssh-t [email protected] and ssh-t [email protected]

As shown in the figure above, the configuration of multiple Git keys is successful

Other problems

If the config file is configured, it does not take effect. The configuration of the local Git software is cached. You can run the following command to check whether the key takes effect:

$ cd ~/.ssh

$ eval $(ssh-agent)
Agent pid 3593

$ exec ssh-agent bash

$ ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/Administrator/.ssh/id_rsa_github

$ ssh -T [email protected]
Hi coderdao! You've successfully authenticated, but GitHub does not provide shell access.

Copy the code