Created
August 31, 2022 02:40
-
-
Save zeushammer/c4152aa18d7acbc13fbb7290ddb5bba7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment