How to Solve Broken Pipe Message in SSH Session

Sometimes my SSH session disconnects with a Write failed: Broken pipe message. What does it mean? And how can I keep my session open?

It’s possible that your server closes connections that are idle for too long. You can update either your client (ServerAliveInterval) or your server (ClientAliveInterval).

ServerAliveInterval Sets a timeout interval in seconds after which if no data has been
received from the server, ssh(1) will send a message through the encrypted channel to request
a response from the server.
The default is 0, indicating that these messages will not be sent to the server.
This option applies to protocol version 2 only. 

ClientAliveInterval Sets a timeout interval in seconds after which if no data has been received
from the client, sshd(8) will send a message through the encrypted channel to request a response
from the client.
The default is 0, indicating that these messages will not be sent to the client. 

This option applies to protocol version 2 only. 

To update your server (and restart your sshd)

 echo "ClientAliveInterval 60" | sudo tee -a /etc/ssh/sshd_config 

Or client-side:

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

Although you have configured all the above parameters, sometimes the connection just breaks. To solve this problem, I wrote this shell script to automatically restart ssh as soon as it breaks down.


#!/bin/sh

#This is an SSH-D proxy with auto-reconnect on disconnect

#Created by Liang Sun on 28, Sep, 2011
#Email: i@liangsun.org

i=0
while test 1==1
do
    remote_ip=YOUR_REMOTE_IP
    remote_user=YOUR_REMOTE_USER
    exist=`ps aux | grep $remote_user@$remote_ip | grep 9090`
    #echo $exist
    if test -n "$exist"
    then
        if test $i -eq 0
        then
            echo "I'm alive since $(date)"
        fi
        i=1
    else
        i=0
        echo "I died... God is bringing me back..."
        ssh $remote_user@$remote_ip -f -N -D 0.0.0.0:9090
    fi
    sleep 1
done

(Replace YOUR_REMOTE_USER and YOUR_REMOTE_IP with the username and ip address of your remote server correspondingly.)

If you want to connect to your remote machine via SSH without a password, go to http://liangsun.org/2011/11/ssh-login-without-password/

Leave a comment ?

13 Comments.

  1. network’unstability may also cause this problem

  2. :roll: network’s instability may also cause the problem

  3. Network’s instability may also cause the problem

  4. Network’s instability may also cause the problem.
    Hope comment successfully this time. :wink:

  5. where i need to place the script that you just make??

    • Place it into a file, then run the file with sh.
      If you name the file proxy.sh, then you can run this command:
      sh proxy.sh

  6. Please could you tell me how exactly do I use this script: i run it in console, but does this mean that whenever my ssh connection in other console window breaks, it recoonects it automatically without entering password, am I correct?

  7. I think that one can alternatively use autossh (“autossh is a program to start a copy of ssh and monitor it, restarting it as necessary should it die or stop passing traffic.”) and add it to crontab.

  8. script did not help me. I have successfully configured a script to connect to host without a password. But it fails after an idle time. While running the proxy script, I get the first echo message ‘I’m alive’. But the second one ‘I died’ is not getting displayed after the idle time. Does it have anything to do witht he 9090?

    • The message ‘I died’ is not intended for displaying after the idle time, instead it will display when the connection is broken and the ssh process exits.

    • Do you use another port instead of 9090? If yes, you need to alter the port in this line:
      exist=`ps aux | grep $remote_user@$remote_ip | grep 9090`

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>