How to Terminate Stuck or Unwanted User Sessions in Linux

Need help with stuck user sessions on Linux? Here's how to terminate them and keep your system running smoothly and securely.

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:

Get information about logged-in users on Linux.
Get information about logged-in users on Linux.

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
Terminating a user session in Linux by TTY/PTS name.
Terminating a user session in Linux by TTY/PTS name.

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
Terminating a user session in Linux by process ID.
Terminating a user session in Linux by process ID.

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%