How to Kill user tty/pts sessions on Linux Systems.

First things first, back in the days user terminals were connected to computers' electromechanical teleprinters or teletypewriters (TeleTYpewriter, TTY), since then the name TTY has continued to be used as the name for the text-only console. The word tty stands for teletype terminals.

1- Check active users logged into the server with: w

In other to kill unwanted or unused or idle TTYs we need the PID (Process ID) of that particular terminal (TTY). We first need to check the current connections to the server using the w command.

$ w
 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    1xx.1xx.xx.xx   14:57    1.00s  0.4s  0.02s w
user2   pts/1    1xx.1xx.xx.xx   09:22    1.00s  0.6s  0.02s -zsh
user3   pts/2    1xx.1xx.xx.xx   06:19    1.00s  0.13s  0.02s -zsh

Here, as you can see there are three (3) tty connections to our server, pts/0, pts/2 and pts/3 where PTS stands for pseudo terminal slave. You can also see which processes are currently executing for those tty connections.

2- Get the PID (Process ID) of a connected terminal (TTY) with: ps -ft tty

We can use the Process Status ps command to find out the process ID.

ps -ft tty

$ ps -ft pts/1

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

Here... #567221 is the process ID. We then use the kill command to terminate that tty connection.

$ kill 567221

If the process doesn't gracefully terminate, you can forcefully kill it by sending a SIGKILL kill -9

$ kill -9 567221

Alternatively use:
Single command to kill tty connections.

We can also use the pkill command along with the switch -t to kill a tty connection forcefully.

$ pkill -9 -t pts/0

Commands

  • w: show who is logged on and what they are doing
  • who: show who is logged on
  • tty: show current users pseudo-terminal
  • ps -ft pts/1: get the process ID for the pseudo-terminal
  • pkill: signal process based on name and other attributes

That's it!!
Thanks for reading, go ahead and kill them OFF.