1. Overview
The Linux system allows multiple users to access it. However, users might misuse their privileges, forcing us as the system administrators to log them out forcefully. In detail, we can achieve this by killing all the processes associated with the user in question.
In this tutorial, we’ll explore all tools to allow force logout of the user in Linux.
2. Using the pkill Command
First, let’s launch our terminal and start typing:
$ who
peter tty2 2022-08-09 19:16 (tty2)
Here, the who command lists all the users currently logged into the system. As a result, we can see the currently active user peter.
Next, let’s have a look at the processes associated with our user:
$ ps -U peter
PID TTY TIME CMD
2145 ? 00:00:01 systemd
2146 ? 00:00:00 (sd-pam)
2152 ? 00:00:00 pipewire
...
The ps (process status) command displays the processes owned by peter. In addition, this output is limited to the current terminal session.
Great, now let’s kill all these processes by utilizing the pkill command:
$ sudo pkill -9 -u peter
The pkill command above guarantees an easy way to terminate all the displayed processes. Additionally, the -u option ensures that pkill matches a process owned by the specific user peter.
On the other hand, what if we prefer to terminate a specific process? Well, we can achieve this by using the process property. For example, we can type in the PID (process ID):
$ sudo pkill -9 -s 2145
When we run the command above we kill the process with the ID 2145.
3. Using the killall Command
The killall command works the same pkill since it also terminates all the processes owned by a specific user.
$ sudo killall -u peter
Here, peter’s processes are forcefully stopped.
4. Conclusion
In this article, we have simplified how to log out a user forcefully from our Linx system. This might come in handy if there is a known or an unknown user whose activities are malicious. An important fact to note is that we should be very careful which user’s processes we are terminating. For instance, we might crash our system if we tamper with the wrong process.