# SSH公鑰
>[success]使用SSH公鑰可以讓你在你的電腦和碼云通訊的時候使用安全連接(Git的Remote要使用SSH地址)
>
>[danger]通常情況下,一臺電腦上配生成一個SSH公鑰就可以了,因為一般情況下,一臺電腦上會登錄一個git賬號。但是也會遇到,同一臺電腦上需要連接多個git的情況,這時一個SSH公鑰就不夠用了。
>
## 1. 一臺電腦使用一個git賬戶
>[success]這時,需要生成一個SSH公鑰就可以了。
生成并部署SSH key
### 如何生成ssh公鑰
你可以按如下命令來生成 sshkey:
~~~
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"
# Generating public/private rsa key pair...
# 三次回車即可生成 ssh key
~~~
查看你的 public key,并把他添加到碼云(Gitee.com) SSH key添加地址:https://gitee.com/profile/sshkeys)
~~~
cat ~/.ssh/id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....
~~~
添加后,在終端(Terminal)中輸入
~~~
ssh -T git@gitee.com
~~~
若返回
~~~
Welcome to Gitee.com, yourname!
~~~
則證明添加成功。
## 2. 一臺電腦使用多個git賬戶
>[success]這時如果使用同一SSH公鑰添加的話,就會出現如下提示:
>
>只為一個SSH公鑰只能被一個git賬號使用。所以在這里就需要使用多個公鑰。
>
生成一個新的自定義名稱的公鑰:
~~~
//生成方式基本一樣
ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" -f ~/.ssh/my
//執行完成后,會在 ~/.ssh/目錄下生成一個 my 和 my.pub 文件
~~~
在 SSH 用戶配置文件 ~/.ssh/config 中指定對應服務所使用的公秘鑰名稱,如果沒有 config 文件的話就新建一個,并輸入以下內容:
~~~
Host github.com www.github.com
IdentityFile ~/.ssh/my
//添加my.pub 到你的git服務器網站上。
~~~
測試配置文件是否正常工作
~~~
ssh -T git@gitcafe.com
~~~
如果,正常的話,會出現如下提示:
~~~
Welcome to Gitee.com, yourname!
~~~
如果出現如下提示,則說明有權限問題:
~~~
Permission denied (publickey)
~~~
如果有權限問題的情況下,你對項目執行push操作的時候,會得到如下提示:
~~~
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.
~~~
### 多用戶時出現權限問題的原因:
>[success]github使用SSH與客戶端連接。如果是單用戶(first),生成密鑰對后,將公鑰保存至 GitHub ,每次連接時SSH客戶端發送本地私鑰(默認~/.ssh/id_rsa)到服務端驗證。單用戶情況下,連接的服務器上保存的公鑰和發送的私鑰自然是配對的。但是如果是 多用戶 (first,second),我們在連接到second的帳號時,second保存的是自己的公鑰,但是SSH客戶端依然發送默認私鑰,即first的私鑰,那么這個驗證自然無法通過。
>
### 解決ssh權限問題():
>[success]通常一臺電腦生成一個ssh不會有這個問題,當一臺電腦生成多個ssh的時候,就可能遇到這個問題,解決步驟如下:
>
查看系統ssh-key代理,執行如下命令
~~~
$ ssh-add -l
~~~
以上命令如果輸出 The agent has no identities. 則表示沒有代理。如果系統有代理,可以執行下面的命令清除代理:
~~~
$ ssh-add -D
~~~
然后依次將不同的ssh添加代理,執行命令如下:
~~~
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/my
~~~
你會分別得到如下提示:
~~~
2048 8e:71:ad:88:78:80:b2:d9:e1:2d:1d:e4:be:6b:db:8e /Users/aysee/.ssh/id_rsa (RSA)
//和
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 ...等命令
>[success]Could not open a connection to your authentication agent.
>
配置 ~/.ssh/config 文件
如果沒有就在~/.ssh目錄創建config文件,該文件用于配置私鑰對應的服務器
~~~
# Default github user(first@mail.com)
Host github.com
HostName github.com
User git
IdentityFile C:/Users/username/.ssh/id_rsa
# my (company_email@mail.com)
Host github-my
HostName github.com
User git
IdentityFile C:/Users/username/.ssh/my
~~~
Host隨意即可,方便自己記憶,后續在添加remote是還需要用到。 配置完成后,在連接非默認帳號的github倉庫時,遠程庫的地址要對應地做一些修改,比如現在添加second帳號下的一個倉庫test,則需要這樣添加:
~~~
git remote add test git@github-aysee:ay-seeing/test.git
#并非原來的git@github.com:ay-seeing/test.git
~~~
ay-seeing 是github的用戶名
測試 ssh
~~~
ssh -T git@github.com
~~~
你會得到如下提示,表示這個ssh公鑰已經獲得了權限
~~~
Hi USERNAME! You've successfully authenticated, but github does not provide shell access.
~~~