git 生成 SSH key

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

 

步驟

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

如何生成SSH KEY

如何生成SSH KEY

1. 檢查SSH keys是否存在

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

ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

2. 生成新的ssh key

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

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

ssh-keygen -t rsa -C "[email protected]"
# Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/your_home_path/.ssh/id_rsa):

第二步:輸入passphrase(本步驟可以跳過)

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

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

sample result:

Your identification has been saved in /your_home_path/.ssh/id_rsa.
Your public key has been saved in /your_home_path/.ssh/id_rsa.pub.
The key fingerprint is:
#01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]

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

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
Agent pid 59566
ssh-add ~/.ssh/id_rsa

3. 將ssh key添加到GitHub中

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

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

mac

pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

windows

clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

linux

sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

本文首載於Gevin's blog



作者:Gevin
鏈接:https://www.jianshu.com/p/31cbbbc5f9fa/

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