Operations 3 min read

Terminate All or Specific Linux User Sessions with a Simple Shell Script

This guide shows how to create and run a Bash script that gathers active TTYs using the w, awk, and tail commands and then force‑kills those sessions with pkill, plus a variant for terminating a single user's sessions.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Terminate All or Specific Linux User Sessions with a Simple Shell Script

Terminate All User Sessions

This section provides a Bash script ( kill-user-sessions.sh) that enumerates every logged‑in user's TTY and terminates each session using pkill -9 -t <tty>. The script works by piping the output of w through awk to skip the header line and print the second column (the terminal name), then using tail -n +2 to ignore the first data line, finally looping over each TTY and sending a SIGKILL.

#!/bin/bash
usession=$(w | awk '{if (NR!=1) {print $2}}' | tail -n +2)
for i in $usession; do
  pkill -9 -t $i
done

After creating the script, make it executable with: chmod +x kill-user-sessions.sh The script relies on three standard utilities: w – lists logged‑in users and their terminals. awk – filters out the header line and extracts the terminal column. tail – skips the first data line to avoid the current shell.

Terminate a Specific User's Sessions

A second script ( kill-specific-user-sessions.sh) accepts a username as an argument and terminates only that user's sessions. It calls w $1 to list sessions for the supplied user, then follows the same processing pipeline as the all‑users script.

#!/bin/bash
usession=$(w $1 | awk '{if (NR!=1) {print $2}}' | tail -n +2)
for i in $usession; do
  pkill -9 -t $i
done

Run the script with the target username, for example: ./kill-specific-user-sessions.sh user01 Verify the termination by executing w again; the specified user's TTY entries should no longer appear.

Summary

The article demonstrates how to use a short Bash script to automatically locate and kill active user sessions on a Linux system, both globally and for an individual user, by leveraging standard command‑line tools and the pkill utility.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxpkillUser session
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.