如何生成SSH key
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何生成SSH key相关的知识,希望对你有一定的参考价值。
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页面即可
不同的操作系统,均有一些命令,直接将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 参考技术A 一:生成KEY
在终端中输入:ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key重新建立ssh_host_dsa_key文件
以下是返回信息
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):(直接回车)
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx root@localhost.localdomain
在终端中输入:ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key重新建立ssh_host_rsa_key文件
以下是返回信息
Generating public/private rkey pair.
Enter passphrase (empty for no passphrase):(直接回车)
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_rey.pub.
二
key生成在目录/etc/ssh
没有.ssh目录的话手动创建,注意,它的目录权限是700
cat ssh_host_dsa_key.pub >> /root/.ssh/authorized_keys
ssh_host_dsa_rey这个放到客户端
/etc/ssh/sshd_config配置如下
Port 22
PubkeyAuthentication yes
AuthorizedKeysFile /root/.ssh/authorized_keys
PasswordAuthentication no /*禁止密码验证登录本回答被提问者和网友采纳
如何生成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的主要内容,如果未能解决你的问题,请参考以下文章