Skip to main content

SSH Troubleshoot

Fixing Broken Pipe Error With SSH Connection

SSH connection getting disconnected due to inactivity? Here's how to handle it.

If you use SSH to connect to remote Linux servers, you'll notice that if you keep your SSH session inactive for some time and then try to use it again, the SSH session disconnects with an error message like this:

:client_loop: send disconnect: Broken pipe

On some systems, it will display 'Write failed: Broken pipe' or 'Connection closed by remote host'.

Let's see what causes this error and how to go about keeping your SSH connection alive.

Fixing broken pipe error with SSH

As you may have guessed, the SSH connection is closed because of inactivity. There is no set value but it usually around 5 minutes or so.

What you can do to avoid the SSH session disconnection is to send an 'alive message' either from the server to client (ClientAliveInterval) or from client to server (ServerAliveInterval) at certain time interval.

This way, you keep the SSH session alive because there is a communication between the client and server and the server understands that client is still there.

Now, there are two ways to do that. Either you send the alive message from the client to the server or from the server to the client.

  • If you connect to multiple servers via SSH, set it on your machine.
  • If you are a sysadmin and several of users complain about frequent SSH connection disconnect, you may set it on the server.

Method 1: Client side SSH configuration change

Let's say you want to keep your SSH connection alive with up to 10 minutes (600 seconds) of idle time.

While connecting to the remote Linux system through SSH, you can mention the ServerAliveInterval value like this:

ssh -o ServerAliveInterval=600 username@server_ip_address

Now, this thing work but manually entering this option each time you connect to the server is tiresome. Why not make it permanent?

I hope you are aware of the SSH config files. On the client side, you can take advantage of it to set certain SSH parameters for specific connections or all of them. I have explained SSH config file in detail here.

First, make sure that you have the ssh config file. If not create it:

touch ~/.ssh/config

It is important to give it the correct file permissions otherwise you'll have permission denied error while connecting via SSH.

Use the chmod command and add the following file permission to it:

chmod 600 ~/.ssh/config

If you're feeling lazy or don't want to go in detail, use this command to set the alive interval to 600 seconds (10 minutes):

echo "ServerAliveInterval 600" >> ~/.ssh/config 

This will set the ServerAliveInterval value to 10 minutes for all SSH connection you'll use. Give it a try if you want to.

If you want to make it more proper, you should add it like this:

Host *
ServerAliveInterval 600

Method 2: Server side SSH config change

The SSH config file for the server is usually located at /etc/ssh/sshd_config.

If you open this file, you'll find two parameters of interest here:

  • ClientAliveInterval: This is the inactivity time period after which the server will send an alive message to the ssh connected client.
  • ClientAliveCountMax: This is the number of attempts the server will make to send the alive message.

Say, you set ClientAliveInterval to 200 seconds and ClientAliveCountMax to 3. This means the server will send alive message after 200 seconds. If there is no activity from the client, it will again send an alive message at 400 seconds. No response/activity from the client and another alive message is sent at 600 seconds. After this (600 seconds) the SSH connection is disconnected.

You can edit the /etc/ssh/sshd_config file in your favorite terminal based text editor like Vim. Look for ClientAliveInterval and ClientAliveCountMax entries. Remove the # key at the beginning of the lines and give them the appropriate value.

Save and exit the file.

Please do not set the SSH connection timeout to several hours. That would be a waste of resources.

I hope this tutorial helped you to fix the broken pipe error issue with SSH connection. Your feedback is welcome.

Abhishek Prakash