For Linux users, encountering stuck or unwanted user sessions can be a frustrating experience. At the same time, those sessions can expose your system to potential risks, leaving it vulnerable to unauthorized access or data breaches.
So, whether caused by misbehaving applications, system glitches, or user errors, these lingering sessions can hinder productivity and compromise system security and performance.
But fear not; this article aims to equip you with the knowledge and tools to regain control and maintain a smoothly running Linux system. By learning the proper methods to handle these situations, you can ensure a more secure computing environment and safeguard your valuable data.
Before we move on, however, we need to clarify the meaning of two terms, TTY and PTS, that are directly involved in terminating a user session in Linux.
What Are TTY and PTS in Linux?
In Linux, both โPTSโ and โTTYโ refer to different terminal devices used to communicate between the user and the operating system. They serve as interfaces to input commands and receive output from the system.
TTY (Teletype)
“TTY” originally stood for Teletype, a typewriter-like device used for input and output in the early days of computing.
In modern Linux systems, a TTY represents a physical or virtual console where users can interact with the system directly.
PTS (Pseudo-Terminal Slave)
PTS stands for Pseudo-Terminal Slave. It is a virtual terminal that emulates a hardware terminal but is not directly connected to any physical device.
Instead, the system creates and manages it to support terminal multiplexing, remote logins, and various interactive applications.
For example, you typically use a pseudo-terminal when you use terminal emulator apps, such as GNOMEโs Terminal or KDEโs Konsole, to get shell access to your Linux system.
These emulators act as the “master” side of the pseudo-terminal, while the shell or the process running within the terminal acts as the “slave” side.
When you open multiple terminal windows or tabs, each corresponds to a separate PTS. For example, if you open three terminal windows, they might be identified as /dev/pts/0, /dev/pts/1, and /dev/pts/2.
In summary, both TTY and PTS are terminal devices in Linux that allow users to interact with the operating system. TTY represents physical or virtual consoles, while PTS is used for terminal emulators and provides additional functionality for multiplexing and remote logins.
With the clarifications thus made, we can now move on to the main topic.
How to Terminate Stuck/Unwanted User Sessions in Linux
You can accomplish this in two ways, so weโll look at each below.
Terminate User Session by TTY
We will use the w
command to get information about logged-in users on our Linux system. It displays information about currently logged-in users and their activities.
When you run the w
command, it provides a summary of the following details for each user:
- USER: The username of the logged-in user.
- TTY: The terminal name or device associated with the user’s session (e.g., /dev/tty1, pts/0).
- FROM: The remote host or IP address from where the user logged in. If the user is logged in locally, it will display the TTY’s name or “–” symbol.
- LOGIN@: The date and time when the user logged in.
- IDLE: The duration of inactivity for the userโs session. If the user actively uses the terminal, it will show โoldโ instead.
- JCPU: The total CPU time used by all processes associated with the user’s session.
- PCPU: The CPU time used by the user’s current process.
- WHAT: The command executed by the user or the process associated with the terminal.
Here’s an example output of the w
command:
As you can see from the example’s output above, there are three logged users – two locally and one remotely.
To terminate the user session of the remotely logged-in user “linuxiac,” we will use the pkill
command in Linux with the option “-KILL,” which means that the Linux process must be terminated immediately (not gracefully). Use the “-t” flag to specify the name of the TTY.
pkill -KILL -t pts/1
That’s all. As can be seen from re-checking with the w
command, the user’s session to our system is terminated immediately.
Terminate User Session by Process ID
The second approach we will show you uses terminating a user session by process ID. To do this, we execute the w
command again to get a list of logged-in users along with their associated TTY/PTS.
Then, once we’ve identified the TTY/PTS session, use the ps
command with “-ft” parameters to find its PID:
ps -ft [TTY/PTS]
Code language: CSS (css)
Finally, use the kill
command with “-9” (unconditionally terminate a process) switch passing the process ID. For example:
kill -9 4374
As a result, the user session is terminated instantly.
Conclusion
Knowing how to terminate stuck or unwanted user sessions is essential in the realm of Linux system administration. Throughout this article, we explored two effective methods to accomplish this task: TTY/PTS or process ID.
The TTY/PTS approach provides a straightforward means to terminate user sessions. On the other hand, terminating sessions by process ID offers a more precise and direct method; which of the two approaches to use? The decision is entirely up to you.
Remember, exercising caution is paramount whether you choose the TTY/PTS approach or the process ID method. Ensure you terminate the correct user session or process to prevent accidental data loss or unintended consequences.
Need more details? Check the pkill
and the kill
commands man pages.