git 使用ssh方式登錄

git 服務器一般提供兩種方式登錄:

1 http 或https

2 SSH Keys

http 已經很熟悉了。 https 也類似就是配置一下證書即可。

 

SSH 協議登錄呢?

之前也搞過。 今天總結一下:

 

首先, 在本地的用戶目錄創建 ssh 的非對稱加密的祕鑰對: 即公鑰、私鑰, 比如我本地目錄是 C:\Users\lk\.ssh 

文件就是 id_rsa(私鑰)和 id_rsa.pub (公鑰)。 所謂公鑰就是可以公開的 祕鑰, 稍後呢, 是需要發給 git 服務器端的;  公鑰它自然是 不受保護的.

 

創建的方式是:

ssh-keygen -t rsa -C '[email protected]'

 -C  表示是註釋,是comments,是可以隨意填寫的,當然,一般就使用登錄的郵箱。當然, 也可以不填,不填默認是 用戶名@主機名, 比如 luok@DESKTOP-G5S1KPQ

 

然後把 id_rsa.pub 即公鑰的內容複製到git 服務器的 個人賬戶的ssh中,也就是添加到了 User Settings -> SSH Keys,  比如這個路徑: http://192.169.2.234/profile/keys

 

 

 

 注意, 這裏的title 默認是  id_rsa.pub 即公鑰的最後一截。 其實僅僅是註釋,是comments,是可以隨意修改的。

 

測試過, 我們也可以添加多個SSH Key, git 客戶端push 的時候,只要能使用其中一個SSH Key 登錄, 那麼就ok。 

 

 

 

 否則就還是會讓你輸入密碼, 即使你密碼正確,但是仍然讓你一直輸入:

luok@DESKTOP-G5S1KPQ MINGW64 /d/code/git/github (master)
$ git push origin master
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.

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

 

SSH 協議登錄 原理

就是跟SSH 協議登錄 shell 遠程虛擬機是一樣的。

 

 

 

如何切換登錄協議?

還有就是,如果我們之前使用http 登錄,現在想改成ssh,那麼如何切換登錄協議呢?  其實很簡單,  git remote set-url 加 倉庫的ssh地址即可。

注意,

倉庫的ssh地址 格式是   git@git的ip地址:端口:項目組/項目名.git

倉庫的http地址 格式是   http://git的ip地址:端口/項目組/項目名.git

ssh協議 前綴是git@, 後面分隔符是 冒號: ,http前綴是http://,後面分隔符是 斜槓/  ,

 

 

 

修改之前:

luok@DESKTOP-G5S1KPQ MINGW64 /d/code/git/github (master)
$ git remote -vv
origin http://192.169.2.234/luokai/redis.git (fetch)
origin http://192.169.2.234/luokai/redis.git (push)

 

修改:

$ git remote set-url origin [email protected]/luokai/redis.git

 

修改之後:

luok@DESKTOP-G5S1KPQ MINGW64 /d/code/git/github (master)
$ git remote -vv
origin [email protected]:luokai/redis.git (fetch)
origin [email protected]:luokai/redis.git (push)

 

 

 注意需要保證 倉庫的ssh地址 的正確。 否則:

luok@DESKTOP-G5S1KPQ MINGW64 /d/code/git/github (master)
$ git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'http://192.169.2.234/luokai/redis.git'

 

當然, 我們也可以直接添加倉庫的ssh地址 作爲遠程地址,比如:

git remote add  mine [email protected]:luokai/aaa_deploy.git

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章