本文默认已经安装git,并有github或者gitlab账号
git在linux下安装参考:https://www.cnblogs.com/lz0925/p/10791147.html
在Linux中配置ssh密钥,在Git中使用SSH协议访问Github,使用 SSH 协议, 您可以连接并验证远程服务器和服务。在每次访问时连接到 GitHub 而不提供用户名或密码。
第一步:检查是否存在SSH秘钥
ls -al ~/.ssh
如果显示 cannot access /root/.ssh: No such file or directory 表示目录不存在,ssh不存在,如果存在也可以继续执行第二步
第二步:生成ssh-key 密钥对,包含:公钥、私钥
ssh-keygen -t rsa -C "271xxxxxx@qq.com"
第一次回车,然后会让你输入两遍密码,可以输个自己容易记住的 也可以不输入直接一路回车到底,出现下面的情况就成功了
The key's randomart image is:
+---[RSA ]----+
|.=oB+o. |
|oo% ooo . |
| =.* o+B. |
| + =.E+= |
| . + + +S* |
| . . . * O |
| . + * . |
| o o |
| o |
+----[SHA256]-----+
秘钥路径在刚刚输入密码后会告诉你,注意检查命令行,一般路径如下在用户目录下的.ssh目录
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
第三步:添加ssh-key私钥到 ssh-agent
1、首先确保ssh-agent正常工作
[root@ACA832F0 ~]# eval $(ssh-agent -s)
Agent pid
2、直接将私钥id_rsa添加到ssh代理中,跟windows不同的是不需要修改后缀为.ppk
[root@ACA832F0 ~]# ssh-add ~/.ssh/id_rsa
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
第四步:将公钥id_rsa.pub添加到你的github或者gitlab等仓库中,
打开公钥文件复制全文
vim /root/.ssh/id_rsa.pub
使用邮箱登录仓库,用户setting -> SSH key 将公钥粘贴进去,起个容易识别的名字 title
第五步:验证
ssh -T git@172.168.50.41
出现类似下面的情况,OK
The authenticity of host '172.168.50.41 (172.168.50.41)' can't be established.
ECDSA key fingerprint is SHA256:28OQAVeipIe3M4oro+tTp+ExT5+z8+1PbE3ArCiTjYs.
ECDSA key fingerprint is MD5:0c:3d:e9:::e9:aa:c1::cc::0e:f7:b6::b3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.168.50.41' (ECDSA) to the list of known hosts.
Welcome to GitLab, @lizh
第六步:使用,clone代码
[root@ACA832F0 project]# git clone ssh://git@192.168.50.49:22/backs/www.facility.com.git
Cloning into 'facility.klagri.com.cn'...
remote: Enumerating objects: , done.
remote: Counting objects: % (/), done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Receiving objects: % (/), 15.31 MiB | 33.01 MiB/s, done.
Resolving deltas: % (/), done.