如何生成SSH key及访问Github

一、检查SSH key是否存在

在终端输入:

ls -al ~/.ssh

如果没有,终端显示如下:

No such file or directory

如果已经存在,则会显示id_rsa和id_rsa.pub

二、生成新的SSH key

在终端输入:

ssh-keygen -t rsa -C "[email protected]"

其中[email protected]为你在Github注册时的邮箱

成功后终端显示如下:

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):

提示你保存.ssh/id_rsa的路径,这里直接enter

Created directory '/Users/xxx/.ssh'.

Enter passphrase (empty for no passphrase):

提示输入passphrase,每次与Github通信都会要求输入passphrase,以避免某些“失误”,建议输入

成功后终端显示:

Your identification has been saved in /Users/xxx/.ssh/id_rsa.

Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.

The key fingerprint is:

16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48 [email protected]

The key's randomart image is:(后面图形省略)

三、添加key到SSH

输入命令:

ssh-add ~/.ssh/id_rsa

此时会要求输入passphrase,输入步骤二中填的passphrase

成功后,终端显示:

Identity added: /Users/xxx/.ssh/id_rsa (/Users/xxx/.ssh/id_rsa)

最后,在/Users/xxx/.ssh/生成两个文件,id_rsa和id_rsa.pub

此时,SSH key已经生成成功

四、添加SSH key到Github

1.复制id_rsa.pub中的所有内容

打开id_rsa.pub,终端命令:

vim ~/.ssh/id_rsa.pub

手动复制以ssh-rsa到以[email protected]结尾的所有内容

或者直接输入命令复制id_rsa.pub中的所有内容,终端命令:

pbcopy < ~/.ssh/id_rsa.pub

2.登录Github

打开个人Settings-->SSH keys-->Add SSH key

Title 随便写

Key 粘贴之前复制的内容

这样SSH key就添加的Github

五、检测SSH key

输入命令:

ssh [email protected]

此时会验证SSH key是否可以访问Gitbub

成功显示如下:

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

Connection to github.com closed.

文/Mawb(作者)

原文链接:http://www.jianshu.com/p/21234432c94e

你可能感兴趣的:(如何生成SSH key及访问Github)