Everybody is good! 👏 Welcome to check out my personal website fzyt.fun

Writing in the front

This article is written on macOs

When github and Gitee both have accounts and Github has multiple accounts, SSH key configuration problems will occur. This paper aims to solve SSH key configuration problems of multiple code hosting platforms and multiple accounts of one code platform.

Scene 1:

When I was in college, I created my own Github account and searched various materials to configure SSH keys.

The following basic configurations can be used for this scenario

Scene 2:

Gradually, I found that the company’s SSH key conflicts with my own github.

This scenario can be solved by configuring different SSH domain names

Scenario 3:

Creating a second Github account to develop code with someone else on Github created another conflict.

This scenario can be solved by configuring SSH keys for multiple accounts

The basic configuration

SSH is introduced

SSH is short for Secure Shell. It is formulated by the Network Working Group of the Internet Tf. SSH is a security protocol based on the application layer. SSH is a reliable protocol designed to provide security for remote login sessions and other network services.

github ssh keys

Github uses SSH for secure code submission. SSH keys are required.

The configuration process

1. Run the ssh-keygen command to generate SSH public and private keys locally

(base) ➜. SSH ssh-keygen -t rsa -c "[email protected]" Generating public/private RSA key pair#The next step asks you to enter the location and name of your key
#Such as input/Users/fzyt /. SSH/lot/id_rsa_github carriage return (need to create in advance/Users/fzyt /. SSH/lot/folder)
Enter file in which to save the key (/Users/fzyt/.ssh/id_rsa): 
#The following step is to set the key file view password, combined with their own Settings, do not set the press enter
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

#Id_rsa_github is the private key and id_rsa_github. Pub is the public key(base) ➜ github ls id_rsa_github ID_rsa_github. PubCopy the code

2. Configure SSH Config

  • in/Users/fzyt/.sshnewconfigfile
  • configurationgithubThe relevant information
# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github/id_rsa_github

#Configuration file parameters
#Host: alias. Host can be thought of as a pattern that you want to recognize. Configure the corresponding Host name and SSH file for the recognized pattern
#Port: Port (22(default))
#HostName: indicates the HostName to be logged in to
#User: indicates the login name
#PreferredAuthentications: Mandatory Public Key authentication (no(default)/yes)
#IdentitiesOnly: allows only SSH key login
#IdentityFile: specifies the path to the User's IdentityFile.
Copy the code

You can run the following command to test the configuration

(base) ➜  .ssh ssh -T [email protected] 
Hi youer_name! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code

3. Add the generated public key to github Settings

  • usecatCommand to copy the public key
(base) ➜ github cat id_rsa_github. Pub ssh-rsa... . . = [email protected]Copy the code
  • Open thegithubI’m gonna go ahead and select my pictureSettings
  • Click on the menu on the leftSSH and GPS keys
  • Click on theNew SSH keyCopy the public key toKeyGive the Title a name
  • Click on theAdd SSH key

4. The clone code

The clone code is ready

git clone [email protected]/test.git
Copy the code

Different domain names are configured for SSH

  • Basic configuration is complete, if yesgithub ssh key
  • generategitee ssh keyIn which the/Users/fzyt/.ssh/gitee/There are valid public and private keys under
  • in/Users/fzyt/.ssh/configAdd the following configuration
# gitee
Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitee/id_rsa_gitee
Copy the code
  • Using command tests
(base) ➜  .ssh ssh -T [email protected] 
Hi youer_name! You've successfully authenticated, but GITEE.COM does not provide shell access.
Copy the code

Configure SSH keys for multiple accounts

  • Complete basic configuration, such as a work accountgithub ssh key
  • Generate privategithub ssh keyIn which the/Users/fzyt/.ssh/gitee/There are valid public and private keys under
  • in/Users/fzyt/.ssh/configAdd the following configuration
# github work
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github/id_rsa_github_work
# github me
Host me.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github/id_rsa_github_me
Copy the code
  • Using command tests
(base) ➜  .ssh ssh -T [email protected] 
Hi youer_name! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code
(base) ➜  .ssh ssh -T [email protected] 
Hi youer_name! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code
  • clonecode
git clone [email protected]/test.git
Copy the code
git clone [email protected]/test.git
Copy the code

You can change me to something else to configure SSH keys for multiple accounts