Skip to content

How to kick out inactive user on Linux

homepage-banner

Introduction

As system administrators, it is important to ensure only authorized users have access to the system. One way to do this is by kicking out inactive users. In this blog post, we will discuss how to kick out inactive users in Linux.

First

First, use w to check all the login user

 09:19:58 up 5 days,  2:53,  2 users,  load average: 1.00, 1.01, 1.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/1    192.168.1.100  07:52   56:32   0.06s  0.06s -bash
root     pts/2    192.168.1.101  09:19    2.00s  0.03s  0.00s w

Then

Confirm that the pts/1 user is unresponsive and kick it out with the following command

pkill -kill -t pts/1

Automating the Process

Manually checking and terminating inactive users can be a tedious and time-consuming task. Fortunately, you can automate this process using a script that runs periodically. The script can check the list of users and their activity status and terminate inactive sessions automatically. You can use the last command to get a list of all logged-in users and their login history. By comparing this with the output of the w command, you can determine which users are inactive and terminate their sessions using the pkill command. You can schedule this script to run every few minutes using a cron job.

Conclusion

Kicking out inactive users is an essential part of managing a multi-user Linux system. By terminating inactive sessions, you can avoid security breaches, resource wastage, and other issues. In this blog post, we discussed how to terminate inactive sessions manually and how to automate the process using a script. By following these steps, you can keep your Linux system secure and efficient.

Leave your message