本文默認已經安裝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 2048]----+ |.=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 16843
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:65:24:e9:aa:c1:09:cc:69:0e:f7:b6:07: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: 2023, done. remote: Counting objects: 100% (2023/2023), done. remote: Compressing objects: 100% (1661/1661), done. remote: Total 2023 (delta 322), reused 1967 (delta 278) Receiving objects: 100% (2023/2023), 15.31 MiB | 33.01 MiB/s, done. Resolving deltas: 100% (322/322), done.