官方說明:https://help.github.com/articles/generating-ssh-keys/
1,為Github賬戶設置SSH key
文章地址:http://zuyunfei.com/2013/04/10/setup-github-ssh-key/
什么是SSH key
一直使用SSH連接服務器,但是對它的原理卻不太了解。這次設置Octopress的時候,需要使用SSH 方式連接Github, 正好對SSH的工作方式做了下了解。(好像Github推薦使用HTTPS的方式訪問repo, 以前Github受到過SSH密匙攻擊,之后升級了SSH key的安全措施,https方式視乎更方便安全,不過Octopress的設置文檔中,我並沒有找到怎么使用HTTPS連接Github)
簡單來說,SSH提供了兩種級別的安全驗證:
- 第一種級別是基於密碼的安全驗證,知道賬號和密碼,就可以登陸到遠程主機。Team的開發工作中,就是使用這種方式登陸編譯服務器,或者開發機器。因為是在內網中,這種級別的安全驗證已經足夠了。
- 第二種級別是基於Public-key cryptography (公開密匙加密)機制的安全驗證,原理如下圖所示:
其優點在於無需共享的通用密鑰,解密的私鑰不發往任何用戶。即使公鑰在網上被截獲,如果沒有與其匹配的私鑰,也無法解密,所截獲的公鑰是沒有任何用處的。
產生SSH key
根據Github提供的help文檔,具體過程如下
1 2 |
$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory |
使用ssh-keygen產生新的key
1 2 3 4 |
$ ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key using the provided email Generating public/private rsa key pair. Enter file in which to save the key (/home/you/.ssh/id_rsa): |
使用默認的文件名直接enter, 按提示輸入密碼(如果不提供密碼,SSH將無密碼連接,如果private key泄露可能會有安全問題)
1 2 |
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
|
密匙產生成功
1 2 3 4 |
Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/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 |
上傳public key到Github賬戶
- 登錄github
- 點擊右上方的Accounting settings圖標
- 選擇 SSH key
- 點擊 Add SSH key
在出現的界面中填寫SSH key的名稱,填一個你自己喜歡的名稱即可,然后將上面拷貝的~/.ssh/id_rsa.pub文件內容粘帖到key一欄,在點擊“add key”按鈕就可以了。
添加過程github會提示你輸入一次你的github密碼
設置SSH使用HTTPS的403端口
在局域網中SSH的22端口可能會被防火牆屏蔽,可以設置SSH使用HTTPS的403端口。
測試HTTPS端口是否可用
1 2 3 |
$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not provide shell access. |
編輯SSH配置文件 ~/.ssh/config 如下:
1 2 3 |
Host github.com
Hostname ssh.github.com
Port 443
|
測試是否配置成功
1 2 3 |
$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.
|
多個Github賬號的SSH key切換
如果在一台機器上要登陸多個Github賬戶,需要一些配置,雖然現在並沒有用到,但是先記下來以備不時之需,過程參看這里。
2,【GitHub】解決每次push代碼到github都需要輸入用戶名和密碼的方法
在github上,建立一個項目test,去主頁查看可以看到
如果使用HTTPS:
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/guochy2012/test.git git push -u origin master
Push an existing repository from the command line
git remote add origin https://github.com/guochy2012/test.git git push -u origin master
如果采用SSH:
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:guochy2012/test.git git push -u origin master
Push an existing repository from the command line
git remote add origin git@github.com:guochy2012/test.git git push -u origin master
使用HTTPS需要每次輸入密碼,SSH則不用,但SSH需要配置密鑰 。
關於怎么產生密鑰可以參見《Generating SSH Keys》一文
3,github地址 從https改成ssh
打開命令行工具,運行 git remote set-url origin
例如:
1
2
3
4
|
$ git remote set-url origin git@github.com:user/repo.git
|
然后再次 commit,如果出現類似:
1
2
3
4
|
Permission denied (publickey).
|
字樣,那么說明你的 SSH key 沒有設置或已經失效(譬如升級到 Mountain Lion 系統后),請重新參照上文的官方文檔進行設置即可。
4,執行pull時報錯
wangkongming@AY140527171808170503Z:~/github/collect$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/wangkongming/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/wangkongming/.ssh/id_rsa
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
答案:http://stackoverflow.com/questions/1556119/ssh-private-key-permissions-using-git-gui-or-ssh-keygen-are-too-open
是因為給 id_rsa的權限太高了,改成700就可以了。也有人說600
You changed the permissions on the whole directory, which I agree with Splash is a bad idea. If you can remember what the original permissions for the directory are, I would try to set them back to that and then do the following
cd ~/.ssh
chmod 700 id_rsa
inside the .ssh folder. That will set the id_rsa file to rwx (read, write, execute) for the owner (you) only, and zero access for everyone else.
If you can't remember what the original settings are, add a new user and create a set of SSH keys for that user, thus creating a new .ssh folder which will have default permissions. You can use that new .ssh folder as the reference for permissions to reset your .ssh folder and files to.
If that doesn't work, I would try doing an uninstall of msysgit, deleting ALL .ssh folders on the computer (just for safe measure), then reinstalling msysgit with your desired settings and try starting over completely (though I think you told me you tried this already).
Edited: Also just found this link via Google -- Fixing "WARNING: UNPROTECTED PRIVATE KEY FILE!" on Linux While it's targeted at linux, it might help since we're talking liunx permissions and such.
======================================
以下是自己在使用git時,總結的:
1,查看當前項目遠程分支的路徑
git remote -v