In this tutorial, we intend to teach you How To Force a User To Logout in Linux.
You may need to forcefully log a user out of the system for many reasons. If you have physical access to the server, you could unplug the network connection and reboot the system. That might not always be possible, such as when working on remote servers via SSH.
To do this, you’ll need to kill processes owned by the user to terminate whatever session the user currently has.
Force User To Logout in Linux
In this guide, you will learn to Force a User To Logout in Linux by using the Linux commands. You must list currently logged-in users in the system. List all processes owned by the user you want to kick out of the Linux system. Kill the user’s terminal or other session processes. Alternatively, kill all processes owned by the user. Check if the user is still logged in.
Follow the steps below to see how it works.
Kick Out a User From Linux Server
First, you need to launch your terminal.
Then, use the following command to list currently logged-in users in your system:
You will get something similar to this in your output:
Output root pts/0 2022-08-28 02:11 (...) orca pts/1 2022-08-28 02:13 (...)
At this point, you can list all processes owned by the user you want to kick out of your Linux system:
ps -U orca
Output PID TTY TIME CMD 97482 pts/0 00:00:00 bash 97522 ? 00:00:00 systemd 97525 ? 00:00:00 (sd-pam) 97531 ? 00:00:00 sshd 97532 pts/1 00:00:00 bash 97559 pts/0 00:00:00 su .....
At this point, you can use the kill command to kill the user’s terminal or any session process you want to kill:
Note: bash is normally the process if the user is connected via SSH.
If you want to kill all the processes owned by your user, you can use the following command:
pkill -u orca
At this point, list currently logged-in users again:
In my case, the orca user is not logged in anymore:
Output root pts/0 2022-08-28 02:11 (...)
After a while, if you see that the user is still logged in, force process termination with the following command:
pkill -9 -u orca
That’s it. You are done.
For more information about Killing the processes, you can visit this guide How to Kill a Process in Linux.
At this point, you learn to Logout A User In Linux by using the Linux commands.
Hope you enjoy it.
Also, you may be interested in these articles:
How To Use RPM Command in Linux