What can I do to configure SSH on both client and servers to prevent Write Failed: broken pipe
errors? It often occurs if you sleep your client computer and resume later.
What can I do to configure SSH on both client and servers to prevent Write Failed: broken pipe
errors? It often occurs if you sleep your client computer and resume later.
I have tried this in
/etc/ssh/ssh_config
for Linux and Mac:This is how often, in seconds, it should send a keepalive message to the server. If that doesn't work then train a monkey to press enter every two minutes while you work.
You could set either
ServerAliveInterval
in/etc/ssh/ssh_config
of the client machine orClientAliveInterval
in/etc/ssh/sshd_config
of the server machine. Try reducing the interval if you are still getting the error.Configuration for a single user can be set in file
~/.ssh/config
both on the server and client side. Make sure the file has correct permissionschmod 644 ~/.ssh/config
.SSH sessions may break due to numerous and possibly unavoidable reasons.
A useful utility which can be used to mitigate problems caused by this is called
screen
. Screen is a powerful utility that allows you to control multiple terminals which will stay alive independently of the ssh session. For example, if you runscreen
in an ssh session you will see a new terminal open and you can use that to run jobs. Lets say your ssh session dies in the process. Runningscreen -d
thenscreen -r
will reopen the last session and you will be able to continue from there. Make sure you read some of the documentation before using it.Client configuration
Try creating the file:
Add the contents:
Now ssh to your server and see if your problem is fixed. ClientAliveInterval option is only useful when configuring the ssh server (aka sshd), it does not change a thing on the ssh client side, so don't use it in the above configuration file.
This will send a hello-are-you-there signal to the server if no packets have been received in the preceding 30 seconds (as specified above). However, if the number of consecutive hello-are-you-there signals reach ServerAliveCountMax then ssh will disconnect from the server. This value is defaulting to 3 (so 3*30 = 90 seconds without server activity), increase it if it suits your needs. There are alot more config options to the .ssh/config file and you could read:
Using an SSH Config File
For more information on other options. You may not want to apply this to every server you connect to which this example will. Or restrain it to only a particular server by replacing the line
Host *
withHost <IP>
(replace by an IP address, see ssh_config man page).Server configuration
Similarly you can tell the server to be gentle with your clients. The configuration file is
/etc/ssh/sshd_config
.You can either deactivate it by setting
ClientAliveInterval
to0
or tweakClientAliveInterval
andClientAliveCountMax
to set a maximum ssh client inactivity without responding to the probes. One advantage of this settings over TCPKeepAlive is that the signals are sent through the encrypted channels, so it is less likely to be spoofable.I'm remotely upgrading an Ubuntu server from lucid to precise and lost the ssh connection in the middle of the upgrade with the message "Write failed. Broken pipe". ClientAliveInterval and ServerAliveInterval did nothing. The solution is to turn on TCPKeepAlive options in client ssh:
in
Hope this will help
For the client, edit your
~/.ssh/config
(or/etc/ssh/ssh_config
) file as follow:For the server, edit your
/etc/ssh/sshd_config
as:If you want ssh client to exit (timeout) automatically after 10 minutes (600 seconds).
See also: What do the options
ServerAliveInterval
andClientAliveInterval
in sshd_config do, precisely?I absolutely love Mosh. I frequently ssh into a server, close my laptop and go to a cafe, open it up and carry on as if nothing changed.
For me, I was getting
Write failed: Broken pipe
even when I was actively typing in vim or at the shell prompt. I couldn't browse the internet locally either for awhile either. (I was connecting remotely to Ubuntu using Terminal.)Others in my network stream a lot of video from Netflix and other places. I can't prove it, but I suspect its an ISP or router issue. For example, Verizon and Netflix are pointing fingers at each other for their customer's network problems.
If you've got a dial-up connection and are streaming video or music with a simultaneous SSH or telnet connection, it's inevitable at some point you'll get a broken pipe message. Upgrading my ISPs broadband package seemed to make my broken connection less frequent.
I posted my answer here, as it was not a Ubuntu VM.
https://unix.stackexchange.com/questions/259225/packet-write-wait-broken-pipe-even-leaving-top-running
I have a script on the remote server that never seems to fails, regardless of the SSH configuration client or server.
Save it to some dummy.sh file and quickly run it before you minimize the window or move away from it. It will keep printing the current time stamp on the server and keeps your connection alive as long as the connection is not dropped by any other reason. When you get back to that terminal, just hit CTRL+C and keep working.
You can add these args every time you invoke ssh:
-o ServerAliveInterval=15 -o ServerAliveCountMax=3
You don't have to edit /etc/ssh/*config files if you do this.
You can create an bash alias or function or script to make this easy.
E.g. these bash functions, you can add into your .bashrc, do_ssh is used manually to turn on keepalives. do_ssh_pty is used within scripts to set pty and avoid prompts.
Now
do_ssh user@host
can be used ordo_ssh user@host <args> <command>
and keepalives will be active.