Prevent SSH Connection Timing Out / Broken Pipe

Both as a user of Windows as well as by Mac (in the last 10 years) I spent a lot of time in SSH connections to web servers, email servers, cloud and other backup packages. Linux's preferred distribution is undoubtedly CentOS.

One of the most stressful things to do administratorii server is to interrupt SSH connections. Either suddenly while buttoning in Putty or Terminal (most often being a local connection issue), or after a while of "idle”- the period of time in which it does not interact with the remote server through the SSH session.

If you are using the Mac and use the Terminal utility for the remote connection via SSH, then you have certainly been disconnected with the message:client_loop: send disconnect: Broken pipe”. I gave the solution in the article here, in which I said that it can be added in "/etc/ssh/ssh_config" the line:

Host *
ServerAliveInterval 120

The above solution is valid for users of Mac, and honestly to be the latest updates of macOS I don't mind restart changes made to the file "ssh_config", And the problem with disconnecting an idle session returns.

A solution by which we can prevent the disconnection of an SSH session at a distance set by Port (macOS) or Putty (Windows), is like besides “ServerAliveInterval”From our computer, to determine the remote server to communicate periodically with the SSH application / client.
That's why we have to put the directive "ClientAliveInterval"In"sshd_config”On the server we are connecting to.

Prevent SSH Connection Timing Out / Broken Pipe (ssh_config Tips)

1. We connect to the server we want to activate and set a time interval for "ClientAliveInternal". We open SSH in Putty, Terminal or other similar utility and we authenticate with the user root.

ssh root@server.hostname

2. Execute the command line to search the "sshd_config" file if "ClientAliveInterval" is active and what time period is set.

sudo grep "ClientAliveInterval" /etc/ssh/sshd_config

In our scenario "ClientAliveInterval" is disabled and the time interval value is zero.

[root@buffy ~]# sudo grep "ClientAliveInterval" /etc/ssh/sshd_config
#ClientAliveInterval 0
[root@buffy ~]# 

The hashtag "#"Placed in front of a line, cancels it. She is inactive.

3. We open with the editor "nano"Or"vim"File"sshd_config“. I prefer "nano".

sudo nano /etc/ssh/sshd_config

4. Delete “#"In front of the line"ClientAliveInterval”And set a number of seconds: 60, 120…

ClientAliveInterval 120
ClientAliveCountMax 10

5. Save the changes and restartI have the service "sshd".

sudo systemctl restart sshd

ClientAliveInterval : It is the time interval in seconds that the server sends a null data packet to the client / application through which we are connected to the server. This practice will keep the connection alive / active.

ClientAliveCountMax : Unresponsive SSH clients in 10 cycles of 120 seconds (set by “ClientAliveInterval”) the connection will be interrupted. That is, after 20 minutes in which the SSH client did not respond to null packets sent by the server.

With these changes the SSH connection will be more stable and remain just as secure.

Passionate about technology, I like to test and write tutorials about operating systems macOS, Linux, Windows, about WordPress, WooCommerce and configure LEMP web servers (Linux, NGINX, MySQL and PHP). I write on since 2006, and a few years later I started writing on iHowTo.Tips tutorials and news about devices in the ecosystem Apple: iPhone, iPad, Apple Watch, HomePod, iMac, MacBook, AirPods and accessories.

Leave a Comment