如何生成SSH key

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 emailGenerating public/private rsa key pair.Enter fileinwhichto save the key (/your_home_path/.ssh/id_rsa):

第二步:输入passphrase(本步骤可以跳过)

设置passphrase后,进行版本控制时,每次与GitHub通信都会要求输入passphrase,以避免某些“失误”

Enterpassphrase (empty for no passphrase):[Type a passphrase]Entersame passphrase again:[Type passphrase again]

sample result:

Your identification has been savedin/your_home_path/.ssh/id_rsa.Your public key has been savedin/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 backgroundeval"$(ssh-agent -s)"Agent pid 59566ssh-add ~/.ssh/id_rsa

3. 将ssh key添加到GitHub中

用自己喜欢的文本编辑器打开id_rsa.pub文件,里面的信息即为SSH key,将这些信息复制到GitHub的Add SSH key页面即可,

注:如何文本编辑器打开id_rsa.pub文件这个可能大家不清的地方是:看图一,图二,图三(我在这里被坑了挺久的)

图一



图二:id.rsa.pub才是你要添加的SHH key


图三:用word或者其他编辑器打开,要从ssh-rsa开始整个复制才是ssh key

不同的操作系统,均有一些命令,直接将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

sudoapt-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

参考链接

参考链接二(里面有动图,如何生成SSH key)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如何生成SSH key 本文载于Gevin的博客 原文地址:http://blog.igevin.info/pos...
    萧玄辞阅读 727评论 0 0
  • 转自别处: SSH key提供了一种与GitHub通信的方式,通过这种方式,能够在不输入密码的情况下,将GitHu...
    Ten_Minutes阅读 723评论 0 3
  • 操作环境为 Mac os,如果你是 Windows 请出门左转。 一、检查 SSH key 是否存在 在终端输入:...
    bincn阅读 8,546评论 1 16
  • 一、检查SSH key是否存在 在终端输入: ls -al ~/.ssh 如果没有,终端显示如下: No such...
    JackieWillen阅读 1,579评论 0 0
  • 如何生成SSH key SSH key提供了一种与GitHub通信的方式,通过这种方式,能够在不输入密码的情况下,...
    lltree阅读 215,942评论 4 29