Automatic logout of inactive users after login timeout under Linux

  • 2021-07-01 08:43:42
  • OfStack

Method 1: By modifying the. bashrc or.bash_profile file

This is done by modifying the. bashrc or. bash_profile file in the home directory. Select one of these two files and add the following line at the end. The specific operation is as follows:


[GeekDevOps@GeekDevOps /]$ cd ~
[GeekDevOps@GeekDevOps ~]$ echo "TMOUT=90">>.bashrc 
[GeekDevOps@GeekDevOps ~]$ source .bashrc

In the above code, we selected a.bashrc file. If you selected a.bash_profile, it is also a kind of operation flow. Above, we set the user's automatic logout time to 90 seconds, and the screen will display as follows after 90 seconds:


[GeekDevOps@GeekDevOps ~]$  Timeout waiting for input: automatic logout 
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(10.1.1.21:22) at 16:38:48.

Method 2: By modifying the configuration file of ssh

ssh is a common tool in our Linux system. By modifying the configuration file of ssh, /etc/ssh/sshd_config We can also realize the timeout automatic logout function, as follows:


[root@GeekDevOps ~]# vim /etc/ssh/sshd_config

Find the following two lines:


#ClientAliveInterval 90
#ClientAliveCountMax 3

Remove the comment and modify the following numbers:


ClientAliveInterval 60
ClientAliveCountMax 5

After saving and exiting, restart the ssh service:


[root@GeekDevOps ~]# service sshd restart

This method is a 60-second login timeout for all users except root and logout automatically. Line 1 indicates detection once every 90 seconds, and Line 2 indicates disconnection when inactivity is detected five times.

Method 3: By modifying the configuration file of ssh

In the/etc/profile file, add the TMOUT field, then source/etc/profile.


TMOUT=300
export TOMOUT

Method 4: Create a script

Log in as an root user and create a new file named autologout. sh.


vim /etc/profile.d/autologout.sh

Add the following:


TMOUT=100
readonly TMOUT
export TMOUT

Save and exit the file and add executable permissions to it:


chmod +x /etc/profile.d/autologout.sh

Now, log out or restart the system. Inactive users will automatically log out after 100 seconds. Ordinary users can't modify the configuration even if they want to keep the session connection, and they will be forced to quit after 100 seconds.

Summarize


Related articles: