--- --- restore content begins
Original Address: https://juejin.im/post/5ac0cf356fb9a028df22c246
1. gitlab and generate the ssh key github
ssh-keygen -t rsa -C "[email protected]" -b 4096
The first generation of gitlab ssh key way to enter to generate a ssh key second github should pay attention to what the name of ssh key needs to be modified a bit, otherwise it will cover gitlab front of key generation, and here I modified id_rsa_github
This time you can see the ~ / .ssh / directory created under the following files
- id_rsa - id_rsa.pub - id_rsa_github - id_rsa_github.pub
2. Copy the contents of each public key, add the ssh key in gitlab and in github
cat ~/.ssh/id_rsa.pub
3. Add the config file
Create a config file in the .ssh / directory
vim config
Add the configuration, content
# gitlab Host gitlab User git HostName gitlab.cheanjiait.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # github Host github User git HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github
4. Test connector
$ ssh -T gitlab Welcome to GitLab, Ethan Chen!
$ ssh -T github Hi azumia! You've successfully authenticated, but GitHub does not provide shell access.
If the above occurs, it means the connection is successful, then you can git out the happy
Precautions
When using github, git in the project initialization time to remember the definition of good user.name and user.email
git config --local user.name 'aaa' git config --local user.email '[email protected]'
If the test connection failed, Permission denied (publickey). The reason is that they have customized id_rsa_github key name, by default, the connection will search id_rsa key name, so here fail
Specific reasons for the failure may be connected by understanding the following
ssh -T -v [email protected]
Solutions to this problem are as follows
Open ssh-agent
# Enable Agent eval $ ( SSH-Agent - S) ← ┘ Agent pid 8428
# Add key name SSH-the Add ~ /. SSH / id_rsa_github ← ┘ the Identity added: / c / the Users / the User /. SSH / id_rsa_github (/ c / the Users / the User /. SSH / id_rsa_github)
# Agent can be closed when not in use eval $ ( SSH-agent - k) ← ┘ Agent pid 8428 killed
If the initialization time to report the following error warehouse
ssh: Could not resolve hostname https: nodename nor servname provided, or not known fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Then look at your git configuration
$ git remote -v
The address by the git ssh mode to https way to