编程知识 cdmana.com

How to manage multiple SSH keys on the same computer

Generally speaking , A computer has only one private key by default id_rsa And public key id_rsa.pub

  • windows Under the system .ssh The folder is in C:\Users\Administrator\.ssh Under this catalog
  • macOS Systematic .ssh A folder is a hidden folder , stay finder I can't see it directly , In the root directory ~/.ssh, We need to input instructions in the terminal open ~/.ssh open

When we need to operate different github account number ( or gitlab、oschina、 Code cloud 、coding), Or build multiple hexo Blog , You need multiple correspondences ssh key, Because a github Account can only create a warehouse with the same account name .

Generate two ssh secret key

If you haven't used it ssh Cloned projects , There is no configuration of any git account number , Please refer to 《github Settings add ssh》

  1. Create a new ssh key
  2. According to the above figure, generate the first 2 individual ssh key, Email address can be the same or different ( In order to prevent accidents, unknown problems , It is recommended to use different email addresses for users ) Remember to put id_rsa Rename it to a different name , Naming rules refer to id_rsa_xx And id_rsa_xx.pub The format of
  3. If not in the beginning .ssh Execute under directory ssh-keygen -t rsa -C " Account email ", You may need to manually convert the generated id_rsa_xx And id_rsa_xx.pub All the documents are moved to .ssh Folder , And only this 4 File , It will be automatically generated later known_hosts file

Add the private key

Default SSH Only read id_rsa, So in order for SSH Identify the new private key , It needs to be added to SSH agent

  1. open ssh-agent
ssh-agent bash
ssh-agent -s
 Copy code 
  • windows Here's the picture
  • macOS Here's the picture
  1. Use instruction ssh-add ~/.ssh/id_rsa_xx Add a new private key
    • windows Here's the picture
    • macOS Here's the picture

To configure config file

  1. stay .ssh Manually create... In the folder config file , And fill in the following template
#  To configure 
Host  Custom alias , It will affect git Relevant command ( Such as account number user.name  or   Account email user.email)
HostName  Remote warehouse real domain name (github.com or git.oschina.net)  or  ip Address 
PreferredAuthentications publickey
IdentityFile  Local private key id_rsa The path of 
User  Configure user name , Write git Just go 
 Copy code 
  1. The configuration is as follows

  2. If it's configuration hexo Blog , You can refer to the following configuration

New SSH key

1 . Put the local public key id_rsa_xx.pub Add them to the corresponding platform account respectively , Here we use github For example , Click on your avatar ,Settings -> SSH and GPG keys -> New SSH key 2. Find the public key you want to configure 3. Double-click to open , Copy ssh-rsa The first string of characters 4. Title Name it at will ,Key Paste the string of public keys that you just copied in 5. You may need an access code , Now the private key has been added

Remote testing

  1. windows platform , open Git Bash client ( Run as Administrator ),macOS platform , open terminal terminal
  2. Execute test command ssh -T git@Host Name test github Whether the public key setting is successful , If the key lights up , The configuration is successful , This command will automatically be in .ssh Directory generation known_hosts File and configure the private key in
  3. about hexo Blog , It can also be direct ssh -T Host Or use ssh git@Host

Remote github Clone the warehouse locally

Now there's more than one key on the computer , At this point, the clone command entered is different ,git clone git@Host Custom alias

git clone git@192.168.27.252:6-sports/clients/app6sports.git
 Change it to 
git clone git@yudesports:6-sports/clients/app6sports.git
 Copy code 
git clone git@github.com:spp90s/vueBuy.git
 Change it to 
git clone git@spp90s:spp90s/vueBuy.git
 Copy code 

git@ What follows is what we added manually config The corresponding alias in the configuration file Host

版权声明
本文为[Jesse90s]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152607770m.html

Scroll to Top