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
- Check whether the local device exists
~/.ssh/id_rsa
File.
# 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
- If there is no
.ssh
Directory 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
- Open one’s own
github
.settings -> SSH and GPG keys -> New SSH key
. - Local to the just generated
~/.ssh/id_rsa.pub
Copy the contents toNew SSH key
的Key
The 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
- Enter the beginning again
clone
Project directory, executegit push
You 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 push
You will encounter the same permission problem:ERROR: Permission to XXX.git
ormaster -> 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
- 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.
-
Look at the new id_rsa_BBB and copy it.
-
Open github for the BBB account. Settings -> SSH and GPG keys -> New SSH key.
-
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 push
Let Git know which key to use
- Open the
~/.ssh/config
If not, create a new file. - 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
- 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.
- Into the
. SSH directory
Create 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.
- 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.
- 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_Rsas
host / 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