一般在管理遠程主機時,都用ssh登錄,ssh user@host,但是這樣每次會使用密碼。 使用ssh-keygen生成的密鑰對,然后將公鑰添加的目標主機的~/.ssh/authorized_keys文件中,當前主機就成為可信任的主機,下次使用ssh登錄時,就不用輸入密碼了。
Gitlab,Github都支持這種方式的連接,具體操作步驟如下:
使用ssh-keygen生成密鑰對:
ssh-keygen -t rsa -C "你的郵箱"這樣就在主目錄下的
.ssh目錄中生成了兩個文件id_rsa和id_rsa.pub。id_rsa中保存的是私鑰,id_rsa.pub中保存的是公鑰。第二步:添加公鑰
拷貝公鑰到剪切板:
pbcopy < id_rsa.pub在 個人資料->SSH Keys->Add new 頁面中粘貼公鑰,就添加完成了。
第三步:測試
ssh加
-T選項測試目標服務是否可用:ssh -T git@"你的gitlab服務器地址"第一次連接時,會詢問是否信任主機,確認后輸入yes。如果看到
Welcome to GitLab, Rusher!就算配置成功了,接下來就可以通過ssh來提交代碼了。Windows
下載 Git-Bash生成密鑰對ssh-keygen -t rsa -C "你的郵箱"生成之后用notepad c:/User/Administrator/.ssh/id_rsa.pub打開文件,然后將公鑰添加的Gitlab中.測試ssh -T git@"你的gitlab服務器地址"Gitlab服務端配置
(只使用客戶端可忽略這節內容)
在客戶端提交時發現以下錯誤:
/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 `<main>'在Github的issue里找到說先運行一下
/home/Git/gitlab-shell/bin/check。先做檢測,發現和上面一樣的錯誤。看錯誤是找不到域名,所以在/etc/hosts中需要配置一個地址的映射。127.0.0.1 YOUR_DOMIN # YOUR_DOMIN是在/home/git/gitlab-shell/config.yml中配置的gitlab_url擴展:ssh多用戶切換
在配置Gitlab的時候一開始是用管理員賬戶做測試的,后來建了我自己的賬號做開發。這樣我的本地就有兩個Gitlab賬號,如果直接用ssh來提交代碼有問題,因為ssh默認使用一開始生成id_rsa那個密鑰對,但不同的賬號又不能對應到同一個公鑰上。如果多個賬戶一起用,還需要做些配置。
假如有兩個賬號:root和rusher。
第一步:為兩個賬戶分別生成密鑰對
提示在哪里存儲密鑰文件的時候,對不同的賬號填不同的路徑,root放在
/Users/you/.ssh/id_rsa_gitlab_root下,rusher的放在/Users/you/.ssh/id_rsa_gitlab_rusherssh-keygen -t rsa -C rusher@you.comGenerating public/PRivate rsa key pair.Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_gitlab_rusherEnter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /Users/you/.ssh/id_rsa_gitlab_rusher.Your public key has been saved in /Users/you/.ssh/id_rsa_gitlab_rusher.pub.ssh-keygen -t rsa -C root@you.comGenerating public/private rsa key pair.Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_gitlab_rootEnter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /Users/you/.ssh/id_rsa_gitlab_root.Your public key has been saved in /Users/you/.ssh/id_rsa_gitlab_root.pub.還是需要將兩個賬號的公鑰分別添加的各自賬號的SSH Keys中(rusher: id_rsa_gitlab_rusher.pub和root: id_rsa_gitlab_root.pub) 。
ssh-add /Users/you/.ssh/id_rsa_gitlab_rusherssh-add /Users/you/.ssh/id_rsa_gitlab_root第二步:添加ssh配置文件
在.ssh目錄中添加
config文件,此文件的為不同的賬戶添加別名(root: root_gitlab 和 rusher: rusher_gitlab),連接還是同一個服務器,但是使用不同的密鑰文件,所以才能將兩個賬號分開。# for rootHost root_gitlabHostName git.you.comUser gitIdentityFile /Users/you/.ssh/id_rsa_gitlab# for rusherHost rusher_gitlabHostName git.you.comUser gitIdentityFile /Users/you/.ssh/id_rsa_gitlab_rusher配置完成后,使用
ssh-add命令接下來這樣使用別名測試,可以查看是否對應到了正確的賬號上:
ssh -T git@root_gitlabssh -T git@rusher_gitlab第三步:在git項目中使用別名
正常的項目,我們clone下來之后,origin對應的URL假設為:
git@git.:Rusher/helloworld,現在需要做個改動,將git.要換成rusher_gitlab,git remote set-url origin git@rusher_gitlab:Rusher/helloworld如果是root用戶的項目:
git remote set-url origin git@root_gitlab:root/helloworld以上配置ssh的方法同樣適用于Github,Bitbucket等網站。
參考文檔
Github HelpUPDATE 2013-08-16: 為不同賬號生成密鑰對后,需要使用ssh-add將密鑰添加進來,否則ssh不能使用正確的密鑰
新聞熱點
疑難解答