centos 7 短时间后SSH连接关闭

centos 7 短时间后SSH连接关闭

当我尝试通过 ssh 连接 centos 7 服务器时,它连接时要求输入用户名密码,我输入然后登录。但过了一会儿,连接关闭并且没有响应。然后我进入服务器控制台,向互联网上的任何地方发送 ping(有时需要重新启动网络),它会成功。之后,我再次尝试来自客户端的 ssh 连接,它会重新连接,但在短时间内再次关闭连接。

当我使用 systemctl status network 检查网络状态时:

network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: active (exited) since Thu 2022-02-17 10:28:03 +03; 43min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 4314 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)
  Process: 4551 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
    Tasks: 0

为什么设置退出了?还有预设:禁用会导致问题吗?这是以太网连接配置:

DEVICE=ens18
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
HWADDR=<hw mac address hidden>
IPADDR=<server ip hidden>
NETMASK=255.255.255.192
GATEWAY=<gateway ip hidden>
DNS1=<dns ip hidden>
DNS2=<dns ip hidden>
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=ens18

SSH 客户端日志:

$ ssh root@<server ip> -v
OpenSSH_7.7p1, OpenSSL 1.0.2p  14 Aug 2018
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to <server ip> [<server ip>] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_ed25519-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_xmss type -1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/serhat/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.7
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to <server ip>:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:MiFljnjU0tNBZzpOa2tl5oean+77W4wZ1/cy8f2ULeU
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:MiFljnjU0tNBZzpOa2tl5oean+77W4wZ1/cy8f2ULeU.
Please contact your system administrator.
Add correct host key in /c/Users/serhat/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /c/Users/serhat/.ssh/known_hosts:3
ECDSA host key for <server ip> has changed and you have requested strict checking.
Host key verification failed.

答案1

在 SSH 上,服务器可能会在一段时间不活动(客户端和服务器之间没有通信)后关闭连接。您可以通过客户端设置或服务器设置来处理此行为。

在客户端编辑文件 ~/.ssh/config (如果不存在则创建它):

Host *
    ServerAliveInterval 240

ServerAliveInterval 指定客户端在一段时间后向服务器发送信号。

如果您有权访问服务器,则可以修改文件 /etc/ssh/sshd_config:

ClientAliveInterval 300
ClientAliveCountMax 10

这将导致服务器每 5 分钟不活动就向客户端发送一个信号。在关闭连接之前,它会执行 10 次。

相关内容