Git SSH Key生成
...大约 1 分钟
Git SSH Key生成
SSH Key是一种加密的公钥私钥对,用于身份验证。
使用Git通过SSH拉取远程仓库时,出现以下内容:
$ git clone git@******.com:********.git
Cloning into 'gk_vblog'...
git@******.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
这是因为Git需要SSH Key来验证身份,而你没有配置SSH Key。
- 首先,检查是否已有SSH Key。
ls -al ~/.ssh
// or
cd ~/.ssh
ls
如果存在id_rsa.pub文件,则说明已有SSH Key。
- 如果没有SSH Key,则生成SSH Key。
ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): yes
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:******** ********
The key's randomart image is:
+---[RSA 3072]----+
| .. |
| . ..o.. |
| .. o oo= |
| .o * o.. |
| .. oS.+.. |
| o. ..+E.+oo |
|oo....o.oo=..o |
|..+. . .oo.=+ . |
|.o.. .o o... |
+----[SHA256]-----+
- 然后,将SSH Key添加到ssh-agent。
ssh-add ~/.ssh/id_rsa
如果出现"Could not open a connection to your authentication agent.",则说明没有安装SSH Key。请安装并配置SSH Key。
eval "$(ssh-agent -s)"
// 或者
eval `ssh-agent`
如果出现以下内容,则说明SSH Key添加成功:
ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/Administrator/.ssh/id_rsa (***email***)
再次执行
ssh-add ~/.ssh/id_rsa
- 将SSH Key添加到GitHub上,以便Git通过SSH拉取远程仓库。 步骤:
Settings
->SSH and GPG keys
->New SSH Key
->Title
->Key
->Add SSH Key
查看公钥,以便复制到GitHub上。
cat ~/.ssh/id_rsa.pub
- 最后,测试SSH Key是否配置成功。
ssh -T git@github.com
- 如果出现以下内容,则说明配置成功。
Hi *! You've successfully authenticated, but GitHub does not provide shell access.
Powered by Waline v3.2.2