preface

Hi, I’m Frank. Today, I had an idea to submit some content on my Github, so I started to configure sSH-key according to the requirements of the official website. Because the company’s SSH-key has been configured on the computer, so it did not succeed, and because it is a Mac book, it is really handy for me, a veteran Windows user. So began the journey of discovery.

The body of the

I don’t know if you have ever had such a feeling, it is a thing that you don’t understand very much, when you solve the problem for the first time, you will have a very confused feeling, looking at the official document is also half-understanding, even when you see so many posts on the Internet, but the problems you have often do not appear, it is very headache. Maybe this is the ability is not enough ~ nonsense don’t say much, we begin the main topic

1. Generate the ssh-key

  • Open the terminal and enter the following command to generate the first onessh-key
SSH -keygen -t rsa -c "Your email address" -f ~/. SSH /id_rsa_oneCopy the code
  • Type again to generate the secondssh-key
SSH -keygen -t rsa -c "Your email address" -f ~/. SSH /id_rsa_twoCopy the code

The purpose of this command is to generate ssh-keys in our.ssh folder

  • After creating our.sshThe following files will be added to the folder

id_rsa_one.pub id_rsa_two.pub id_rsa_one id_rsa_two

Mac users can run the following command to view information

Go to the.ssh directorycd.ssh # check all files in the current directoryCopy the code

2. Add ssh-key to Git

Windows users can directly start file replication. Mac users need to run the following command

SSH /id_rsa_one.pub pbcopy < ~/.ssh/id_rsa_one.pub pbcopy < ~/.ssh/id_rsa_one.pubCopy the code

Copy it and paste it into git

3. Add the private key to the ssh-agent

Ssh-agent Indicates the SSH agent.

  • Enter the following command to add the private key tossh-agentIn the
ssh-add ~/.ssh/id_rsa_one
ssh-add ~/.ssh/id_rsa_two
Copy the code

If the prompt “Could not open a connection to your authentication agent” is displayed, you can run the following command:

ssh-agent bash
Copy the code

If the following information is displayed, it indicates success

Identity added ...
Copy the code
  • View the private key list
ssh-add -l
Copy the code
  • Delete the private key
ssh-add -D
Copy the code
  • In addition, Mac users should be aware that the computer will forget the sSH-add private key every time it shuts down. The solution is as follows

    1. Open automator.app and search in the upper right corner if you don’t know how to open it
    2. A new document
    3. Add a shell script: Enter
    ssh-add ~/.ssh/id_rsa_one ~/.ssh/id_rsa_two
    Copy the code
    1. Save the above as add_ssh_key.app
    2. System Preferences => Users and Groups => Login items => Add add_ssh_key.app
    3. Restart => Succeeded

4. Add the config file to the. SSH folder

Let Git know which private key to look for.

  • Config Indicates the configuration of the file

Host server address or domain name HostName server alias. The same as Host is recommended. User User name PreferredAuthentications publickey this parameter indicates the publickey and does not require changing the IdentityFile private key file path

  • According to the above instructions, our config configuration is as follows
# gitlab
Host gitlab
HostName gitlab.com
User `yourname`
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_one

# github
Host github
HostName github.com
User `yourname`
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_two
Copy the code

Windows users can edit directly, Mac users can use the following command, of course, if you can use the Vim editor

Go to the root directorycd~ # Open the.ssh file and visualize the open.ssh operationCopy the code

5. Test the connection

SSH -t [email protected] SSH -t [email protected]Copy the code

Then you can play happily ~

Afterword.

Hope to help you ~