轉載自:http://www.cnblogs.com/ayseeing/p/4445194.html
如果你已經有了一套名爲 id_rsa
的公祕鑰,將要生成另外一個公鑰,比如 aysee ,你也可以使用任何你喜歡的名字。
步驟如下:
1、生成一個新的自定義名稱的公鑰:
1
|
|
執行命令後,生成命名的公鑰和生成默認公鑰的步驟一樣。
執行完成後,會在 ~/.ssh/目錄下生成一個 aysee 和 aysee.pub 文件。
2、在 SSH 用戶配置文件 ~/.ssh/config 中指定對應服務所使用的公祕鑰名稱,如果沒有 config 文件的話就新建一個,並輸入以下內容:
1
2
|
Host
github.com www.github.com IdentityFile
~/.ssh/aysee |
3、添加 aysee.pub 到你的git服務器網站上。
4、測試配置文件是否正常工作
1
|
ssh
-T [email protected] |
如果,正常的話,會出現如下提示:
1
|
Hi
USERNAME! You've successfully authenticated, but github does not provide shell access. |
如果出現如下提示,則說明有權限問題:
1
|
Permission
denied (publickey) |
如果有權限問題的情況下,你對項目執行push操作的時候,會得到如下提示:
1
2
3
4
5
6
|
Warning:
Permanently added the RSA host key for IP
address '192.30.252.129' to
the list of known hosts. Permission
denied (publickey). fatal:
Could not read from remote
repository. Please
make sure you have the correct access rights and
the repository exists. |
多用戶時出現權限問題的原因:
github使用SSH與客戶端連接。如果是單用戶(first),生成密鑰對後,將公鑰保存至 GitHub ,每次連接時SSH客戶端發送本地私鑰(默認~/.ssh/id_rsa)到服務端驗證。單用戶情況下,連接的服務器上保存的公鑰和發送的私鑰自然是配對的。但是如果是 多用戶 (first,second),我們在連接到second的帳號時,second保存的是自己的公鑰,但是SSH客戶端依然發送默認私鑰,即first的私鑰,那麼這個驗證自然無法通過。
解決ssh權限問題():
通常一臺電腦生成一個ssh不會有這個問題,當一臺電腦生成多個ssh的時候,就可能遇到這個問題,解決步驟如下:
1、查看系統ssh-key代理,執行如下命令
1
|
$
ssh-add -l |
以上命令如果輸出 The agent has no identities. 則表示沒有代理。如果系統有代理,可以執行下面的命令清除代理:
1
|
$
ssh-add -D |
2、然後依次將不同的ssh添加代理,執行命令如下:
1
2
|
$
ssh-add ~/.ssh/id_rsa $
ssh-add ~/.ssh/aysee |
你會分別得到如下提示:
1
|
2048
8e:71:ad:88:78:80:b2:d9:e1:2d:1d:e4:be:6b:db:8e /Users/aysee/.ssh/id_rsa (RSA) |
和
1
2
|
2048
8e:71:ad:88:78:80:b2:d9:e1:2d:1d:e4:be:6b:db:8e /Users/aysee/.ssh/id_rsa (RSA) 2048
a7:f4:0d:f1:b1:76:0b:bf:ed:9f:53:8c:3f:4c:f4:d6 /Users/aysee/.ssh/aysee (RSA) |
如果使用 ssh-add ~/.ssh/id_rsa的時候報如下錯誤,則需要先運行一下 ssh-agent bash 命令後再執行 ssh-add ...等命令
1
|
Could
not open a connection to your authentication agent. |
3、配置 ~/.ssh/config 文件
如果沒有就在~/.ssh目錄創建config文件,該文件用於配置私鑰對應的服務器
1
2
3
4
5
6
7
8
9
10
11
12
|
#
Default github user([email protected]) Host
github.com HostName
github.com User
git IdentityFile
C:/Users/username/.ssh/id_rsa #
aysee ([email protected]) Host
github-aysee HostName
github.com User
git IdentityFile
C:/Users/username/.ssh/aysee |
Host隨意即可,方便自己記憶,後續在添加remote是還需要用到。 配置完成後,在連接非默認帳號的github倉庫時,遠程庫的地址要對應地做一些修改,比如現在添加second帳號下的一個倉庫test,則需要這樣添加:
1
2
|
git
remote add test git@github-aysee:ay-seeing/test.git #並非原來的[email protected]:ay-seeing/test.git |
ay-seeing 是github的用戶名
4、測試 ssh
1
|
ssh
-T [email protected] |
你會得到如下提示,表示這個ssh公鑰已經獲得了權限
1
|
Hi
USERNAME! You've successfully authenticated, but github does not provide shell access. |