服务器上开了个 Ubuntu 的虚拟机,版本是最新的 22.04.2 ,用来跑 CI/CD 的环境。
但是发现用 MobaXtrem 、putty 之类的 ssh 工具连不上去
修改过 ssh_config:
按理来说这样设置是没有问题的,机器重启后的第一次登录是正常的,第二次之后才不正常 提示如下:
Server refused our key
No supported authentication methods available (server sent: publickey)
────────────────────────────────────────────────────────────────────────────────
Session stopped
- Press <Return> to exit tab
- Press R to restart session
- Press S to save terminal output to file
无论是使用私钥登录,还是密码登录都是这样。我查询了很多方法,没有用。
希望各路大神可以点拨一下
说来也很奇怪,按照上面提示的信息,应该是服务器只接受验证密钥对的方式进行登录 但是我是设置了 PasswordAuthentication yes 并且在重启后也能正常使用密码登录。
但是登录一段时间后就无法再次用密码登录 并且用密钥对登录也会提示错误
1
xubeiyan 2023-08-07 09:17:25 +08:00 via Android
多半是密钥交换算法服务器觉得太老了,但你的终端软件又不支持新的。自己打开服务器的 ssh.log 看
|
2
dayeye2006199 2023-08-07 09:18:22 +08:00 via Android
连的时候,加 verbose 参数看具体原因
|
3
twofox OP |
4
icefrogisgreat 2023-08-07 09:49:43 +08:00
客户端使用命令行 ssh -v 登录 看输出
|
5
xubeiyan 2023-08-07 10:06:43 +08:00
@twofox 在`/etc/ssh/sshd_config`里找到日志等级相关的`LogLevel`,改为`LogLevel DEBUG3`
然后重启你的`sshd`(例如`sudo service ssh restart`) 再用 putty 登一下,就能看到默认在 `/var/log/auth.log`的日志文件的出错原因了 参考来自于 https://stackoverflow.com/questions/20864224/putty-getting-server-refused-our-key-error |
6
Fris 2023-08-07 11:17:53 +08:00 1
ubuntu 22.04 系统 的 openssh 默认禁用了 SHA-1 哈希算法的 RSA 签名,使用 ssh-keygen -t ed25519 生成新的密钥对就行了
|
7
twofox OP |
8
xubeiyan 2023-08-07 17:40:56 +08:00 via Android
@twofox 楼上说的只是一种可能,我现在依然用 ssh-rsa 的公钥在连 ubuntu22.04 ,并没有影响。你可以看看是否成功重启了 sshd ,log 里面没有改变那基本上是没有成功重启修改了配置的 sshd ,你试那么多 ssh 终端软件没重启 sshd 还不是没作用,贴下你的`tail -f /var/log/auth.log`,看看有没有 debug3 字样。回复时要多贴运行结果,少发你自己的操作。提供下你的 linux 发行版本,也好排查。
|
9
twofox OP @xubeiyan 重启成功了的,service sshd restart 或者是 systemctl restart sshd.service 我都执行过。下面是日志,最后两行就会一直重复。然后无论是 xshell 还是 mobaxterm 登录都再无其他日志了
``` Aug 8 00:08:46 boen sshd[150596]: Received signal 15; terminating. Aug 8 00:08:46 boen sshd[2422778]: debug3: already daemonized Aug 8 00:08:46 boen sshd[2422778]: debug3: oom_adjust_setup Aug 8 00:08:46 boen sshd[2422778]: debug1: Set /proc/self/oom_score_adj from 0 to -1000 Aug 8 00:08:46 boen sshd[2422778]: debug2: fd 3 setting O_NONBLOCK Aug 8 00:08:46 boen sshd[2422778]: debug1: Bind to port 22 on 0.0.0.0. Aug 8 00:08:46 boen sshd[2422778]: Server listening on 0.0.0.0 port 22. Aug 8 00:08:46 boen sshd[2422778]: debug2: fd 4 setting O_NONBLOCK Aug 8 00:08:46 boen sshd[2422778]: debug3: sock_set_v6only: set socket 4 IPV6_V6ONLY Aug 8 00:08:46 boen sshd[2422778]: debug1: Bind to port 22 on ::. Aug 8 00:08:46 boen sshd[2422778]: Server listening on :: port 22. Aug 8 00:08:47 boen sshd[10729]: debug3: receive packet: type 2 Aug 8 00:08:47 boen sshd[10729]: debug3: Received SSH2_MSG_IGNORE Aug 8 00:09:25 boen sshd[6748]: debug3: receive packet: type 2 Aug 8 00:09:25 boen sshd[6748]: debug3: Received SSH2_MSG_IGNORE Aug 8 00:09:28 boen sshd[7913]: debug3: receive packet: type 2 Aug 8 00:09:28 boen sshd[7913]: debug3: Received SSH2_MSG_IGNORE ``` |
10
twofox OP @xubeiyan 忘了补充版本信息
root@boen:~/.ssh# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.2 LTS Release: 22.04 Codename: jammy root@boen:~/.ssh# ssh -V OpenSSH_8.9p1 Ubuntu-3ubuntu0.3, OpenSSL 3.0.2 15 Mar 2022 |
11
xubeiyan 2023-08-08 09:14:43 +08:00
@twofox 使用公钥认证时的 sshd log 应该长这样,这是客户端等待输入用户时的
··· Aug 8 08:50:14 sakuramoyu sshd[2885479]: debug3: fd 5 is not O_NONBLOCK Aug 8 08:50:14 sakuramoyu sshd[2885479]: debug1: Forked child 2885502. Aug 8 08:50:14 sakuramoyu sshd[2885479]: debug3: send_rexec_state: entering fd = 8 config len 3256 Aug 8 08:50:14 sakuramoyu sshd[2885479]: debug3: ssh_msg_send: type 0 Aug 8 08:50:14 sakuramoyu sshd[2885479]: debug3: send_rexec_state: done Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: oom_adjust_restore Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: Set /proc/self/oom_score_adj to 0 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock 8 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: inetd sockets after dupping: 4, 4 Aug 8 08:50:14 sakuramoyu sshd[2885502]: Connection from 10.1.122.254 port 59841 on 10.1.123.123 port 22 rdomain "" Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.3 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: Remote protocol version 2.0, remote software version PuTTY_Release_0.77 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: compat_banner: no match: PuTTY_Release_0.77 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug2: fd 4 setting O_NONBLOCK Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: ssh_sandbox_init: preparing seccomp filter sandbox Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug2: Network child is on pid 2885503 Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: preauth child monitor started Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: privsep user:group 106:65534 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: permanently_set_uid: 106/65534 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: ssh_sandbox_child: setting PR_SET_NO_NEW_PRIVS [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: ssh_sandbox_child: attaching seccomp filter program [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: append_hostkey_type: ssh-rsa key not permitted by HostkeyAlgorithms [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: list_hostkey_types: rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: send packet: type 20 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: SSH2_MSG_KEXINIT sent [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug3: receive packet: type 20 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug1: SSH2_MSG_KEXINIT received [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug2: local server KEXINIT proposal [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,[email protected],diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hel lman-group18-sha512,diffie-hellman-group14-sha256 [preauth] Aug 8 08:50:14 sakuramoyu sshd[2885502]: debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 [preauth] ··· 可以看出我是使用`putty`连接的,下面有`HostKeyAlgorithms`不允许`ssh-rsa`这种类型 如果你的 SSH log 没有类似于上面的,可以考虑是不是输错了地址(连到别人的 SSH 上了也不是没这个可能)或者是直接被防火墙拦截了 |