使用密钥认证方式连接ssh2(openssh8.8以上版本服务端)

1,419 阅读4分钟

一、起因

2022年8月14日,周日,晚上我正像平常一样使用mobaxterm连接到树莓派,发现系统存在可用的更新,原本的Ubuntu 20 TLS可以更新到Ubuntu 22.04.1 LTS啦,内核也能从5.4.0升级到5.15.0。

我没有经受住诱惑。

升级过程还是很简单的,因为ubuntu贴心地提供了升级工具,只需要用root权限执行do-release-upgrade命令即可。但是升级完成后重启系统,发现ssh无法正常连接了,我的树莓派因为要挂到公网,所以老早就配置了证书登录,结果系统升了个级,xshell和mobaxterm在连接服务端时都报错说公钥未注册。

二、排查

xshell报错时显示了服务端ssh的版本信息,mobaxterm报错时显示服务器拒绝了现有的私钥,如图:

ssh_ver_err.png

ssh_deny.png

所以可能是服务端升级ssh服务端导致密钥丢失、配置文件不兼容、密钥不兼容等原因。

使用mobaxterm的报错信息搜索,几乎全都是让我启用密码认证,确实,启用密码认证真的能解决这问题。。。

使用xshell的报错信息搜索也基本是一样的效果,甚至有的需要修改文件权限新建~/.ssh2目录等都试过,并未解决问题。

直到使用关键字xshell SSH2, OpenSSH_8.9p1搜索,终于找到有用的信息,新版ssh服务端使用的加密方式发生了变化,默认不再支持rsa格式密钥,可以显式修改服务端配置,使服务端再次支持rsa密钥。但是官方既然已经认为rsa密钥不再安全,我这边也只好更换官方支持的密钥格式。

然后在试图使用xshell生成新密钥时就发现了这个:

ssh2_key.png

三、解决方案

后来又经过了一系列的乱七八槽的尝试,最终解决方案总结如下。

1.生成密钥对

生成密钥可以使用xshell的功能,也可以使用mobaxterm的功能,最终我使用了最新版puttygen,版本号0.77,密钥制作方式如下图:

makekey.gif

2.配置服务端

将上一步复制的公钥信息粘贴到服务器~/.ssh/authorized_keys,注意不要覆盖已有的公钥,每条公钥信息各自独占一行。

然后修改服务器/etc/ssh/sshd_config文件,最终内容大致如下(仅代表OpenSSH_8.9p1版本):

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

# Authentication:
PermitRootLogin no  # 1.禁止root账号使用ssh登录
PubkeyAuthentication yes  # 2.使用公钥认证

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no  # 3.禁止使用密码认证

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes
X11Forwarding yes
PrintMotd no

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem	sftp	/usr/lib/openssh/sftp-server

修改配置文件后重启ssh服务。

3.配置xshell

  1. 使用alt+o快捷键打开会话管理窗口。
  2. 右击服务器会话,点击属性
  3. 属性窗口左侧点击“用户身份验证”,右侧“方法”选择“publickey”,“用户密钥”后面点击“浏览”

property.png

  1. “用户密钥”窗口点击“导入”,选择puttygen工具生成的密钥文件,根据提示输入密码(生成密钥时填写的密码)

import.png

  1. 在属性窗口的密码栏也填入相同的密码,点击“确定”
  2. 最后在会话管理窗口点击“连接”即可正常连接到ssh服务。

4.配置mobaxterm

  1. 升级mobaxterm到最新版V22.1,低版本不支持最新puttygen生成的密钥。
  2. 创建会话时填写服务器地址、端口、账号信息后进入“Advanced SSH settings”标签页。
  3. 勾选“Use private key”并选择puttygen生成的私钥文件。
  4. 点击“ok”保存会话信息,登录时按提示输入密码即可。

session.png

其他客户端如juicessh也都大同小异了,注意客户端可能都需要使用最新版