如何生成SSH key

如何生成SSH key

SSH key提供了一種與GitHub通信的方式,通過這種方式,能夠在不輸入密碼的情況下,將GitHub作爲自己的remote端服務器,進行版本控制

步驟

  • 檢查SSH keys是否存在
  • 生成新的ssh key
  • 將ssh key添加到GitHub中

     

    gevin-essay-how-to-generate-SSH-key.png

1. 檢查SSH keys是否存在

輸入下面的命令,如果有文件id_rsa.pubid_dsa.pub,則直接進入步驟3將SSH key添加到GitHub中,否則進入第二步生成SSH key

2. 生成新的ssh key

第一步:生成public/private rsa key pair
在命令行中輸入ssh-keygen -t rsa -C "[email protected]"

默認會在相應路徑下(/your_home_path)生成id_rsaid_rsa.pub兩個文件,如下面代碼所示

 

gcl:~ smartlei$ ssh-keygen -t rsa -C "http://igit.58corp.com"
Generating public/private rsa key pair.
//說明會在這個路徑下/Users/smartlei/.ssh/id_rsa 生成id_rsa和id_rsa.pub文件,
//如果你敲入別的地址則爲在別的地址生生成,例如我在/Users/smartlei/.ssh/id_rsa_igit
//這個文件下生成id_rsa_igit 文件文件名也可以修改()
Enter file in which to save the key (/Users/smartlei/.ssh/id_rsa): /Users/smartlei/.ssh/id_rsa_igit
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/smartlei/.ssh/id_rsa_igit.
Your public key has been saved in /Users/smartlei/.ssh/id_rsa_igit.pub.
The key fingerprint is:
SHA256:++Ik2wTYxuEz5UP/G+XEo7ju8pM8SZhol0ifwdZa15U http://igit.58corp.com
The key's randomart image is:
+---[RSA 2048]----+
|                .|
|               E.|
|      ..o.   . . |
|     =.=+.o ...  |
|    ..O=SO..  =  |
|     .++Oo.o = . |
|     ...+o.o+ .  |
|       *o.*. o   |
|      ..o**o.    |
+----[SHA256]-----+

"[email protected]" 郵箱幹嘛用的

每次生成密鑰,看到國內的博客,都要求輸入郵箱。但不知道意義何在。
後來在stackOverFlow上看到這個答案。
The email is only optional field to identify the key.
郵箱僅僅是識別用的key
When you create the ssh you type (for example):
ssh-keygen -t rsa -C “any comment can be here”
當你創建ssh的時候
-t = The type of the key to generate
密鑰的類型
-C = comment to identify the key
用於識別這個密鑰的註釋
So the Comment is for you only and you can put anything inside.
Many sites and software are using this comment as the key name.
所以這個註釋你可以輸入任何內容,很多網站和軟件用這個註釋作爲密鑰的名字

第二步:輸入passphrase(密碼:某些網站clone時需要密碼)(本步驟可以跳過)

設置passphrase後,進行版本控制時,每次與GitHub通信都會要求輸入passphrase,以避免某些“失誤”

sample result:

第三步:將新生成的key添加到ssh-agent中:

 

ssh-add ~/.ssh/id_rsa_igit

Mac 系統內置了一個 Keychain 的服務及其管理程序,可以方便的幫你管理各種祕鑰,其中包括 ssh 祕鑰。
ssh-add 默認將制定的祕鑰添加在當前運行的 ssh-agent 服務中,但是你可以改變這個默認行爲讓它添加到 keychain 服務中,讓 Mac 來幫你記住、管理並保障這些祕鑰的安全性。
你所要做的就是執行下面的命令:

 

$ ssh-add -K [path/to/your/ssh-key]

之後,其他的程序請求 ssh 祕鑰的時候,會通過 Keychain 服務來請求。

3. 將ssh key添加到GitHub中

用自己喜歡的文本編輯器打開id_rsa.pub文件,裏面的信息即爲SSH key,將這些信息複製到GitHub的Add SSH key頁面即可

不同的操作系統,均有一些命令,直接將SSH key從文件拷貝到粘貼板中,如下:

mac

windows

linux



作者:lltree
鏈接:https://www.jianshu.com/p/259773014e03
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章