Mac電腦用終端生成SSH key 訪問自己的Github
一、首先你要檢測自己電腦是否存在 SSH key
在終端輸出: ls -al ~/.ssh
如果你本地沒有生成的話
終端上面會顯示:No such file or directory
如果已經存在的話,
則會顯示 id_rsa 和 id_rsa.pub
二、如果本地沒有的話,我們要自己手動生成了現在我們來Show一把
在終端輸入:ssh-keygen -t rsa -C "your_email@example.com"
"your_email@example.com" 為事例郵箱,此處應填寫為你在github上面注冊的郵箱。
成功后終端上面會顯示結果如下:
Generating public/private rsa key pair. Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):
提示你保存.ssh/id_rsa的路徑,這里直接Enter就好
Greated directory '/Users/xxx/.ssh'. Enter passphrase(empty for no passphrase):
這時提示輸入passphrase,每次與Github通信都會要求輸入passphrase,以避免某些“失誤”,建議輸入
成功后,終端顯示
Your identitification has been saved in /Users/xxx/.ssh/id_rsa. Your public key has been saved in /user/xxx/id_rsa.pub. the key fingerprint is xxxxxxxxxx.......xxxxxxxx your_email@example.com The key's randomart image is:(這里圖就省略顯示了)
三、添加key到SSH
終端輸入命令:
ssh-add ~/.ssh/id_rsa
此時會要求輸入passphrase(輸入步驟 二 中的 passphrase就好 )
成功后,中選顯示:
identity added:/User/xxx/.ssh/id_rsa(/Users/xxx/.ssh/id_rsa)
最后會在 /Users/xxx/.ssh/ 生成兩個文件,id_rsa和id_rsa.pub,這樣 SSH key 就已經生成成功了
四、添加SSH key 到Github 上
這里有兩種方法:一種找到內容id_rsa.pub、一種用命令行
1、直接 終端輸入:vim ~/.ssh/id_rsa.pub
終端顯示

手動復制從ssh-rsa 開始,以your_email@example.com結束,然后粘貼到你登錄的github賬號上面Settings -->SSH keys -->Add SSH key 保存即可 Title 可以隨便寫,Key粘貼剛復制內容,這樣SSH key 就添加到你的Github上了。
2、或者 在終端輸入命令
pbcopy < ~.ssh/id_rsa.pub
然后粘貼到你登錄的github賬號上面Settings -->SSH keys -->Add SSH key 保存即可 Title 可以隨便寫,Key粘貼剛復制內容,這樣SSH key 就添加到你的Github上了。
五、如何檢測SSH key
輸入命令:ssh git@github.com
此時會驗證SSH key是否有權限訪問 你的 github
成的顯示為
Hi your_name!You've successfully authenticated,but Github does not provide shell access. connection to github.com closed.