在為windows 環境下的github賬戶添加SSH key時,需要在Git Bash執行如下命令:
第一步:檢查已有的SSH keys
$ ls -al ~/.ssh
第二步:生成新的SSH key
$ ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter] Enter passphrase (empty for no passphrase): [Type a passphrase] # Enter same passphrase again: [Type passphrase again]
接着會得到如下的提示:
Your identification has been saved in /c/Users/you/.ssh/id_rsa. # Your public key 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
然后將這個新的key添加到ssh-agent中:
$ ssh-agent -s # Agent pid 59566 $ ssh-add ~/.ssh/id_rsa
*注: 如果執行 ssh-add 時顯示錯誤 Could not open a connection to your authentication agent. 那么執行
eval `ssh-agent -s`
后重新執行 ssh-add 那條命令即可。
第三步:將SSH key添加到你的GitHub賬戶
首先將公鑰復制到粘貼板:
$ clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard
或者使用文本編輯工具也能解決。
然后在github的賬戶頁的右上角,點擊配置(settings, 齒輪圖形),在sidebar中點擊“SSH keys”,接着點擊“Add SSH key”,在"title"欄輸入一個自己喜歡的標題,“key”欄中粘貼剛剛復制的公鑰內容,最后點擊“Add key”按鈕。
第四步:檢查SSH key是否成功設置
$ ssh -T git@github.com # Attempts to ssh to GitHub
可能會看到如下的警告:
The authenticity of host 'github.com (207.97.227.239)' can't be established. # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. # Are you sure you want to continue connecting (yes/no)?
輸入“yes”,如果得到下面的結果,說明你的設置是成功的!
Hi username! You've successfully authenticated, but GitHub does not # provide shell access.