问题描述: 我们在clone代码的时候可以选择https协议,也可以选择ssh协议来拉取代码。使用“git clone ssh://xxx.git”拉取代码时失败,提示没有权限(没有弹出要输入git账号、密码)。
$ git clone ssh://xxx.git
Cloning into 'web-project'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
鼠标右键选择git bash here
,先切换到.ssh
cd ~/.ssh
ls
使用下面命令生成ssh公钥和私钥对
ssh-keygen -t rsa -b 4096 -C '[email protected]'
或
ssh-keygen -t rsa -C '[email protected]'
参数含义:
-t 指定密钥类型,默认使用rsa,可以不写
-C 表示comment,参数是你的邮箱地址
-b 指定密钥长度。对于RSA密钥,最小要求768位,默认是2048位。DSA密钥必须恰好是1024位(FIPS 186-2 标准的要求)。
-f 指定密钥文件存储文件名。
以上命令省略了 -f 参数,因此,运行上面命令后会让你输入一个文件名"xxx",用于保存刚才生成的 SSH key 代码【如果不输入文件名,直接按回车,则使用默认名称"id_rsa" 存放ssh key】
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa):xxx
passphrase可以设置密码"xxx"(该密码是你访问gitlab的时候要输入的密码,不是gitlab的密码)【如果不输入密码,直接按回车。那么在访问gitlab的时候就不需要输入密码】
Enter passphrase (empty for no passphrase):xxx
Enter same passphrase again:xxx
接下来,会提示如下信息,说明SSH key生成成功
Your identification has been saved in /c/Users/xxx/.ssh/id_rsa
Your public key has been saved in /c/Users/xxx/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:Z4lZoxfIZW2B4xYL3TrsfGgHsI/yFPFzFqSFWJl8iH0 [email protected]
之后在“.ssh”目录就会生成 id_rsa 和 id_rsa.pub 两个秘钥文件【或者是xxx和 xxx.pub,xxx为自己输入的文件名 】。
在“C:/Users/xxx/.ssh”目录下,找到刚生成的 id_rsa.pub 文件,使用编辑器打开复制文件内容,也可以在git bash中输入以下命令复制
clip < ~/.ssh/id_rsa.pub
登录到你的gitlab,点击右上角头像的下拉菜单“Setting”或“Edit profile”,找到User Settings–>SSH Keys—>Add SSH Key
到此为止,就完成了gitlab配置ssh key的所有步骤,我们可以使用ssh协议进行代码的拉取及提交等操作了
git clone ssh://xxx.git
在gitlab配置好ssh key之后,还是拉取不了代码
$ git clone ssh://xxx.git
Cloning into 'web-project'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
发现在~/.ssh文件夹里面有三个文件,其中有个"known_hosts"文件,这个文件包含使用过的公钥信息,将现有的公钥信息清除,有以下两个方法:
ssh-keygen -R IP/域名/域名:端口
,目的是清除你当前电脑里关于链接的远程服务器的缓存和公钥信息,注意是大写的字母“R”。1、 清空对应IP/域名的公钥信息
使用ssh-keygen -R 命令遇到的情况:
$ ssh-keygen -R 10.xxx.xxx.xxx
Cannot stat /c/Users/xxx/.ssh/known_hosts: No such file or directory
$ ssh-keygen -R 10.xxx.xxx.xxx
Host 10.xxx.xxx.xxx not found in /c/Users/xxx/.ssh/known_hosts
$ ssh-keygen -R 10.xxx.xxx.xxx
# Host 10.xxx.xxx.xxx found: line 1
/c/Users/xxx/.ssh/known_hosts updated.
Original contents retained as /c/Users/xxx/.ssh/known_hosts.old
2、 然后重新输入命令git clone ssh://xxx.git
去拉代码时,会提示是否要连接((yes/no/[fingerprint])?,输入yes),这时候会重新创建一个新的"known_hosts"文件,或更新"known_hosts"文件中的内容
$ git clone ssh://xxx.git
Cloning into 'web-project'...
The authenticity of host '[xxx.com]:22 ([10.xxx.xxx.xxx]:22)' can't be established.
ED25519 key fingerprint is SHA256:sd6p08YWHoWlVlEnnOnzhKI2mGJ0RM3AXeJwOpXa+6c.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[xxx.com]:22' (ED25519) to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
3、 还是报错啊啊啊,没办法了,我重新把第一步(生成 SSH key)、第二步(在gitlab上添加SSH key)重新操作了一次,再输入命令git clone ssh://xxx.git
,正常拉取代码了。
$ git clone ssh://xxx.git
Cloning into 'web-project'...
The authenticity of host '[xxx.com]:22 ([10.xxx.xxx.xxx]:22)' can't be established.
ED25519 key fingerprint is SHA256:sd6p08YWHoWlVlEnnOnzhKI2mGJ0RM3AXeJwOpXa+6c.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[xxx.com]:22' (ED25519) to the list of known hosts.
remote: Enumerating objects: 33489, done.
remote: Counting objects: 100% (33489/33489), done.
remote: Compressing objects: 100% (9262/9262), done.
remote: Total 33489 (delta 23974), reused 33489 (delta 23974), pack-reused 0
Receiving objects: 100% (33489/33489), 9.55 MiB | 2.18 MiB/s, done.
Resolving deltas: 100% (23974/23974), done.
【经验之谈_XXX】系列文章持续更新中……