Scenario 1: Only one SSH ID_RSA is required to connect to the GitHub repository

Now you have your own github repository [email protected]:AAA/ aaa.git, which you need to clone locally to modify the code and commit.

git clone [email protected]:AAA/AAA.git
Copy the code

Modify a few more files and commit the code

git add .
git push 
Copy the code

ERROR: Permission to aaa.git

This is because the local repository does not have permission to push to the remote Github repository.

The solution

  1. Check whether the local device exists~/.ssh/id_rsaFile.
# check whether the.ssh directory exists
ls ~ -a  List all files to see if they contain.ssh

# if the. SSH directory exists, check whether the id_rsa and id_rsa.pub files exist
ls ~/.ssh -a
Copy the code
  1. If there is no.sshDirectory or notid_rsa id_rsa.pub
mkdir .ssh # if there is no.ssh directory
cd .ssh
# If you don't have id_rsa, id_rsa.pub, run the following command and press Enter
ssh-keygen -t rsa -C "[email protected]" 
Copy the code
  1. Open one’s owngithub.settings -> SSH and GPG keys -> New SSH key.
  2. Local to the just generated~/.ssh/id_rsa.pubCopy the contents toNew SSH keyKeyThe inside.
# go to.ssh
cd ~/.ssh/
cat id_rsa.pub
Copy the code

Copy the displayed contents into the key of the New SSH key. To Add a title, click Add SSH Key

  1. Enter the beginning againcloneProject directory, executegit pushYou can do it.

The solution described above is to create a key that matches the local and remote Git keys. This key is a string of passwords that can only be used locally to open a github user’s repository.

Scenario 2: If we have two Github users A and B, when we switch to another user B and clone A warehouse under this user locally, then whengit pushYou will encounter the same permission problem:ERROR: Permission to XXX.gitormaster -> master (Permission denied).

This is because A local SSH public key (id_rsa.pub) has been configured, and this public key matches the lock of github user A. When we switch to Github user B, this public key does not have permissions.

The solution

  1. Generate a new SSH KEY (id_rsa id_rsa.pub)
# go to.ssh
cd ~/.ssh/
Create another SSH key
ssh-keygen -t rsa -C "[email protected]"
Copy the code

You don’t want to enter here, you don’t want to enter here, you don’t want to enter here and you’ll get a reminder to fill in a path for a key, a new path for ID_RSA. For example: id_rsa_userBBB, in this picture I used id_rsa_BBB and the rest can be entered.

  1. Look at the new id_rsa_BBB and copy it.

  2. Open github for the BBB account. Settings -> SSH and GPG keys -> New SSH key.

  3. Copy the contents of ~/.ssh/ id_rsa_bbb. pub generated locally to the key of the New SSH key.

SSH does not distinguish between the two keys. By default, it only uses the key of A. You can test this in the root directory of the BBB account repository:

ssh -T [email protected]
Copy the code

Access keys

The following two local keys need to be distinguished for usegit pushLet Git know which key to use

  1. Open the~/.ssh/configIf not, create a new file.
  2. Edit config contents:
# default github setting 
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# another user rsa pub 
Host github-BBB
HostName github.com
User git
# ~/. SSH /id_rsa_BBB file is a newly generated RSA file
IdentityFile ~/.ssh/id_rsa_BBB
Copy the code
  1. Replace the git repository address under BBB account repository.
git remote -v Check the warehouse address
Copy the code

github.com/
Lot - BBB:
github-BBB

 git remote set-url origin github -bbb: indicates the BBB user /XXX repositoryCopy the code

/ /.ssh/id_rsa_BBB / * * * * * * * * * * * * * * * * * * * * * * * *

Multiple public keys matching different repositories can be solved in this way. Run the SSH -t git@Host command to view the relationship between the Host key and the warehouse lock

Warning: Permanently added the RSA host key for IP address ‘xxx.xx.xxx.xxx’ to the list of known hosts.

This is because the default RSA file name is ID_rsa, but we have created another RSA file id_rsa_BBB, which matches the HostName address (set in config file) and the IP address does not exist in the RSA host list. The host for this IP needs to be permanently added to the RSA host list.

  1. Into the. SSH directoryCreate an SSH proxy to store the RSA host list
 eval "$(ssh-agent -s)"
Copy the code

ssh-add id_rsa_BBB
Copy the code

Summary: The local repository pushes to the remote repository using SSH connection, which needs to use ID_RSA to SSH as the key to open the remote repository.

  1. To connect a local key to a remote device, you only need to configure an ID_RSA and add an SSH key in github repository. Set the content of ID_Rsa. pub to this key to connect the device.
  2. If you need to connect to multiple Github repositories, you need to create multiple ID_Rsas locally, use different files to distinguish them, and use the config file to configure the corresponding ID_Rsashost / hostName / IdentityFile, to realize the distinction between keys. The address of the remote repository is reset to host for the different repository names. Finally, the corresponding ID_RSA public key is added to the SSH key on Github