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 script inside /etc/profile.d (The script name can be anything, log-out-sh is just a demo script name)
# touch /etc/profile.d/

# cd /etc/profile.d

# vi
# 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
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 script as below
# 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

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

I hope the article was useful.

2 thoughts on “How to auto logout(timeout) a normal user and root user in Linux?”

  1. How about taking this to the next level ?
    Instead of just closing my cmd line session, have it totally log me off after a time out.
    and not using some existing OS setting ?


Leave a Comment