How to Kill TTY Sessions in Linux.

Learn how to identify active TTY sessions in Linux, find process IDs (PIDs), and safely terminate unwanted terminal processes using `w`, `ps`, `kill`, and `pkill`. A practical guide for Linux system administrators managing remote servers and SSH connections.

How to Kill TTY Sessions in Linux.
Image credit: Unknown. Please contact us if you are the owner.

Before we get started, let's take a quick look at the history of TTYs.

Back in the early days of computing, users interacted with computers through electromechanical terminals known as teleprinters or teletypewriters (TeleTYpewriter, or TTY). Although modern computers no longer rely on these devices, the term TTY remains widely used in Linux to describe terminal devices and interfaces.

When connecting to a Linux server through SSH, you will typically interact with a pseudo-terminal, commonly identified as pts/0, pts/1, or pts/2.

Sometimes, you may need to terminate a terminal session that is no longer needed. In this tutorial, we'll explore how to identify logged-in users, inspect their terminal processes, and terminate unwanted sessions using standard Linux commands.

Let's get started.

1. Check Active Users Logged Into the Server With w

Before terminating a terminal session, we need to identify which users are connected to the server and which terminals they are using.

We can do this using the w command.

$ w

Example output:

 14:57:48 up 10 days,  4:03,  3 users,  load average: 0.11, 0.11, 0.21
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
user1    pts/0    192.168.1.10     14:57    1.00s  0.4s   0.02s w
user2    pts/1    192.168.1.20     09:22    1.00s  0.6s   0.02s -zsh
user3    pts/2    192.168.1.30     06:19    1.00s  0.13s  0.02s -zsh

As you can see, three users are logged into the server through the pseudo-terminals pts/0, pts/1, and pts/2.

Let's take a closer look at the output:

  • USER: The username of the logged-in user.
  • TTY: The terminal associated with the user's session.
  • FROM: The remote host or IP address from which the user connected.
  • LOGIN@: The time the user logged in.
  • IDLE: How long the user has been inactive at the terminal.
  • JCPU: The CPU time used by processes associated with the terminal.
  • PCPU: The CPU time used by the process currently listed under WHAT.
  • WHAT: The command the user is currently running.

In this example, user2 and user3 are running Zsh shells, while user1 is executing the w command.

Important: An idle terminal is not necessarily an unused terminal. Before terminating a session, make sure the user isn't running an important command or maintaining an active connection.

Now that we know which terminals are connected, let's identify their associated processes.

2. Find the PID of a Terminal Process With ps

The ps (Process Status) command allows us to inspect running processes and identify their process IDs (PIDs).

Suppose we want to inspect the processes associated with pts/1.

Run the following command:

$ ps -ft pts/1

Example output:

UID       PID     PPID  C STIME TTY      TIME CMD
user2     567221  567220 0 15:02 pts/1   00:00:00 -zsh

Here, 567221 is the PID of the Zsh shell associated with pts/1.

Let's briefly explain the relevant columns:

  • PID: The process ID.
  • PPID: The parent process ID.
  • TTY: The terminal associated with the process.
  • CMD: The command being executed.

In this example, the process with PID 567221 is the user's shell.

We can now request that this process terminate.

Terminate a Process With kill

To send a termination signal to the process, execute:

$ kill 567221

By default, kill sends SIGTERM, which requests that the process terminate gracefully.

Unlike immediately forcing a process to stop, SIGTERM gives the application an opportunity to perform cleanup before exiting.

However, terminating the shell process does not necessarily terminate every process associated with that terminal.

Forcefully Terminate a Process

What if the process doesn't terminate after receiving SIGTERM?

If you've confirmed that the process is safe to terminate, you can send SIGKILL instead:

$ kill -9 567221

The -9 option sends SIGKILL, which forces the operating system to terminate the process. Unlike SIGTERM, this signal cannot be caught or ignored by the process.

Use SIGKILL as a last resort. It prevents the process from performing its normal cleanup, which can be problematic for applications handling files, transactions, or other resources.

Also, remember that the PID may no longer refer to the same process if enough time has passed. Verify the target before sending a signal.

3. Terminate Processes Associated With a TTY Using pkill

Instead of identifying and terminating a process by its PID, we can use the pkill command to send a signal to processes matching specific criteria.

The -t option matches processes associated with a particular terminal.

For example, to send SIGTERM to processes associated with pts/1, execute:

$ pkill -TERM -t pts/1

This sends a termination request to matching processes associated with that terminal.

If a process refuses to terminate and you have verified that it is safe to force termination, you can use:

$ pkill -KILL -t pts/1

This sends SIGKILL to matching processes associated with pts/1.

Be careful when using this command: It can affect multiple processes, not just the user's shell. Depending on the processes involved, terminating them may interrupt running commands or disrupt an active session.

If you're working on a production server, inspect the terminal and its processes before sending signals.

For example:

$ ps -t pts/1 -o pid,ppid,pgid,sid,stat,cmd

This displays the process ID, parent process ID, process group ID, session ID, process state, and command for processes associated with the terminal.

Use this information to understand what you're about to terminate.

Also note that signaling the processes associated with a terminal does not guarantee that every component of the login session will be closed. If your goal is to terminate an entire login session, you may need to identify and stop the session itself rather than simply signaling its terminal processes.

4. Useful Commands to Remember

Here are the commands covered in this tutorial.

Display logged-in users and their current activities:

$ w

Display the users currently logged in:

$ who

Display the terminal associated with your current shell:

$ tty

Inspect processes associated with a specific terminal:

$ ps -ft pts/1

Display additional process and session information:

$ ps -t pts/1 -o pid,ppid,pgid,sid,stat,cmd

Request graceful termination of a specific process:

$ kill 567221

Forcefully terminate a specific process when necessary:

$ kill -9 567221

Send SIGTERM to processes associated with a terminal:

$ pkill -TERM -t pts/1

Send SIGKILL to processes associated with a terminal when necessary:

$ pkill -KILL -t pts/1

Replace pts/1 and 567221 with the actual terminal and PID you intend to target.

Keep in mind that who lists logged-in users, while tty reports the terminal attached to your current standard input. The ps and pkill commands help you inspect and signal processes associated with a terminal.

That's it!
You now know how to identify active terminal sessions, inspect their associated processes, and terminate unwanted processes using standard Linux commands.

Whether you're administering a remote server, troubleshooting SSH connections, or cleaning up abandoned shells, understanding the relationship between TTYs, processes, and login sessions is an essential Linux system administration skill.

One last thing: Always verify the user, terminal, and processes before sending a termination signal. A session that appears idle may still be doing important work.

Thanks for reading, and happy Linux administration.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers