On our Debian servers, I want to ensure that any SSH connections time out and disconnect after 8 hours. This was recommended by our security consultant.
I executed these steps:
# Log in as root then:
sudo vi /etc/ssh/sshd_config
# Use / to search for and change:
ClientAliveInterval=3600
ClientAliveCountMax=8
# Second parameter is number of hours before connection times out and drops.
# Then test config:
sudo sshd -t
# Restart
sudo systemctl restart sshd
However, the tests failed. When I temporarily set ClientAliveInterval=120
and ClientAliveCountMax=2
and restarted the sshd daemon, I would expect the connection to drop after 4 minutes of inactivity. However, it did not.
Any ideas?
OpenSSH
sshd_config
Correct configuration keywords since OpenSSH 9.2 (Debian 12)
The OpenSSH 9.2 (Feb 2, 2023) introduced new features for inactive sessions.
For the eight hours in this question, e.g.,
Where:
*=8h
monitors all channels separately. If you want activity on any channel to reset the timeout, use the special keywordglobal=8h
.UnusedConnectionTimeout
is set to one minute in order to terminate the connection shortly after the last channel has been closed.For OpenSSH < 9.2 that has to be done by other means.
The used configuration keywords are for unresponsive sessions
The
ClientAliveInterval
does not monitor for inactivity but whether the client is still responsive.The documentation for
ClientAliveCountMax
may help in understanding this:📜 A historical note: Before OpenSSH 8.2 (Feb 14, 2020) the
ClientAliveCountMax
worked differently with0
, having the side effect that it could have been used for terminating idle connections. From the changelog:⚠️ Yet, this misleading instruction has been repeated all over the Internet:
Of these, only Gite had the
ClientAliveCountMax=0
that could have worked before 2020, but does this count? After all, the article was likely written in 2021... Makes me wonder does anyone read the documentation or are they busy copy-pasting other's blog posts. 🤦♂️In-depth discussion on alternatives
Scripting – be careful you detect inactivity correctly
Hayden James gives an example for Automating the Process with a script. However, using that script is dangerous as it could kill SSH sessions that are in active use. The script gets the "idle time" from
ps -eo pid,etimes,comm
where theetimes
does not show the idle time of the process but the total time it has been running. From ps(1):Satish Kumar suggests using
who
for list of active users and the fifth column ofw -h
for detecting inactivity. However, this script also has some flaws:2.00s
,7:51m
or5days
. The[[ "$idle" -gt "1800" ]];
does not parse that correctly.screen
,tmux
) and other processes that are intended to live outside an active SSH session.The detection of the inactivity should be way more precise, as well as the selection of the process to be killed.
This approach could work:
who
.stat
to get the last access time of the TTY device (see answer from Celada). That can be directly converted to age in seconds:pgrep
to findsshd
processes for the TTYs, orpkill
to kill them.All put together (more complete
find-inactive-ssh-sessions.sh
in oh2fih/Misc-Scripts):Built-in features in shells
As Vivek Gite mentions, shells have features for automatically logging the user out after a period of inactivity. These might work if there is no need to enforce the logout, as the user is able to change these settings.
TMOUT
environment variable forbash
,zsh
&ksh
.autologout
setting intcsh
&csh
.If you configure these through, e.g.,
/etc/profile.d/
, they affect all sessions. However, you could use features insshd_config
to configure them, too:SetEnv
&ForceCommand
. E.g.,Where does the recommendation come from?
The advice is likely based on, e.g., NIST 800-171 Revision 2:
You should decide which one is more important for you: preventing someone from using the connection without permission or the reliability of management connections. Could the unauthorized use be prevented by other means? E.g., could the SSH connections be limited only to company computers, and could the company computers be forced to be locked on inactivity?