Non-active users in Linux exit automatically.

Source: Internet
Author: User
Tags exit in

Non-active users in Linux exit automatically.

Let's imagine this scenario. One of your servers is often accessed by multiple users in different systems on the network. Some users may forget to log out of the session to keep the session in connection. We all know how dangerous it is to leave a connected user session. Some users may intentionally do something that damages the system. As a system administrator, you will go to every system to check whether the user has logged out? In fact, this is completely unnecessary. In addition, if there are hundreds of machines in the network, this is too time-consuming. However, you can enable the user to log out automatically when the user is not active for a certain period of time on the local machine or SSH session. This tutorial will teach you how to implement this in Unix-like systems. It's not hard at all. Follow me.

In Linux, the system automatically logs out of non-active users after a period of time.

There are three implementation methods. Let's look at the first method first.

Method 1:

Edit ~ /. Bashrc or ~ /. Bash_profile file:

$ vi ~/.bashrc

Or,

$ vi ~/.bash_profile

Add the following lines to the table:

TMOUT=100

This will allow the user to automatically log out after 100 seconds of stopping the action. You can define this value as needed. Save and close the file.

Run the following command to make the change take effect:

$ source ~/.bashrc

Or,

$ source ~/.bash_profile

Now, the session is idle for 100 seconds. After 100 seconds of inactivity, you will see the following information and the user will automatically exit the session.

timed out waiting for input: auto-logoutConnection to 192.168.43.2 closed.

This setting can be easily modified by users. Because ,~ /. The bashrc file is owned by the user.

To modify or delete the timeout settings, you only need to delete the row added above and then execute source ~ /. Bashrc command to make the modification take effect.

In addition, you can run the following command to disable Timeout:

$ export TMOUT=0

Or,

$ unset TMOUT

If you want to prevent users from modifying this setting, use the following method instead.

Method 2:

Log on as the root user.

Create a new file named autologout. sh.

# vi /etc/profile.d/autologout.sh

Add the following content:

TMOUT=100readonly TMOUTexport TMOUT

Save and exit the file.

Add executable permissions for it:

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

Now, log out or restart the system. Inactive users will log out in 100 seconds. Normal users cannot modify the configuration even if they want to keep the session connection. They will forcibly exit in 100 seconds.

These two methods are applicable to both local and remote sessions (that is, users logging on locally and users logging on through SSH on the remote system ). Next let's take a look at how to automatically log out of an idle SSH session without automatically logging out of the local session.

Method 3:

In this way, the SSH session user is automatically logged out after a period of inactivity.

Edit the/etc/ssh/sshd_config file:

$ sudo vi /etc/ssh/sshd_config

Add/modify the following line:

ClientAliveInterval 100ClientAliveCountMax 0

Save and exit the file. Restart the sshd service to make the change take effect.

$ sudo systemctl restart sshd

Now, log on to the remote system through ssh. After 100 seconds, the ssh session will be automatically closed. You will also see the following message:

$ Connection to 192.168.43.2 closed by remote host.Connection to 192.168.43.2 closed.

Now, anyone logging on to the system via SSH from a remote system will automatically log out after 100 seconds of inactivity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.