How to auto logout(timeout) a normal user and root user in Linux?

Below steps are validated on Red Hat Enterprise Linux 7.

Auto-logout functionality is mostly used to logout a user if the session is idle for a while. Using this article you should be able to set different timeout values for individual user in your Linux setup.

To only disable a root user, create a log-out.sh script inside /etc/profile.d (The script name can be anything, log-out-sh is just a demo script name)
# touch /etc/profile.d/log-out.sh

# cd /etc/profile.d

# vi log-out.sh
#!/bin/bash
# Log out in 2 minutes if the session is idle
export TMOUT=120
readonly TMOUT

Here we are setting the variable as readonly. The TMOUT variable terminates the shell if there is no activity for the specified number of seconds (for us 120). You can change the limit as per your requirement.

Log out and login using a new session to validate the new config
[root@localhost ~]# echo $TMOUT
120
So above new value is set properly, and after waiting for 2 minutes the shell logs me out automatically
[root@localhost ~]# timed out waiting for input: auto-logout
[deepak@localhost ~]$
NOTE: The above values can also be directly added to /etc/profile but that is not a recommended procedure.

How to add different auto logout time value to individual users?

I want my root user sessions to logout automatically after 2 minutes if idle and my normal user "deepak" to logout after 1 minute for idle sessions.

To achieve this modify your log-out.sh script as below
#!/bin/bash
# Log out in 2 minutes if the session is idle
if [ `id -nu` == "
root" ];then
   
export TMOUT=120
   readonly TMOUT
elif [ `id -nu` == "deepak" ];then
   export TMOUT=60
   readonly TMOUT
fi

Log out and re-login to validate your new changes
[deepak@localhost ~]$ echo $TMOUT
60
[deepak@localhost ~]$ su -
Password:
Last login: Sun Sep  3 16:51:40 IST 2017 on pts/1
[root@localhost ~]# echo $TMOUT
120
As you see I have different values for different user. You can modify your log-out.sh script similarly to add more users with their respective timeout values

I hope the article was useful.

Leave a Comment