Linux Commands

How to Prevent the SSH Broken Pipe Error

When connecting to a remote server, you can do so using a secure transmission protocol such as SSH. With SSH, you need the remote IP of the machine that you wish to connect to and the username. Once you execute the SSH connect command, you will get prompted to enter the password to complete the connection.

Unfortunately, you may sometimes run into the SSH broken pipe error which disconnects you from the remote server and hinders your activities. You will quickly get frustrated when you don’t understand what this error means or how you can fix it. This post explains everything about preventing the SSH broken pipe error to avoid interruptions. Take a look!

What Is the SSH Broken Pipe Error

There are different instances when the SSH broken pipe error may occur. In most instances, you are trying to log in to a remote machine only for the error to occur after you log in and you end up getting disconnected. The error can frustrate you, especially since you must initiate the connection repeatedly.

The broken pipe error signifies that the client and the server can’t connect, and their TCP has been broken. Sometimes, the error may arise when there is a timeout error of the interval period for requests between the client and the server. The client relies on the interval period to receive responses. When none gets shared or the set intervals are reached, you will encounter the broken pipe error.

How to Prevent the SSH Broken Pipe Error

The SSH broken pipe error is undesirable; you can use the different measures to ensure that you don’t encounter it. The aim is to keep the connection between the client and the server alive by adjusting various metrics in the SSH config file for the client and the server. There are two metrics to note when preventing the SSH broken pipe: ClientAliveInterval and ClientAliveCountMax.

The ClientAliveCountMax is the number of cycles after which the connection is interrupted if the SSH client is unresponsive. The ClientAliveInterval is the time interval in seconds in which the server takes to send the null data packets to a connected client to ensure that the connection is kept alive.

Here’s how you can prevent the SSH broken pipe error:

On the Client Side

You can specify how long to keep a SSH connection alive before the disconnection occurs. That way, you increase the ServerAliveInterval. You can adjust it when making your SSH connection or creating a config file on the client side.

When connecting via SSH, you can use the following syntax to prevent the SSH broken pipe:

ssh -o ServerAliveInterval=300 username@server_ip

Alternatively, you can create an SSH config file. We used the “touch” command in this case:

touch ~/.ssh/config

Once created, change its file permissions correctly with the following command:

chmod 600 ~/.ssh/config

Lastly, echo the preferred ServerAliveInterval. We set it to 5 minutes in the following example:

echo “ServerAliveInterval 300>> ~/.ssh/config

With that, you prevented the SSH broken pipe error.

On the Server Side

You can get the config file in the /etc/ssh/sshd_config location on the server side. The file has the ClientAliveInterval and the ClientAliveCountMax metrics which you can configure to help prevent the SSH broken pipe error.

If your ClientAliveInterval is set to 100 seconds and the ClientAliveCountMax is set to 4, the server send the alive messages every 100 seconds four times. After which, it displays an SSH broken pipe error.

So, to prevent the error from occurring, you must open this config file and set your preferred parameters. By default, the metrics are commented out. Remove the hashtag (#) at the start to uncomment them. Here’s an example of configuring and uncommenting the metrics:

Save your file and exit. With that, you won’t face the error at any instance.

Conclusion

The SSH broken pipe error occurs when the client fails to get responses from the client which causes the connection to disconnect. You can prevent this by adjusting the ClientAliveInterval and the ClientAliveCountMax on the server side. Still, you can use the ServerAliveInterval metric when connecting to it via SSH or create a SSH config file on the client and add the ServerAliveInterval. This post elaborated on the two options in detail.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.