how to kill user sessions in Linux

How to Kill User Session in Linux

Sometimes you may need to kill one or more user session in Linux. There are several ways to kill user sessions in Linux. You can always kill your own session. But in order to be able to kill other user sessions, you need to have root or sudo privileges. In this article, we will learn how to kill user session in Linux.


How to Kill User Session in Linux

Every user session is a process and in order to kill a user session, you need to find the process responsible for it, and kill it with KILL, PKILL, SIGTERM or SIGKILL command.

SIGTERM is used for graceful termination of process. However, it may be blocked if the process is busy waiting for I/O input. When you run KILL command, it sends SIGTERM command under the hood.

SIGKILL will immediately terminate process along with its child processes. It will terminate the process at any cost, so it is a brute-force way to kill processes.

First we need to find out all user sessions running on your system, using w command.

# w
00:34:21 up 48 days, 23:38, 4 users, load average: 0.79, 0.58, 0.56
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
john pts/0 192.168.1.101 19:47 4:45m 0.04s 0.00s sh /opt/scripts/disk-usage.sh
dave pts/1 192.168.1.102 20:35 3:54m 2:23 0.00s sh bash
jim pts/2 192.168.1.103 00:27 5.00s 0.08s 0.04s ssh
root pts/4 192.168.1.104 00:34 1.00s 0.02s 0.01s w


Kill Session Using TTY

You can kill session process by referring to its TTY column’s value above. Every user session will have TTY value starting with pts/. Here is the command to kill process with TTY value as PTS/1 using pkill command.

# pkill -9 -t pts/1


Terminate Session using Killall command

You can also terminate all sessions of a specific user using killall command, followed by -u option. Here is the command to terminate all sessions pertaining to user John.

# killall -u john


Terminate Session using Kill command

You can also use Kill command to terminate sessions but it is a roundabout process since kill command requires PID of the process to be killed. So first we need to find PID of process running user session. Here is the command to get PID of user session using its TTY value.

# ps -ft [tty]

Here is an example to get PID of pts/2. It is the second column in the following command’s output.

# ps -ft pts/2
ps -ft pts/2
UID PID PPID C STIME TTY TIME CMD
root 12183 10092 0 00:34 pts/2 00:00:00 -bash
root 12015 10183 0 00:35 pts/2 00:00:00 ps -ft pts/2

Once you have found out the PID of process, issue kill command to kill it.

# kill -9 12183

In this article, we have seen several ways to kill user sessions in Linux. You can kill it referring to its TTY value, or username or PID, depending on your requirement. But in all cases, you need to have the right privileges to delete user sessions, otherwise the above commands won’t work for you.

Also read:

Git Tags vs Branches
How to Sleep Function in Python
How to Fix Access Denied in MySQL Ubuntu
How to Remove NaN from Python List
Delete All Files But One in Linux

Leave a Reply

Your email address will not be published.