How to Work with SSH Timeout

preview_player
Показать описание
In this easy step by step tutorial, you will learn how to setup a timeout for SSH session.

When you log in to an SSH server using an SSH client, there is no time limit set in the SSH configuration file, which enables the session to be logged in for the maximum amount of time. We can set such a limit, so that the session expires after a specific period of time. This tutorial will show you how to setup a timeout value to an SSH session.

Step#1: Opening Configuration file

For the purpose of this tutorial, we are going to use the Ubuntu environment and will add the commands for SSH timeout in the configuration file.
First of all, let's open up the "sshd_config" file in the graphical editor:

Step#2: Editing Configurations

Next, let's insert the "ClientAliveInterval" command over here and specify the number of seconds we want to set for the timeout. Let's specify 25 seconds for now and just beneath that, let's add a command that would allow the session to be terminated after the 25 seconds in case of no activity. For that, we will use the "ClientAliveCountMax" command and specify the value to zero.
With that done, save the file.

Step#3: Implementing the Configurations

Finally, restart the host's services by typing "service ssh restart" in the terminal.
Now let's login to the server once more and wait for 25 seconds to see whether the session times out or not.
And there you have it. The session has been terminated automatically after 25 seconds, which means that the SSH timeout feature has been enabled in the configuration file successfully.
Рекомендации по теме
Комментарии
Автор

Thank you man, you saved my test in linux

kanlolghany
Автор

I am working with SONiC which is built on top of Debian and I have only found two methods that work to execute the ssh timeout. The method presented on the video will not work on SONiC unless if the client machine has the timeout configuration as well; I found this to be the case even though the server file (/etc/ssh/sshd_config) had ClientAliveInterval 30 and ClientAliveCountMax 0; on the client file (/etc/ssh/ssh_config) the settings were ServerAliveInterval 30 and ServerAliveCountMax 0. What I found was that the only way the timeout would execute was by having the same config on both machines. A workaround I found was editing the /etc/profile file with TMOUT = 30, readonly TMOUT, export TMOUT; this edit executes a timeout on 30 seconds regardless of what is going on and on all connections including console connections (something that might not be desired).

emmanuelnunez
Автор

code.py:
import time
i = input()
time.sleep(int(i))

input.txt:
20

shell:
$ timeout 2s cat input.txt | python3 code.py

but it terminates after 20 seconds not 2 seconds.
please help!

raghavgupta
Автор

This feature no longer valid on ubuntu 20 and up

singdo