轉(zhuǎn)自別處:
SSH key提供了一種與GitHub通信的方式,通過這種方式声登,能夠在不輸入密碼的情況下,將GitHub作為自己的remote端服務(wù)器,進行版本控制
步驟
檢查SSH keys是否存在
生成新的ssh key
將ssh key添加到GitHub中
1. 檢查SSH keys是否存在
輸入下面的命令突勇,如果有文件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"
默認會在相應(yīng)路徑下(/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(本步驟可以跳過)
設(shè)置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中:
(ssh-agent -s)
Agent pid 59566
ssh-add ~/.ssh/id_rsa
3. 將ssh key添加到GitHub中
用自己喜歡的文本編輯器打開id_rsa.pub文件账磺,里面的信息即為SSH key,將這些信息復制到GitHub的Add SSH key頁面即可
不同的操作系統(tǒng)痊远,均有一些命令垮抗,直接將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