Kill process on port using command fuser

fuser is a linux command to identify processed using files or socket

fuser -k -n tcp PORT
  • -n tcp: select tcp protocol
  • -k : kill
  • PORT : port number

Alternatively you can use:

fuser -k PORT/tcp
  • PORT/tcp is shortcut of -n tcp PORT

Below is a real example:

root@ubuntu:~# fuser -n tcp -k 22
22/tcp:               7256  7349  7401
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# fuser  -k 22/tcp
22/tcp:               7643
root@ubuntu:~#

Kill process on port using command lsof

lsof stands for list open files.

kill -9 `lsof -t -i:port`

OR

kill -9 $(lsof -t -i:port)
  • -t : produce process ID only
  • -i:port : specify a port

For example ,below command kill all processes on port 22 (ssh)

root@ubuntu:~# kill -9 `lsof -t -i:22`

Kill process on port manually

Normally I kill process on port manually , first filter out the IDs of processes who are on port ,then use kill -9 PID to kill them.

1.list process on port

  • use linux command ss

    ss -apn |grep :PORT
    
    • -a : Display both listening and non-listenning
    • -p : show process ID
    • -n : numeric , eg: resove sshd as number 22

OR

  • use linux command netstat

    netstat -apn | grep :PORT
    

Below are examples which list processes on port 22 (ssh)

root@ubuntu:~# ss -apn |grep :22
tcp     LISTEN   0        128                                           0.0.0.0:22                                               0.0.0.0:*                       users:(("sshd",pid=7834,fd=3))                                                 
tcp     ESTAB    0        0                                      192.168.75.128:22                                          192.168.75.1:61270                   users:(("sshd",pid=7894,fd=4),("sshd",pid=7842,fd=4))                          
tcp     LISTEN   0        128                                              [::]:22                                                  [::]:*                       users:(("sshd",pid=7834,fd=4))                                                 
root@ubuntu:~# 
root@ubuntu:~# netstat -apn | grep :22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7834/sshd: /usr/sbi 
tcp        0      0 192.168.75.128:22       192.168.75.1:61270      ESTABLISHED 7842/sshd: j [priv] 
tcp6       0      0 :::22                   :::*                    LISTEN      7834/sshd: /usr/sbi 
root@ubuntu:~#
  • 7834/sshd : where 7834 is one of the process ID

2.kill those processes using kill -9 processID

Then you can kill those particular processes using linux command kill.