If you have your own Linux server, whether it's a home server or a VDS, you have to connect to it in many ways, and one of the most common is SSH. When working with the server via SSH, you may have noticed that the connection is disconnected after a certain time. Sometimes this time may not be enough and you need to increase the idle timeout.

Change SSH session timeout in Linux

To increase the connection timeout , you need to edit the configuration file on the server, which is located at:

/etc/ssh/sshd_config

To edit it, use any text editor. In my case, I'll use nano.

sudo nano /etc/ssh/sshd_config

Open the configuration file and find the following lines there:

  • ClientAliveInterval
  • ClientAliveCountMax

The first parameter is responsible for sending null packets to clients, and the second is responsible for closing the connection if the client has been inactive for the specified interval.

For example, we will set the first parameter ClientAliveInterval to 1200 seconds, the second parameter ClientAliveCountMax will leave the default value - 3.

1200 * 3 = 3600 seconds = 1 hour

You can set any value, depending on your needs.

Also, I don't recommend setting a long timeout in SSH without the need, as this can adversely affect security.

Don't forget to restart the ssh service after changing the settings.

sudo systemctl restart ssh

Conclusion

In such a simple way, we increased the waiting time by as much as an hour, as already mentioned, you can set any necessary time, but it must be set in seconds. If necessary, you can always change the set time back by commenting out these values by default. In any case, before making any changes to the configuration files on your server, I recommend making backups so that you have the opportunity to restore the source if something goes wrong.