The solution to the frequent disconnection of the ssh connection server

When using sshto connect to a remote server, it is often encountered that the connection is disconnected shortly after the connection is made. This is because sshthe unique session connection mechanism of the service judges whether the client is alive or does not take action for a long time and actively disconnects it. Open, such a mechanism often has some misjudgments.

Such a mechanism can be modified by setting sshd_configthe file :
1. Open the sshd_config file

imaginemiracle@im-Linux:~$ sudo vim /etc/ssh/sshd_config

2. Add the following two lines of configuration, save and exit

# 设定每隔多少秒给 ssh 客户端发送一次信号
ClientAliveInterval 60
# 设定超过多少秒后断开与 ssh 客户端连接
ClientAliveCountMax 86400

[注]:此处的配置名称需要拼写正确,如若不然则会导致 sshd 无法重启。
3. Restart the ssh service

imaginemiracle@im-Linux:~$ sudo service sshd restart

4. Convenient modification of the shell command line
[注]:执行过 1、2、3 步骤后可跳过第 4 步,或仅使用第 4 步,一步搞定。

sudo sed -i 's/^export TMOUT=.*/export TMOUT=0/' /etc/profile &&sudo sed -i "/#ClientAliveInterval/a\ClientAliveInterval 60" /etc/ssh/sshd_config &&sudo sed -i "/#ClientAliveInterval/d"                        /etc/ssh/sshd_config &&sudo sed -i '/ClientAliveCountMax/ s/^#//'                   /etc/ssh/sshd_config &&sudo /bin/systemctl restart sshd.service

You can also copy and write the commands here into a shellscript to save and use.
At this point, reconnecting to the server will no longer disconnect frequently!

If you think this article is helpful to you, please leave a like~ *v*
Please respect the author, and please indicate the source when reprinting! Thanks for your cooperation~
[Initial Release Date]: 2022-10-09
[Author]: Imagine Miracle
[Copyright]: This work is licensed under the Creative Commons Attribution-Non-Commercial-Share Alike 4.0 International License .
[Link to this article]: https://blog.csdn.net/qq_36393978/article/details/127228176

Guess you like

Origin blog.csdn.net/qq_36393978/article/details/127228176