How to kill or terminate unwanted tty/pts sessions in Linux?
Did you ever face this situation, a lot of terminal connections to your server?
Before starting, we have a brief discussion on TTY. The word tty stands for teletype terminals. Some years ago, 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. Here CryBit going to explain the command-line option to kill unwanted or unused or idle ttys.
We need the PID (Process ID) of that particular terminal (tty). First check the active connections server using the command “w.” Please see the sample output pasted below:
[email protected] [~]# w 02:05:41 up 234 days, 23:46, 3 users, load average: 1.47, 1.89, 1.98 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 w3-oc.lolipop-i 23:51 53:49 0.04s 0.04s -bash root pts/2 w3-oc.lolipop-i 01:11 0.00s 0.01s 0.01s w root pts/3 w3-oc.lolipop-i 01:12 53:32 0.00s 0.00s -bash
Here, you can see three tty connections to your server, pts/0, pts/2 and pts/3 where PTS stands for pseudo terminal. You can also see which processes are currently executing for those tty connections. In this command we could not see the process ID (PID) of those ttys.
We can use the PS command to find out the process ID. Here is the sample output:
# ps -ft tty
[email protected] [~]# ps -ft pts/0 UID PID PPID C STIME TTY TIME CMD root 331857 331761 0 Oct09 pts/0 00:00:00 -bash
Here You will get the user info and process ID. Then use kill command to terminate that tty connection.
For the above example
# kill 331857
If the process doesn’t gracefully terminate, just as a last option you can forcefully kill by sending a SIGKILL
# kill -9
Another way; single command to kill tty connections
You can also use the PKILL command along with the switch “-t” to kill a tty connection forcefully. Please see the commend pasted below:
# pkill -9 -t
# pkill -9 -t pts/0
How to check the current tty/pts session where you connected?
Yup, before going with the kill command, you must have an idea about your tty/pts session. This can be simply checked using the command ps or tty. See the usages pasted below:
[[email protected] ~]# ps PID TTY TIME CMD 29849 pts/0 00:00:00 bash 29996 pts/0 00:00:00 ps
[[email protected] ~]# tty /dev/pts/0
tty is the best command!!
That’s it!! Go ahead and kill _/\_