How to avoid timeouts while you are logged on SSH

Are you being logged off while away on SSH? Here is how to fix it.

a year ago   •   2 min read

By aquasp
Table of contents


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":

mkdir ~/.ssh
nano ~/.ssh/config

Creating the ssh folder if it doesn't exists and opening the nano editor inside the .ssh folder

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

cd C:\Users\YOURNAME\.ssh

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 :)

You can also help with Monero, Litecoin, Bitcoin or Nano: Monero:837X2SppmrrPkBbpsy9HQU1RFxKhsBcn6GdQv2wR5wGoiw8ctfh6Rt36xaszveZHysYA5KSDBr51y5YuQ3YCV23sJS9nhqW BTC:bc1qrvgz7dzzlfllulakw87vzvtf7s2u8t0sxpjehr Litecoin:ltc1qycz6ssg6xjxttuld6l6ulzqdr3y70rm8wv2g9p Nano:nano_1jmd6dg4dbem7f3wrojr7g45ioe6eb5et3iq11f8urfxe8qausxipup8bhua

Spread the word

Keep reading