Linux logged out a user automatically

Q. We have a few users who frequently forget to log out and I’d like to force them or automatically log out a user in case of inactivity detected. How do I force user to be logged out automatically?

ADVERTISEMENTS

A. BASH provides TMOUT variable. It is necessary for security reasons to have an inactive user logged out after a period of inactivity from Linux / UNIX ssh / telnet session. This can be accomplished by setting an environment variable TMOUT.

Automatically log out a user

Add the TMOUT variable to your /etc/bashrc file:
# vi /etc/bashrc
Set TMOUT to 300 seconds (5 minuets):
TMOUT=300
Save an close the file. Above config would automatically logout users after 300 seconds of inactivity. Please note that this hack only works with run level 2, 3 i.e. it will not work with GUI sessions.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
4 comments… add one
  • Anil Nov 10, 2007 @ 14:35

    Hi

    Do i need to add this to every bashrc in user’s home dir to work ???

    Thanks
    Anil

  • HarryWWC Dec 5, 2007 @ 0:57

    Hullo Anil,
    if you read carefully, you are editting the “/etc/bashrc” – this is the *system* (global) BASH Resource Configuration file. Hence, editting this file will make it affect *all* (subsequent) logons.
    You could (in theory) in your own .bashrc modify the value to something else (e.g. to 600seconds = 10mins) if you wished.
    hth,
    .h

  • Prasad Nov 12, 2009 @ 8:36

    Hi,

    As it is mentioned, this does not work for GUI based sessions. Can someone please let me know how can I kill the GUI based idle sessions?

    Prasad

  • Geff Feb 1, 2010 @ 15:08

    On an apparent contrary, how do you rcover from a situation where linux automatically logs a user out immediately after logging in onto the linux console?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.