一、SSH key简介
SSH key即SSH公钥,因为许多Git服务器都是支持使用SSH协议来访问 ,因此要通过SSH 协议来访问Git仓库需要向 Git 服务器提供 SSH 公钥
如果Git仓库未添加本地生成的SSH Key就会导致clone失败
$ git clone <git远程仓库地址>
Cloning into '项目名称'...
GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
二、生成SSH key的步骤
- 查看是否已经生成SSH Key
- 生成新的SSH Key
- 复制SSH Key添加到Git服务器
- 查看是否已经生成SSH Key
$ ls -al ~/.ssh
total 24
drwx------ 5 <用户名> staff 160 11 1 2018 .
drwxr-xr-x 54 <用户名> staff 1728 12 6 13:36 ..
-rw------- 1 <用户名> staff 1679 10 29 2018 id_rsa
-rw-r--r--@ 1 <用户名> staff 397 10 29 2018 id_rsa.pub
-rw-r--r-- 1 <用户名> staff 357 5 27 2019 known_hosts
查看SSH Key
$ cat /Users/<用户名>/.ssh/id_rsa.pub
ssh-rsa XXXXXXXXXXXX <邮箱地址>
- 生成新的SSH Key
$ ssh-keygen -t rsa -C "<邮箱地址>"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/<用户名>/.ssh/id_rsa):
/Users/<用户名>/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/<用户名>/.ssh/id_rsa.
Your public key has been saved in /Users/<用户名>/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXX <邮箱地址>
The key's randomart image is:
+---[XXX XXX]----+
| ..+*XxX .. |
| .XX^ +..o|
| ..% * oo+|
| . + + +..=|
| S * E. oo|
| + + .|
| . . . |
| |
| |
+----[XXXXXX]-----+
- 复制SSH Key添加到Git服务器
$ pbcopy < ~/.ssh/id_rsa.pub
到Git服务器添加SSH Key,重新clone项目成功
$ git clone <git远程仓库地址>
Cloning into 'XXXXX'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
注意:
如果提示: Are you sure you want to continue connecting (yes/no)?
直接输入yes回车
$ ssh -T git@e.coding.net
The authenticity of host 'e.coding.net (111.123.70.251)' can't be established.
RSA key fingerprint is SHA156:jok3FH7q5LJ6qxX7iPNehBgXXXXw001ErE77S0Dn+Vg/Ik.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'e.coding.net,111.123.70.251' (RSA) to the list of known hosts.
Coding 提示: Hello 黄小明, You've connected to Coding.net via SSH. This is a personal key.
黄小明,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥
参考文章:
git 如何解决The authenticity of host 'git.coding.net (123.59.86.7)' can't be established问题
网友评论