git实现ssh免密上传远程gitlab仓库

前言

环境:centos7.9
我们知道,克隆或上传代码有2中方式,一种是通过https,一种是ssh协议,而克隆代码都是不需要密码,但是上传呢是需要输入用户密码,为了方便了,我们可以做ssh免密上传,本篇我们就来讲解使用ssh协议实现免密上传。

ssh免密上传

1、开发者电脑产生秘钥对

[root@git Haroy_Project]# ssh-keygen -t rsa								#指定生成的秘钥为rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 				#按回车保持默认
Enter passphrase (empty for no passphrase): 							#按回车保持默认
Enter same passphrase again: 											#按回车保持默认
Your identification has been saved in /root/.ssh/id_rsa.				#生成的私钥
Your public key has been saved in /root/.ssh/id_rsa.pub.				#生成的私钥
The key fingerprint is:
SHA256:EW6eIMTowT+ELV3OaJv5tMafjYICohRmoW7aQnYCQQc root@git
The key's randomart image is:
+---[RSA 2048]----+
|.E.B... .        |
|..B.=+ . .       |
|.o.=+ + +        |
|o+..o= + o       |
|= . +.. S        |
|oB . + .         |
|Ooo  .=          |
|+ o .... +       |
| . .   .+ .      |
+----[SHA256]-----+
[root@git Haroy_Project]#

2、gitlab配置免密上传
复制公钥,即复制/root/.ssh/id_rsa.pub文件的内容,粘贴到gitlab的界面上保存,如下所示:
git实现ssh免密上传远程gitlab仓库_第1张图片
3、免密测试上传
这次我们上传不在使用http的方式,而是采用ssh协议上传。

[root@git ~]# git clone [email protected]:root/haroy_project.git	#先使用ssh方式克隆一份项目代码下来
Cloning into 'haroy_project'...
remote: Enumerating objects: 25, done.
remote: Total 25 (delta 0), reused 0 (delta 0), pack-reused 25
Receiving objects: 100% (25/25), done.
Resolving deltas: 100% (4/4), done.
[root@git ~]# cd haroy_project/
[root@git haroy_project]# git remote -v						#克隆时候默认会将本地仓库与远端仓库建立链接,名称叫origin
origin  [email protected]:root/haroy_project.git (fetch)
origin  [email protected]:root/haroy_project.git (push)
[root@git haroy_project]# echo "I am good">>haroy_7.sh		#创建一个文件
[root@git haroy_project]# git add haroy_7.sh				#提交暂存区
[root@git haroy_project]# git commit -m "Add haroy_7.sh"	#提交本地仓库
[root@git haroy_project]# git push origin dev:dev			#提交到远程gitlab仓库dev分支,不用输入密码,实现了ssh免密上传
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To [email protected]:root/haroy_project.git
   cdc32e1..286da83  dev -> dev

你可能感兴趣的:(DevOps,CICD,Git,GitHub综合篇,git,gitlab,Linux运维,devops)