Termux的ssh连接不上,Connection to 192.168.1.5 closed by remote host.(半解决)

oldsaltfish / 2024-11-10 / 原文

背景

拿另一个旧手机玩termux,但是意外地发现ssh连不上,起初以为是我操作有问题,配置不当,因为新版的openssh我不是很了解。

客户端显示如下:

PS C:\Users\acer> ssh u0_a216@192.168.1.5 -p 8011
u0_a216@192.168.1.5's password:
Connection to 192.168.1.5 closed by remote host.
Connection to 192.168.1.5 closed.

如果故意输错密码,会提示重新输出,因此不是密码问题。

服务端执行如下命令,可以看到调试信息

# 结束当前sshd进程
pkill sshd
# 重新启动sshd
sshd -d

以下是日志信息中最重要的部分。

debug1: pty_setowner: no tty group
chown(/dev/pts/1, 10216, 10216) failed: Permission denied
debug1: do_cleanup

这段信息后,ssh服务端就被关闭了。

试解决

最接近的问题如下,说是可以关闭SELinux来解决。
https://github.com/tiann/KernelSU/issues/1727

# 以下命令必须有root权限
su
# 查看selinux状态,Enforcing(强制,也就是开启),Permissive(宽容)
getenforce
# 设为宽容
setenforce 0
# 设为强制
setenforce 1

此处我们把selinux设为宽容,可以发现能够连上ssh了。

但是关掉selinux会有一些衍生问题,比如打不开某些银行软件之类的。很多手机不需要关闭selinux就能连上ssh。