如何生成SSH key
本文載于Gevin的博客
原文地址:http://blog.igevin.info/posts/generate-ssh-key-for-git/
SSH key提供了一種與GitHub通信的方式袒餐,通過這種方式古今,能夠在不輸入密碼的情況下勺美,將GitHub作為自己的remote端服務器,進行版本控制
步驟
- 檢查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"
默認會在相應路徑下(/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
頁面即可
不同的操作系統(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
本文首載于Gevin's blog