熊猫家族博客新版本上线,欢迎大家交流

Generating SSH Keys

如果你不使用推荐的HTTPS方法,可以使用SSH密钥建立安全连接您的计算机和GitHub。下面的步骤将引导您通过生成一个SSH密钥,然后添加到您的GitHub的帐户的公共密钥。

Step 1: Check for SSH keys

确认你本机的密钥,如果你有现有的密钥,则直接跳到step 4。

首先,我们需要检查您的计算机上现有的SSH密钥。打开终端并运行:

$ cd ~/.ssh
#确认".ssh" 目录是否存在

If it says “No such file or directory” skip to step 3. Otherwise continue to step 2.

Step 2: 备份并移除存在的SSH keys

$ ls
# Lists all the subdirectories in the current directory
 config  id_rsa  id_rsa.pub  known_hosts

$ mkdir key_backup
# Makes a subdirectory called "key_backup" in the current directory

cp id_rsa* key_backup
# Copies the id_rsa keypair into key_backup

rm id_rsa*
# Deletes the id_rsa keypair

Step 3: Generate a new SSH key

要生成新的SSH密钥,输入下面的代码。修改默认值,要求输入一个文件,保存密钥,只需按Enter键。

$ ssh-keygen -t rsa -C "your_email@youremail.com"
# Creates a new ssh key using the provided email

Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa):

按回车保存到默认位置,再稍等出来提示输入密码短语,输完按回车要输两遍;它用来加密私钥,也就是以后使用私钥的时候要输这个密码;

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

密钥生成成功,则会提示如下:

Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/you/.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@youremail.com

Step 4: 添加SSH key to GitHub

sudo apt-get install xclip
#Downloads and installs xclip

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

警告:重要的是要复制的关键,完全不添加换行符或空格。值得庆幸的是xclip的命令可以很容易地完全执行此设置。

  1. Go to your Account Settings

  2. Click "SSH Keys" in the left sidebar

  3. Click "Add SSH key"

  4. Paste your key into the "Key" field

  5. Click "Add key"

  6. Confirm the action by entering your GitHub password

Step 5: 测试

$ ssh -T git@github.com
# Attempts to ssh to github

You may see this warning:

The authenticity of host github.com (207.97.227.239) can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

输入”yes”,验证的指纹相匹配.

Hi username! You've successfully authenticated, but GitHub does not
provide shell access.