ssh公鑰登錄
一般在管理遠(yuǎn)程主機(jī)時(shí),都用ssh登錄,ssh user@host,但是這樣每次會(huì)使用密碼。 使用ssh-keygen生成的密鑰對(duì),然后將公鑰添加的目標(biāo)主機(jī)的~/.ssh/authorized_keys文件中,當(dāng)前主機(jī)就成為可信任的主機(jī),下次使用ssh登錄時(shí),就不用輸入密碼了。
Gitlab,Github都支持這種方式的連接,具體操作步驟如下:
使用ssh-keygen生成密鑰對(duì):
ssh-keygen-t rsa-C"你的郵箱"
這樣就在主目錄下的.ssh目錄中生成了兩個(gè)文件id_rsa和id_rsa.pub。id_rsa中保存的是私鑰,id_rsa.pub中保存的是公鑰。
拷貝公鑰到剪切板:
pbcopy
在 個(gè)人資料->SSH Keys->Add new 頁(yè)面中粘貼公鑰,就添加完成了。
ssh加-T選項(xiàng)測(cè)試目標(biāo)服務(wù)是否可用:
ssh-T git@"你的gitlab服務(wù)器地址"
第一次連接時(shí),會(huì)詢問(wèn)是否信任主機(jī),確認(rèn)后輸入yes。如果看到Welcome to GitLab, Rusher!就算配置成功了,接下來(lái)就可以通過(guò)ssh來(lái)提交代碼了。
Windows
下載Git-Bash
生成密鑰對(duì)ssh-keygen -t rsa -C "你的郵箱"
生成之后用notepad c:/User/Administrator/.ssh/id_rsa.pub打開(kāi)文件,然后將公鑰添加的Gitlab中.
測(cè)試ssh -T git@"你的gitlab服務(wù)器地址"
(只使用客戶端可忽略這節(jié)內(nèi)容)
在客戶端提交時(shí)發(fā)現(xiàn)以下錯(cuò)誤:
/usr/local/lib/ruby/1.9.1/net/http.rb:762:in`initialize': getaddrinfo: Name or service not known (SocketError)
from /usr/local/lib/ruby/1.9.1/net/http.rb:762:in `open'
from /usr/local/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
from/usr/local/lib/ruby/1.9.1/timeout.rb:54:in`timeout'
from /usr/local/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from /usr/local/lib/ruby/1.9.1/net/http.rb:762:in `connect'
from/usr/local/lib/ruby/1.9.1/net/http.rb:755:in`do_start'
from /usr/local/lib/ruby/1.9.1/net/http.rb:744:in `start'
from /home/git/gitlab-shell/lib/gitlab_net.rb:64:in `get'
from/home/git/gitlab-shell/lib/gitlab_net.rb:30:in`check'
from ./check:11:in `'
在Github的issue里找到說(shuō)先運(yùn)行一下/home/git/gitlab-shell/bin/check。先做檢測(cè),發(fā)現(xiàn)和上面一樣的錯(cuò)誤。看錯(cuò)誤是找不到域名,所以在/etc/hosts中需要配置一個(gè)地址的映射。
127.0.0.1YOUR_DOMIN# YOUR_DOMIN是在/home/git/gitlab-shell/config.yml中配置的gitlab_url
在配置Gitlab的時(shí)候一開(kāi)始是用管理員賬戶做測(cè)試的,后來(lái)建了我自己的賬號(hào)做開(kāi)發(fā)。這樣我的本地就有兩個(gè)Gitlab賬號(hào),如果直接用ssh來(lái)提交代碼有問(wèn)題,因?yàn)閟sh默認(rèn)使用一開(kāi)始生成id_rsa那個(gè)密鑰對(duì),但不同的賬號(hào)又不能對(duì)應(yīng)到同一個(gè)公鑰上。如果多個(gè)賬戶一起用,還需要做些配置。
假如有兩個(gè)賬號(hào):root和rusher。
提示在哪里存儲(chǔ)密鑰文件的時(shí)候,對(duì)不同的賬號(hào)填不同的路徑,root放在/Users/you/.ssh/id_rsa_gitlab_root下,rusher的放在/Users/you/.ssh/id_rsa_gitlab_rusher
ssh-keygen-t rsa-C rusher@you.com
Generatingpublic/privatersa key pair.
Enterfileinwhich to save the key(/Users/you/.ssh/id_rsa):/Users/you/.ssh/id_rsa_gitlab_rusher
Enterpassphrase(emptyfornopassphrase):
Entersame passphrase again:
Youridentification has been savedin/Users/you/.ssh/id_rsa_gitlab_rusher.
Yourpublickey has been savedin/Users/you/.ssh/id_rsa_gitlab_rusher.pub.
ssh-keygen-t rsa-C root@you.com
Generatingpublic/privatersa key pair.
Enterfileinwhich to save the key(/Users/you/.ssh/id_rsa):/Users/you/.ssh/id_rsa_gitlab_root
Enterpassphrase(emptyfornopassphrase):
Entersame passphrase again:
Youridentification has been savedin/Users/you/.ssh/id_rsa_gitlab_root.
Yourpublickey has been savedin/Users/you/.ssh/id_rsa_gitlab_root.pub.
還是需要將兩個(gè)賬號(hào)的公鑰分別添加的各自賬號(hào)的SSH Keys中(rusher: id_rsa_gitlab_rusher.pub和root: id_rsa_gitlab_root.pub) 。
ssh-add/Users/you/.ssh/id_rsa_gitlab_rusher
ssh-add/Users/you/.ssh/id_rsa_gitlab_root
在.ssh目錄中添加config文件,此文件的為不同的賬戶添加別名(root: root_gitlab 和 rusher: rusher_gitlab),連接還是同一個(gè)服務(wù)器,但是使用不同的密鑰文件,所以才能將兩個(gè)賬號(hào)分開(kāi)。
# for root
Hostroot_gitlab
HostNamegit.you.com
Usergit
IdentityFile/Users/you/.ssh/id_rsa_gitlab
# for rusher
Hostrusher_gitlab
HostNamegit.you.com
Usergit
IdentityFile/Users/you/.ssh/id_rsa_gitlab_rusher
配置完成后,使用ssh-add命令
接下來(lái)這樣使用別名測(cè)試,可以查看是否對(duì)應(yīng)到了正確的賬號(hào)上:
ssh-T git@root_gitlab
ssh-T git@rusher_gitlab
正常的項(xiàng)目,我們clone下來(lái)之后,origin對(duì)應(yīng)的URL假設(shè)為:git@git.:Rusher/helloworld,現(xiàn)在需要做個(gè)改動(dòng),將git.要換成rusher_gitlab,
git remoteset-url origin git@rusher_gitlab:Rusher/helloworld
如果是root用戶的項(xiàng)目:
git remoteset-url origin git@root_gitlab:root/helloworld
以上配置ssh的方法同樣適用于Github,Bitbucket等網(wǎng)站。
為不同賬號(hào)生成密鑰對(duì)后,需要使用ssh-add將密鑰添加進(jìn)來(lái),否則ssh不能使用正確的密鑰