文章目录
转载请标明出处:
https://bigmaning.blog.csdn.net/article/details/125567360
本文出自:【BigManing的博客】
一、ssh-keygen作用
ssh-keygen用来生成ssh公钥认证所需的公钥和私钥文件
二、ssh-keygen命令参数
ssh-keygen [选项]
-b
:指定密钥长度;
-e
:读取openssh的私钥或者公钥文件;
-C
:添加注释; (一般设置为自己的邮箱)
-f
:指定用来保存密钥的文件名;
-i
:读取未加密的ssh-v2兼容的私钥/公钥文件,然后在标准输出设备上显示openssh兼容的私钥/公钥;
-l
:显示公钥文件的指纹数据;
-N
:提供一个新密语;
-P
:提供(旧)密语;
-q
:静默模式;
-t
:指定要创建的密钥类型。 (常用参数)
三、ssh-keygen示例
生成的密钥对,默认会生成在用户
目录下的.ssh
目录下,密钥对格式为id_「密钥类型」
,例如 私钥:id_rsa
、公钥:id_rsa_pub
1、生成RSA类型密钥对
命令
ssh-keygen -t rsa -C Bigmaning@qq.com
输出
Generating public/private rsa key pair.
//======> 存储路径 直接回车 使用默认路径
Enter file in which to save the key (/Users/bigmaning/.ssh/id_rsa):
//======> 设置密码 直接回车 默认不设置密码
Enter passphrase (empty for no passphrase):
//======> 确认密码 操作同上(直接回车 默认不设置密码)
Enter same passphrase again:
//======> 生成的密钥对的位置
Your identification has been saved in /Users/bigmaning/.ssh/id_rsa.
Your public key has been saved in /Users/bigmaning/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:K9eP3Ik0VtCVPJtfsu4Q8UFumumtCnQxCpLMBladBg8 Bigmaning@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| o.Eo . .o. |
| . + ++ . ++ |
| *.o + o ++ |
| . . . . + B+..|
| S . * .+.|
| . + o o. .|
| . + = o.. |
| o = * +. |
| =.=.. |
+----[SHA256]-----+
2、生成ed25519类型密钥对
同rsa操作
ssh-keygen -t ed25519 -C Bigmaning@qq.com
四、使用场景
1、github免密push代码
使用ed25519
类型密钥对中的公钥,配置到github中。
- 登录你的github账号,从又上角的设置( Account Settings )进入,然后点击菜单栏的 SSH key 进入页面添加 SSH key。
- 复制
id_ed25519.pub
公钥内容 - 点击 Add SSH key 按钮添加一个 SSH key 。把你复制的公钥内容粘贴到 key 所对应的输入框中。
2、免密登陆远程服务器
嗯 配置OK后,每次登陆远程机器 只需 ssh root@xxx.xxx.xxx.xxx 命令即可,不需要输入密码了
- 生成rsa密钥对,比如公钥地址
~/.ssh/id_rsa.pub
- 复制公钥到远程机器 :
ssh-copy-id -i ~/.ssh/id_rsa.pub root@xxx.xxx.xxx.xxx
,这个步骤是需要输入远程服务器密码。 - 上步骤成功后,后续
root@xxx.xxx.xxx.xxx
就能直接登陆到远程机器了。