Sometimes we might need to kill a user session in Linux based on requests, which can be done by sending SIGNALS with kill command.
We need to send one of the following signals based on our requirement, to kill a process in Linux.
- SIGTERM
- SIGKILL
What is SIGTERM?
The ‘SIGTERM’ is a signal used for graceful termination of a process, which can be ignored or blocked sometimes in case a process is in busy state (i.e. waiting for disk I/O).
However by default, a kill command sends the SIGTERM signal along with the request.
What is SIGKILL?
The ‘SIGKILL’ signal is used for instant termination of process, along with its child processes.
This is a brutal way to kill a process & can be opted as a final option.
Use ‘SIGKILL’ or ‘9’ with the kill command to terminate a process.
For instance:
- All users need to be logged out of system before we trigger a monthly job
- To kill multiple ssh sessions running in system
- One user session went unresponsive & needs to be killed
This article further covers the list of commands that can be used to perform this operation.
- Suggested Read: How to kill any inactive or idle ssh session in Linux
To view list of active ssh sessions, use ‘w’ command.
[root@vps1001 ~]# 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
magesh pts/0 192.168.1.101 19:47 4:45m 0.04s 0.00s sh /opt/scripts/disk-usage.sh
renush pts/1 192.168.1.102 20:35 3:54m 2:23 0.00s sh bash
thanis 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
1) Kill user session using pkill command
TTY session can be used to kill a specific user ssh session & to identify tty session, please use ‘w’ command.
For example:
To kill the user “magi”, connected to the server with ‘pts/2’, use the command below:
# pkill -9 -t pts/2
To understand the difference between tty and pts, refer the details below:
- TTY (Teletypewriter): is directly connected to the system as a keyboard/mouse or a serial connection to the device (i.e. the console on your system).
- PTS (pseudo terminal slave): is a terminal device, which is emulated by another program (i.e. ssh session to your system).
2) Terminating user session, using Killall command
User session can be killed using the ‘killall’ command.
For example, to terminate user “magesh”, use “-u” switch, as shown below:
# killall -u magesh
3) How to terminate user session, using Kill command
This process needs additional efforts to terminate a user, using kill command when compared to previous two steps.
Before we proceed to kill the user session, we must find the tty session from ‘w’ command, as shown below:
[root@vps1001 ~]# 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
magesh pts/0 192.168.1.101 19:47 4:45m 0.04s 0.00s sh /opt/scripts/disk-usage.sh
renush pts/1 192.168.1.102 20:35 3:54m 2:23 0.00s sh bash
thanis 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
Once we identify the tty session, locate the PID of corresponding tty session using ps command:
ps -ft [tty]
[root@vps1001 ~]# ps -ft pts/4
UID PID PPID C STIME TTY TIME CMD
root 155183 155092 0 00:34 pts/4 00:00:00 -bash
root 163015 155183 0 00:35 pts/4 00:00:00 ps -ft pts/4
Now, terminate the process id of the user session:
kill -9 PID
# kill -9 155183
Conclusion:
This article covers multiple ways to terminate user sessions. We hope this topic was apt to your requirement.
Please fill in our comment section with your valuable feedback & suggestions.