Generate and add the first ssh key
The first time you use ssh to generate a key, by default, two files, id_rsa and id_rsa.pub, will be generated in the user ~ (root directory); so it will also be generated when multiple ssh keys need to be added. Corresponding private key and public key
ssh-keygen -t rsa -C "[email protected]"
Execute this command in Git Bash and press Enter, two files, id_rsa and id_rsa.pub, will be generated in the ~/.ssh/ directory. Use a text editor to copy and paste the contents of id_rsa_pub to github (gitlab) to
generate and add a second ssh key
ssh-keygen -t rsa -C "[email protected]"
Be careful not to press Enter all the way, give this file a name, such as id_rsa_github, so a corresponding id_rsa_github.pub file will also be generated.
Find the id_rsa_github and id_rsa_github.pub files in the current directory where ssh-keygen is executed, and add them to the .ssh/ directory
or
add the private key
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/id_rsa_github
If it prompts "Could not open a connection to your authentication agent" when executing ssh-add, you can execute the command now:
ssh-agent bash
Then run the ssh-add command again.
# You can check the private key list by ssh-add -l
ssh-add -l
# You can clear the private key list by ssh-add -D
ssh-add -D
Modify the configuration file
and create a new config file in the ~/.ssh directory
vim config
Added content:
# gitlab Host gitlab.com HostName gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github
test
ssh -T [email protected]
output
Hi user! You've successfully authenticated, but GitHub does not provide shell access. It means successfully connected to github