使用ssh key驗證github的好處就是不用每次提交代碼的時候都要輸入用戶名和密碼,因為着在一定程度上對效率有很大的影響,雖然這么做可以防止代碼提交的次數過多,但這也看個人的習慣吧。
今天嘗試了一下配置ssh key
OS:mac os
打開控制台,建一個目錄,比如~/.ssh(其實叫什么都無所謂,網上搜的基本上都這么配置)
在該目錄下輸入命令:
ssh-keygen -t rsa -C "youname@example.com"
注意:雙引號換成自己的郵箱,如果遇到權限問題,只需在前面加上sudo
Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): ./id_rsa#這里填的是保存文件的路徑 Enter passphrase (empty for no passphrase):#直接enter跳過 Enter same passphrase again: #同上 Your identification has been saved in ./id_rsa. Your public key has been saved in ./id_rsa.pub. The key fingerprint is: 25:68:54:4a:f1:03:ab:78:15:3f:f7:9d:f0:bf:08:ee bsn.huang@gmail.com The key's randomart image is: +--[ RSA 2048]----+ | *o. | | o O | | * * o . | | . + * . + . | | . o S . + | | . . | | . .| | . . . .| | .E . . | +-----------------+
這個時候,在.ssh目錄下有兩個文件
id_rsa id_rsa.pub
其中id_rsa是私鑰 id_rsa.pub是公鑰
然后,執行下面的命令,將生成的key添加
ssh-add id_rsa
然后將id_rsa.pub里面的內容復制下來,在github上的settings里面找到add keys,將其粘貼到key即可,title隨便填
這個時候可以在控制台上測試一下
$ ssh -T git@github.com The authenticity of host 'github.com (192.30.252.128)' 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 Failed to add the host to the list of known hosts (/Users/Bsn/.ssh/known_hosts). Hi bsnwong! You've successfully authenticated, but GitHub does not provide shell access.
其他不用管,只要看到最后一行,說明驗證成功了
后話:剛開始忘記執行ssh-add id_rsa 命令,一直驗證不成功,困在這里好久