请教一个 ubuntu 无法登录的问题

280 天前
 twofox

服务器上开了个 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

无论是使用私钥登录,还是密码登录都是这样。我查询了很多方法,没有用。

希望各路大神可以点拨一下

991 次点击
所在节点    问与答
13 条回复
xubeiyan
280 天前
多半是密钥交换算法服务器觉得太老了,但你的终端软件又不支持新的。自己打开服务器的 ssh.log 看
dayeye2006199
280 天前
连的时候,加 verbose 参数看具体原因
twofox
280 天前
@xubeiyan auth.log 没有什么异常,只有一个 pam_linux(corn:session),百度了一下,看起来是周期任务


@dayeye2006199 这个参数怎么加,没搜到
icefrogisgreat
280 天前
客户端使用命令行 ssh -v 登录 看输出
xubeiyan
280 天前
@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
Fris
280 天前
ubuntu 22.04 系统 的 openssh 默认禁用了 SHA-1 哈希算法的 RSA 签名,使用 ssh-keygen -t ed25519 生成新的密钥对就行了
twofox
280 天前
@xubeiyan 使用客户端连接报错,没有在日志找到错误原因,甚至都没有反应。xshell 、mobaxterm 、git 自带的 ssh 我都试过了


@Fris 我看网上有说是这个原因。我试着加入了这个新的密钥,也无法访问
xubeiyan
280 天前
@twofox 楼上说的只是一种可能,我现在依然用 ssh-rsa 的公钥在连 ubuntu22.04 ,并没有影响。你可以看看是否成功重启了 sshd ,log 里面没有改变那基本上是没有成功重启修改了配置的 sshd ,你试那么多 ssh 终端软件没重启 sshd 还不是没作用,贴下你的`tail -f /var/log/auth.log`,看看有没有 debug3 字样。回复时要多贴运行结果,少发你自己的操作。提供下你的 linux 发行版本,也好排查。
twofox
279 天前
@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

```
twofox
279 天前
@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
xubeiyan
279 天前
@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,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,sntrup761x25519-sha512@openssh.com,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 上了也不是没这个可能)或者是直接被防火墙拦截了
twofox
279 天前
@xubeiyan 确实没有类似的日志出现。奇了怪了。也不会连接错的,我都开了两个会话连接成功了。我再琢磨琢磨
twofox
277 天前
@xubeiyan 裂开,我知道原因了。docker 里面跑着 gitlab ,占用了 22 端口
所以我 reboot 后的一小段时间,我可以登录。等 gitlab 跑起来了,我再连接就 tm 连接的 gitlab 的容器了!
我真是个傻逼

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/962909

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX