Linux / UNIX Automatically Logout BASH / TCSH / SSH Users After a Period of Inactivity

My development and testing webserver is used by over 100s of users. These users login from Windows XP, Linux, Mac OS X system via ssh/putty client. How do I set or automatically log users out after a period of inactivity under RHEL/Fedora/CentOS Linux server to improve server security and save some resources?

You can configure Linux or Unix-like system to automatically log users out after a period of inactivity. [donotprint][/donotprint]Simply login as the root user and create a file called /etc/profile.d/autologout.sh, enter:

# vi /etc/profile.d/autologout.sh

Append the following code:

TMOUT=300
readonly TMOUT
export TMOUT

TMOUT=300
readonly TMOUT
export TMOUT

Save and close the file. Set permissions:
# chmod +x /etc/profile.d/autologout.sh
Above script will implement a 5 minute idle time-out for the default /bin/bash shell.

A note about tcsh/csh user

You can also create tcsh version as follows:
# vi /etc/profile.d/autologout.csh
Append the following code:

set -r autologout 5

set -r autologout 5

Save and close the file. Set permissions, enter:
# chmod +x /etc/profile.d/autologout.csh

Dealing with ssh clients

SSH allows administrators to set an idle timeout interval. After this interval has passed, the idle user will be automatically logged out. Open /etc/ssh/sshd config file, enter:
# vi /etc/ssh/sshd_config
Find ClientAliveInterval and set to 300 (5 minutes) as follows:

ClientAliveInterval 300
ClientAliveCountMax 0

Save and close the file. Restart sshd service:
# service sshd restart
OR use the following command on RHEL/CenOS 7.x or Fedora Linux server:
# systemctl restart sshd

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.