Ubuntu 下 SSH 连接后闪退/自动断开,通常和服务端配置、客户端环境、资源限制、网络有关。下面按最常见 → 较少见顺序给你排查和处理方法。
在 Ubuntu 服务器上执行:
sudo tail -n 50 /var/log/auth.log
常见报错及含义:
Connection closed by ... → 被服务端主动断开error: Could not get shadow information → 用户/权限问题fatal: mm_request_send: write: Broken pipe → 内存或 PAM 问题session opened then closed → shell 启动失败如果 .bashrc / .profile 中有:
exit会导致 一登录就退出。
✅ 解决:
ssh user@ip "bash --noprofile --norc"
或单用户模式修改:
mv ~/.bashrc ~/.bashrc.bak
检查:
grep youruser /etc/passwd
应为:
user:x:1000:1000::/home/user:/bin/bash
如果是 /bin/false 或 /usr/sbin/nologin → 会闪退
✅ 修复:
sudo chsh -s /bin/bash youruser
df -h
如果 / 或 /home 是 100%:
✅ 清日志或扩容
free -h
dmesg | grep oom
✅ 释放内存或加 swap
看起来像闪退,其实是空闲断开
编辑服务器:
sudo nano /etc/ssh/sshd_config
添加:
ClientAliveInterval 60
ClientAliveCountMax 3
重启:
sudo systemctl restart ssh
sudo nano /etc/security/limits.conf
如果有:
* hard nproc 0
会导致闪退
本地测试:
ssh -v user@ip
看断在哪一步
尝试:
ssh -o ServerAliveInterval=60 user@ip
✅ 换 Terminal / FinalShell / OpenSSH 测试
sudo ufw status
fail2ban-client status
你可以按顺序试:
/var/log/auth.log.bashrcClientAlive如果你愿意,可以把下面信息发我,我可以直接帮你定位:
ssh -v 输出/var/log/auth.log 报错我可以一步一步带你修。