一般在管理遠(yuǎn)程主機(jī)時,都用ssh登錄,ssh user@host
,但是這樣每次會使用密碼。 使用ssh-keygen
生成的密鑰對,然后將公鑰添加的目標(biāo)主機(jī)的~/.ssh/authorized_keys
文件中,當(dāng)前主機(jī)就成為可信任的主機(jī),下次使用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
選項(xiàng)測試目標(biāo)服務(wù)是否可用:ssh -T git@"你的gitlab服務(wù)器地址"
第一次連接時,會詢問是否信任主機(jī),確認(rèn)后輸入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服務(wù)器地址"
Gitlab服務(wù)端配置
(只使用客戶端可忽略這節(jié)內(nèi)容)
在客戶端提交時發(fā)現(xiàn)以下錯誤:
/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里找到說先運(yùn)行一下
/home/Git/gitlab-shell/bin/check
。先做檢測,發(fā)現(xiàn)和上面一樣的錯誤。看錯誤是找不到域名,所以在/etc/hosts
中需要配置一個地址的映射。127.0.0.1 YOUR_DOMIN # YOUR_DOMIN是在/home/git/gitlab-shell/config.yml中配置的gitlab_url
擴(kuò)展:ssh多用戶切換
在配置Gitlab的時候一開始是用管理員賬戶做測試的,后來建了我自己的賬號做開發(fā)。這樣我的本地就有兩個Gitlab賬號,如果直接用ssh來提交代碼有問題,因?yàn)閟sh默認(rèn)使用一開始生成id_rsa那個密鑰對,但不同的賬號又不能對應(yīng)到同一個公鑰上。如果多個賬戶一起用,還需要做些配置。
假如有兩個賬號:root和rusher。
第一步:為兩個賬戶分別生成密鑰對
提示在哪里存儲密鑰文件的時候,對不同的賬號填不同的路徑,root放在
/Users/you/.ssh/id_rsa_gitlab_root
下,rusher的放在/Users/you/.ssh/id_rsa_gitlab_rusher
ssh-keygen -t rsa -C [email protected]
Generating public/PRivate rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_gitlab_rusher
Enter 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 [email protected]
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_gitlab_root
Enter 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_rusher
ssh-add /Users/you/.ssh/id_rsa_gitlab_root
第二步:添加ssh配置文件
在.ssh目錄中添加
config
文件,此文件的為不同的賬戶添加別名(root: root_gitlab 和 rusher: rusher_gitlab),連接還是同一個服務(wù)器,但是使用不同的密鑰文件,所以才能將兩個賬號分開。# for root
Host root_gitlab
HostName git.you.com
User git
IdentityFile /Users/you/.ssh/id_rsa_gitlab
# for rusher
Host rusher_gitlab
HostName git.you.com
User git
IdentityFile /Users/you/.ssh/id_rsa_gitlab_rusher
配置完成后,使用
ssh-add
命令接下來這樣使用別名測試,可以查看是否對應(yīng)到了正確的賬號上:
ssh -T git@root_gitlab
ssh -T git@rusher_gitlab
第三步:在git項(xiàng)目中使用別名
正常的項(xiàng)目,我們clone下來之后,origin對應(yīng)的URL假設(shè)為:
git@git.:Rusher/helloworld
,現(xiàn)在需要做個改動,將git.
要換成rusher_gitlab
,git remote set-url origin git@rusher_gitlab:Rusher/helloworld
如果是root用戶的項(xiàng)目:
git remote set-url origin git@root_gitlab:root/helloworld
以上配置ssh的方法同樣適用于Github,Bitbucket等網(wǎng)站。
參考文檔
Github HelpUPDATE 2013-08-16: 為不同賬號生成密鑰對后,需要使用ssh-add將密鑰添加進(jìn)來,否則ssh不能使用正確的密鑰
新聞熱點(diǎn)
疑難解答
圖片精選