如何生成SSH key及访问Github
Posted 阿奇XS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何生成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 "your_email@example.com"
其中your_email@example.com为你在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 your_email@example.com
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到以your_email@example.com结尾的所有内容
或者直接输入命令复制id_rsa.pub中的所有内容,终端命令:
pbcopy < ~/.ssh/id_rsa.pub
2.登录Github
打开个人Settings-->SSH keys-->Add SSH key
Title 随便写
Key 粘贴之前复制的内容
这样SSH key就添加的Github
五、检测SSH key
输入命令:
ssh git@github.com
此时会验证SSH key是否可以访问Gitbub
成功显示如下:
Hi your_name! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
以上是关于如何生成SSH key及访问Github的主要内容,如果未能解决你的问题,请参考以下文章
如何生成 SSH keys, 并在 Github 或 Gitlab 等上添加密钥