Quantcast
Channel: GoLinuxHub
Viewing all articles
Browse latest Browse all 392

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

$
0
0
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.

Follow the below links for more tutorials

How to find the path of any command in Linux
How to configure a Clustered Samba share using ctdb in Red Hat Cluster
How to delete an iscsi-target from openfiler and Linux
How to perform a local ssh port forwarding in Linux
How to use yum locally without internet connection using cache?
What is umask and how to change the default value permanently?
Understanding Partition Scheme MBR vs GPT
How does a successful or failed login process works in Linux
How to find all the process accessing a file in Linux
How to exclude multiple directories from du command in Linux
How to configure autofs in Linux and what are its advantages?
How to resize software raid partition in Linux
How to configure Software RAID 1 mirroring in Linux
How to prevent a command from getting stored in history in Linux


Viewing all articles
Browse latest Browse all 392

Trending Articles