How to avoid timeouts while you are logged on SSH

Introduction

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

~/.ssh/config

How that is even working?

If you want to understand this code, this is how it is working:

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 with your friends or subscribe to The Self Hosting Art. Thank you for reading :)

You can also help with XMR(Monero):

8AWKRGyqQ6fdaLwGVAdVTbEP6ZttSXwcYWQWy7gnq6zceTngtJgaAr82Hxr2FY5bkCUJVerccH9XNFX1qWnZxuGYTU5bJ34