輸入下面的命令,如果有文件id_rsa.pub 或 id_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 "your_email@example.com"
默認會在相應路徑下(/your_home_path)生成id_rsa和id_rsa.pub兩個文件,如下面代碼所示
ssh-keygen -t rsa -C "your_email@example.com"
# 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 your_email@example.com
第三步:將新生成的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