ssh免密登录

1618 字
8 分钟
ssh免密登录

一、Windows的SSH免密登录操作步骤#

1.1 客户端生成秘钥对#

在Windows客户端中创建秘钥对,以xshell为例创建秘钥对

image-20260119113007105
image-20260119113007105

秘钥参数中的秘钥类型秘钥长度可自定义选择,这里默认即可,点击下一步

image-20260119113059912
image-20260119113059912

点击完成下一步后再点击下一步,来到这个界面

image-20260119113303583
image-20260119113303583

秘钥名称注释按需求设置,其中秘钥的密码可设置,设置后每次使用秘钥需要输入密码,这里不进行密码设置,直接下一步来到公钥界面

image-20260119113529886
image-20260119113529886

公钥的格式也有多个可选,这里默认选择常用的为ssh2-openssh,点击完成即可。

至此,Windows客户端就生成了一个秘钥对,一个公钥加一个秘钥(私钥)

  • SSH1:已废弃,不安全,强烈建议不要使用
  • SSH2 - OpenSSH:现代标准,绝大多数 Linux 系统默认支持。
  • SSH2 - IETF SECSH:较新标准,也兼容 OpenSSH,但较少见。使用该格式时若ssh服务端不支持,这需要转换格式或者更换ssh服务端

1.2 将公钥上传至服务器#

手动将公钥内容复制或者将保存下来的公钥文件上传到服务器中

存放公钥的默认文件位置和名称(若不存在创建即可)

Terminal window
~/.ssh/authorized_keys
# 修改权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

修改sshd配置文件

Terminal window
vim /etc/ssh/sshd_conf
# 禁用密码认证
PasswordAuthentication no
# 禁用空密码登录(默认通常已是 no)
PermitEmptyPasswords no
# 确保启用公钥认证
PubkeyAuthentication yes
# 指定 authorized_keys 文件路径(通常默认即可)
AuthorizedKeysFile .ssh/authorized_keys
# (可选)禁止 root 直接登录(推荐)
PermitRootLogin no
# 或设为 prohibit-password(等同于只允许密钥)
# PermitRootLogin prohibit-password

重启sshd服务

Terminal window
systemctl restart sshd

1.3 测试#

在客户端使用生成的密钥对进行测试,这里使用xshell进行测试

image-20260119133013221
image-20260119133013221

新建标签页,然后设置好名称主机、和 端口号,然后点击用户身份验证

image-20260119133757887
image-20260119133757887

用户名:为root或其他可登录用户

密码:输入密码、如果使用的是秘钥登录,那么这里为空

方法:主要使用pubic keypassword

如果使用的是密码登录,那么在密码输框输入密码并将password`勾选并上移至第一选项,如果使用`秘钥登录,那么密码输入框为空并将public key上移到第一选项,之后点击设置选择秘钥

image-20260119134413567
image-20260119134413567

如果秘钥设置了密码,那么可以摘密码输入框中输入密码。

设置完成后点击确定连接即可

选择的即为秘钥对中的私钥,不可泄露,使用秘钥可以生成公钥

二、linux的SSH免密登录操作#

2.1 linux客户端创建秘钥对#

Terminal window
# 推荐使用 ed25519(更安全、更快)
ssh-keygen -t ed25519
# 如果配置了邮箱服务,可加-C参数
ssh-keygen -t ed25519 -C "your_email@example.com"
# 或使用 RSA(兼容性更好,但需指定长度)
# ssh-keygen -t rsa -b 4096
# ======================================================
[root@mater-22 ~/.ssh]#ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519): # 指定秘钥对保存路径,空为默认
Enter passphrase (empty for no passphrase): # 秘钥密码
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519.
Your public key has been saved in /root/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:iyIZe5+EhfLt5w7wwv2fphndIAMpk0NRJ4JPbrc2x0I root@mater-22
The key's randomart image is:
+--[ED25519 256]--+
| .+oo . |
| ...o + |
| += o |
| +=E. |
| o.oo.oS . |
| B B=.o= o |
| + B.B+o . . |
| o * +.o.. |
| ++*+o |
+----[SHA256]-----+
# ==================================================================

2.2 将公钥上传至服务端#

使用 ssh-copy-id(最简单,推荐)

Terminal window
# 格式:ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
ssh-copy-id -i /root/.ssh/id_ed25519.pub root@10.1.1.28

输入命令回车,提示输入 服务器密码(仅这一次),然后自动将公钥追加到服务器的 ~/.ssh/authorized_keys 文件中。

手动上传的话和上边Windows的方式一样。

2.3 修改sshd配置文件#

Terminal window
vim /etc/ssh/sshd_config
# 禁用密码认证
PasswordAuthentication no
# 禁用空密码登录(默认通常已是 no)
PermitEmptyPasswords no
# 确保启用公钥认证
PubkeyAuthentication yes
# 指定 authorized_keys 文件路径(通常默认即可)
AuthorizedKeysFile .ssh/authorized_keys
# (可选)禁止 root 直接登录(推荐)
PermitRootLogin no
# 或设为 prohibit-password(等同于只允许密钥)
# PermitRootLogin prohibit-password

2.4 测试#

Terminal window
# 格式:ssh 用户名@ip -p 端口
[root@mater-22 /]#ssh root@10.1.1.28
Last login: Mon Jan 19 14:41:04 2026 from 192.168.106.22
[root@nfs-28 ~]#

可以看到主机名已经从mater-22切换至nfs-28,说明已经登录成功,且没有输入密码。

Terminal window
# 正常密码登录过程
[root@nfs-28 ~/.ssh]#ssh root@10.1.1.22
root@10.1.1.22's password:
Last login: Mon Jan 19 14:47:10 2026 from 10.1.1.28
[root@mater-22 ~]#
[root@mater-22 ~]#

三、服务器查看公钥#

服务器中存在两个公钥:

  • 一个为服务器主机公钥(Host Key)

  • 一个为登录公钥(User Public Key)

3.1 服务器主机公钥#

第一次登录服务器要求保存的就是:主机公钥(Host Key)

  • 是谁的服务器自己的
  • 作用认服务器,防止你连到假服务器
  • 存在哪:/etc/ssh/ssh_host_rsa_key.pub
  • 你只负责保存,不负责生成

3.2 登录公钥#

是谁的用户自己的生成,生成的为秘钥对,包含了公钥和私钥

作用证明你是你,让服务器允许你登录

存在哪~/.ssh/authorized_keys

用户生成,然后上传到服务器中

3.3 主机公钥、登录公钥、私钥的通俗理解#

你去开一个带门禁的保密机房

① 主机公钥 = 机房的门牌 / 身份证#

  • 第一次去,你看一眼门牌:哦,这是真机房
  • 以后再来,先对门牌:门牌没变,才进去
  • 这就是:主机公钥

② 你的登录公钥 = 机房里录入的你的门禁卡信息#

  • 你提前把卡的信息交给管理员,录入系统
  • 这就是:把 id_rsa.pub 放进 authorized_keys

③ 你的私钥 = 你手里真正的门禁卡#

  • 你刷卡 → 系统对比你录入的信息
  • 对上了 → 开门
  • 这就是:私钥登录

主机公钥服务器的身份证,用户保存,用来验证服务器的真假。

登录公钥用户的身份信息,放服务器,用来认证。

私钥只和用户自己的登录公钥配对,是用户登录的凭证。

四、主机查看公钥#

4.1 查看用户主机公钥#

4.1.1 xshell#

image-20260225114944773
image-20260225114944773

进入后

image-20260225115034207
image-20260225115034207

之后与/etc/ssh/ssh_host_rsa_key.pub中的主机公钥进行比对,看是否一致

4.1.2 CRT#

image-20260225130548743
image-20260225130548743

进入后

image-20260225130656566
image-20260225130656566

4.2 查看登录公钥#

4.2.1 xshell#

image-20260225130833340
image-20260225130833340

进入后

image-20260225130911267
image-20260225130911267

image-20260225130937001
image-20260225130937001

4.2.2 CRT#

4.2.2.1 生成秘钥对#

image-20260225131621285
image-20260225131621285

4.2.2.2 查看登录公钥#

image-20260225132125852
image-20260225132125852

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

ssh免密登录
https://2233941.xyz/posts/ssh免密登录/
作者
lumifly
发布于
2026-04-18
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
lumifly
不逐世间万丈星河,自守一隅细碎明光
✨ 今日一言
" 加载中... "
——
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
本年还剩 -- --%
本月还剩 -- --%
本周还剩 -- --%
距离--
--
--
天气预报
站点统计
文章
24
分类
1
标签
15
总字数
136,852
运行时长
0
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.12.1
文章许可
CC BY-NC-SA 4.0

文章目录