One of the most annoying things in the world is when you are working on a server through ssh and get disconnected.
Today you will learn how to quickly fix that.
Fixing it on any Linux Distro
To fix it on any Linux Distro* (as far as I know this method doesn't depends on any specific features) you can go to your home folder, and create a file called "config" inside the hidden folder ".ssh":
After that, you can just copy and paste the following code:
Host * ServerAliveInterval 120 ServerAliveCountMax 2
Fixing it on Windows and Mac
If you are using windows, you can search for this folder using PowerShell
After you find that folder, you can create a "config" file as well with the same content.
On Mac that should be just like Linux. You should be able to create the file at
How that is even working?
If you want to understand this code, this is how it is working:
- Host: This is defining that these settings will apply to ANY host. And what is a host? It's a IP/domain. Host * means "for any ssh connection".
- ServerAliveInterval: This is defining the time interval to send a "keepalive" pakacge to the server. 120 seconds = 2 minutes, so every 2 minutes a package will be sent to check if the connection is still alive.
- ServerAliveCountMax: This is defining the amount of "keepalive" packages that can be sent before the connection is considered dead. In the code that I provided, we are using the number "2", so if after 2 tries, the connection will be considered dead and will be closed.
So in short, we are saying "Hey, please send a package to the server every 2 minutes to say that we are alive AND kill the ssh connection if trying to send the package to the server fails twice"
When the package is sent to the server, that ensures to the server that we are not dead and we want to stay connected. In this settings, a package will be send every 2 minutes and 2 tries will be made until the ssh connection is considered dead. That way you can lose internet conneciton for about 4 minutes and you would probably still be alive if the server didn't cut you.
We can't control how the ssh server behaves without root access, but we can surely connect our ssh client to "last more"
If you enjoyed this article, you can share it your friends or subscribe to The Self Hosting Art to keep us motivated. Thank you for reading :)