使用Github,也許你們以爲比較麻煩的就是在每次push的時候,都須要輸入用戶名和密碼。若是使用SSH,就能夠記住用戶名,並建立屬於本身 的密碼來保證安全操做,還有神奇的一招能夠「不用輸入密碼」哦。下面將介紹如何建立SSH Keys並將公鑰加到GitHub帳戶中,使用SSH Keys在本機和GitHub之間創建一個安全的鏈接。git
首先,咱們須要看看是否看看本機是否存在SSH keys,打開Git Bash,並運行:github
1
|
$cd~/.ssh#
|
檢查你本機用戶home目錄下是否存在.ssh目錄shell
若是,不存在此目錄,則進行第二步操做,不然,你本機已經存在ssh公鑰和私鑰,能夠略過第二步,直接進入第三步操做。安全
輸入以下命令:app
1
2
3
4
|
$ssh-keygen-trsa-C"your_email@example.com"
#這將按照你提供的郵箱地址,建立一對密鑰
Generating public/privatersa key pair.
Enter file inwhich tosave the key(/c/Users/you/.ssh/id_rsa):[Press enter]
|
直接回車,則將密鑰按默認文件進行存儲。此時也能夠輸入特定的文件名,好比/c/Users/you/.ssh/github_rsassh
接着,根據提示,你須要輸入密碼和確認密碼(說到這裏,若是你很放心,其實能夠不用密碼,就是到輸密碼的地方,都直接回車,因此每次push就只管回車就好了。所謂的最安全的密碼,就是沒有密碼 – – 哈哈)。相關提示以下:ide
1
2
|
Enter passphrase(empty forno passphrase):[Typeapassphrase]
Enter same passphrase again:[Type passphrase again]
|
輸入完成以後,屏幕會顯示以下信息:測試
1
2
3
4
|
Your identification has been saved in/c/Users/you/.ssh/id_rsa.
Your publickey has been saved in/c/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
|
運行以下命令,將公鑰的內容複製到系統粘貼板(clipboard)中。spa
1
|
clip<~/.ssh/id_rsa.pub
|
接着:3d
1. 登錄GitHub,進入你的Account Settings.
2. 在左邊菜單,點擊」SSH Keys」.
5. 點擊」Add Key」按鈕。
6. 再彈出窗口,輸入你的GitHub密碼,點擊確認按鈕。
7. 到此,大功告成了!
爲了確認咱們能夠經過SSH鏈接GitHub,咱們輸入下面命令。輸入後,會要求咱們提供驗證密碼,輸入以前建立的密碼就ok了。
1
|
$ssh-Tgit@github.com
|
你可能會看到告警信息,以下:
1
2
3
|
The authenticity of host'github.com (207.97.227.239)'can'tbe established.
RSA key fingerprint is16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want tocontinueconnecting(yes/no)?
|
不用擔憂,直接輸入yes。
若是看到下面信息,就說明一切完美!
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.
首先,咱們須要看看是否看看本機是否存在SSH keys,打開終端(Terminal),並運行:
$cd ~/.ssh
# 檢查你本機用戶home目錄下是否存在.ssh目錄
若是,不存在此目錄,則進行第二步操做,不然,你本機已經存在ssh公鑰和私鑰,能夠略過第二步,直接進入第三步操做。
輸入以下命令:
1
2
3
4
|
$ssh-keygen-trsa-C"your_email@example.com"
#這將按照你提供的郵箱地址,建立一對密鑰
Generating public/privatersa key pair.
Enter file inwhich tosave the key(/Users/you/.ssh/id_rsa):[Press enter]
|
直接回車,則將密鑰按默認文件進行存儲。此時也能夠輸入特定的文件名,好比/Users/you/.ssh/github_rsa
接着,根據提示,你須要輸入密碼和確認密碼。相關提示以下:
1
2
|
Enter passphrase(empty forno passphrase):[Typeapassphrase]
Enter same passphrase again:[Type passphrase again]
|
輸入完成以後,屏幕會顯示以下信息:
1
2
3
4
|
Your identification has been saved in/Users/you/.ssh/id_rsa.
Your publickey has been saved in/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
|
運行以下命令,將公鑰的內容複製到系統粘貼板(clipboard)中。
pbcopy < ~/.ssh/id_rsa.pub
接着:
1. 登錄GitHub,進入你的Account Settings.
2. 在左邊菜單,點擊」SSH Keys」.
3. 點擊」Add SSH key」按鈕.
4. 粘貼你的密鑰到key輸入框中.
5. 點擊」Add Key」按鈕。
6. 再彈出窗口,輸入你的GitHub密碼,點擊確認按鈕。
7. 到此,大功告成了!
爲了確認咱們能夠經過SSH鏈接GitHub,咱們輸入下面命令。輸入後,會要求咱們提供驗證密碼,輸入以前建立的密碼就ok了。
$ ssh -T git@github.com
你可能會看到告警信息,以下:
1
2
3
|
The authenticity of host'github.com (207.97.227.239)'can'tbe established.
RSA key fingerprint is16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want tocontinueconnecting(yes/no)?
|
不用擔憂,直接輸入yes。
若是看到下面信息,就說明一切完美!
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.
【注意】
若是前面沒有將生成的密鑰存放在默認的文件id_rsa中(而是my_rsa中),那麼ssh -T git@github.com命令就須要添加參數來執行。以下:
$ ssh -T -i my_rsa git@github.com
其實,一套SSH密鑰是能夠用在不一樣的SSH環境的.
可是若是因爲某種要求,須要用不一樣的SSH密鑰鏈接不一樣的Git環境。假設具體場景是,已經建了密鑰github_rsa,還須要建立work_rsa鏈接工做環境git倉庫,那麼,能夠按下面操做進行:
1. 建立另外一對密鑰work_rsa.
$ssh-keygen -t rsa -C 「work@mail.com」
#保存密鑰爲work_rsa
2. 添加新身份信息
$ssh-add ~/.ssh/work_rsa
3. 配置.ssh/config
咱們須要經過Host別名,將不一樣的帳號區分開來。
1
2
3
4
5
6
7
8
9
|
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile~/.ssh/github_rsa
Host work.comp.com
HostName comp.com
PreferredAuthentications publickey
IdentityFile~/.ssh/work_rsa
|
參考文檔:GitHub官方文檔Generating SSH Keys