【经验之谈_gitlab】通过ssh协议拉取gitlab代码

前言

问题描述: 我们在clone代码的时候可以选择https协议,也可以选择ssh协议来拉取代码。使用“git clone ssh://xxx.git”拉取代码时失败,提示没有权限(没有弹出要输入git账号、密码)。

$ git clone ssh://xxx.git
Cloning into 'web-project'...
git@xxx.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

一、生成 SSH key

1)、检查是否已有SSH key

鼠标右键选择git bash here,先切换到.ssh

cd ~/.ssh
ls

2)、生成 SSH key

使用下面命令生成ssh公钥和私钥对

ssh-keygen -t rsa -b 4096 -C 'xxx@xxx.com'

ssh-keygen -t rsa -C 'xxx@xxx.com'

参数含义:
-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 xxx@xxx.com

之后在“.ssh”目录就会生成 id_rsa 和 id_rsa.pub 两个秘钥文件【或者是xxx和 xxx.pub,xxx为自己输入的文件名 】。
在这里插入图片描述

二、在gitlab上添加SSH key

1)、复制

在“C:/Users/xxx/.ssh”目录下,找到刚生成的 id_rsa.pub 文件,使用编辑器打开复制文件内容,也可以在git bash中输入以下命令复制

clip < ~/.ssh/id_rsa.pub

2)、将SSH key可以配置到gitlab

登录到你的gitlab,点击右上角头像的下拉菜单“Setting”或“Edit profile”,找到User Settings–>SSH Keys—>Add SSH Key

  1. 把上一步中复制的id_rsa.pub内容,粘贴到Key所对应的文本框中
  2. 在Title对应的文本框中,可以给这个ssh key设置备注信息
  3. 点击Add key按钮

在这里插入图片描述

三、拉取代码

到此为止,就完成了gitlab配置ssh key的所有步骤,我们可以使用ssh协议进行代码的拉取及提交等操作了

git clone ssh://xxx.git

四、拉取代码时还是报错

1)、报错:Permission denied (publickey).

在gitlab配置好ssh key之后,还是拉取不了代码

$ git clone ssh://xxx.git
Cloning into 'web-project'...
git@xxx.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

2)、分析问题

发现在~/.ssh文件夹里面有三个文件,其中有个"known_hosts"文件,这个文件包含使用过的公钥信息,将现有的公钥信息清除,有以下两个方法:

  1. 将这个文件改个名字或删除,或打开"known_hosts"文件删除对应IP/域名的公钥信息。
  2. 在git bash里面输入命令ssh-keygen -R IP/域名/域名:端口目的是清除你当前电脑里关于链接的远程服务器的缓存和公钥信息,注意是大写的字母“R”。

3)、解决问题

1、 清空对应IP/域名的公钥信息

使用ssh-keygen -R 命令遇到的情况:

  • 没有"known_hosts"文件时:
$ ssh-keygen -R 10.xxx.xxx.xxx
Cannot stat /c/Users/xxx/.ssh/known_hosts: No such file or directory
  • 在"known_hosts"文件中未找到时:
$ ssh-keygen -R 10.xxx.xxx.xxx                                                 
Host 10.xxx.xxx.xxx not found in /c/Users/xxx/.ssh/known_hosts
  • 在"known_hosts"文件中找到了,就会备份生成“known_hosts.old”文件,原“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.
git@xxx.com: 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】系列文章持续更新中……

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值