为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

使用git clone命令从GitLab上同步代码库时,如果使用SSH链接(如:[email protected]:example/example.git),但是你的SSH key没有添加到GitLab的账号设置中,系统会报下面的错误:

Permission denied (publickey).

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

或者是

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文件的内容添加到GitLab账号上去。创建SSH key的方法很简单。 

生成SSH Key的过程如下:

1).首先打开linux服务器,输入命令:ls -al ~/.ssh,检查是否显示有id_rsa.pub或者id_dsa.pub存在,如果存在请直接跳至第3步。

2).在bash中输入ssh-****** -t rsa -C ”[email protected]”,注意这个地方的邮箱地址地址替换成你自己的邮箱地址即可,在显示如下的输出后一直按回车即可:
为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

最后显示是这样:

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

在这里可以看到id_rsa和id_rsa.pub已经生成,并且生成的路径为/root/.ssh/。

3).打开id_rsa.pub文件,并且复制全部内容。 

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

4).打开GitLab账户,打开SSH Keys: 

[[email protected] ~]# ssh-******

[[email protected] ~]# cd .ssh

[[email protected] .ssh]# ls

id_rsa id_rsa.pub (公钥)

[[email protected] .ssh]# cat id_rsa.pub

拷贝公钥内容。

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

一个gitlab可以拥有多台主机上的公钥文件。

5)jenkins主机测试使用ssh方式拉取代码。

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

客户端安装git 命令;如果已安装请忽略。

[[email protected] .ssh]# yum install git

使用git clone 命令进行clone 这个项目,这个时候你就不需要输入用户名和密码了。第一次需要输入 / yes

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法

查看到最终拉取下来的项目。

为GitLab帐号添加SSH keys并连接GitLab;报错解决方法