Linux: Kill process on specific port

6

2

How to kill a process if its port is known? For example if a process is running at port 12345 then how it can be terminated in linux/ubuntu.

user2823345

Posted 2013-10-07T11:27:00.763

Reputation: 61

Answers

11

You can use

sudo netstat -tupln

to show what is listening on what port. You should see something similar to this (I've simplified the output somewhat).

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2472/apache2

That fourth column (0.0.0.0:80 in my example) will show you the port number (80 here) and the final column (2472/apache2) will show you the PID (2472).

You can then issue

sudo kill -15 PID

where PID is the PID we found with the previous command. This will send SIGTERM to the process. If that fails, you may need to

sudo kill -9 PID

but that is generally a less friendly way to kill process. For more info, you should checkout

man kill

bugsduggan

Posted 2013-10-07T11:27:00.763

Reputation: 111

I've been searching for a simple solution. This is it. It should be accepted. Thanks for the great response. – Jacob Schneider – 2019-02-16T00:33:55.250

1

  1. list all listening port:

    netstat -antu

  2. take the correspondent one, let's say 80 and kill it using this:

    kill -9 $( lsof -i:80 -t )

hd84335

Posted 2013-10-07T11:27:00.763

Reputation: 173