Kill process running on port 80

There are several ways to find which running process is using a port.

Using fuser it will give the PID(s) of the multiple instances associated with the listening port.

sudo apt-get install psmisc
sudo fuser 80/tcp

80/tcp:               1858  1867  1868  1869  1871

After finding out, you can either stop or kill the process(es).

You can also find the PIDs and more details using lsof

sudo lsof -i tcp:80

COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME  
nginx   1858     root    6u  IPv4   5043      0t0  TCP ruir.mxxx.com:http (LISTEN)  
nginx   1867 www-data    6u  IPv4   5043      0t0  TCP ruir.mxxx.com:http (LISTEN)  
nginx   1868 www-data    6u  IPv4   5043      0t0  TCP ruir.mxxx.com:http (LISTEN)  
nginx   1869 www-data    6u  IPv4   5043      0t0  TCP ruir.mxxx.com:http (LISTEN)  
nginx   1871 www-data    6u  IPv4   5043      0t0  TCP ruir.mxxx.com:http (LISTEN)  

To limit to sockets that listen on port 80 (as opposed to clients that connect to port 80):

sudo lsof -i tcp:80 -s tcp:listen

To kill them automatically:

sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill

Here is a oneliner that shows the command to run :

echo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":80$") { gsub("/.*","",$7); print $7; exit } }')

Replace echo by sudo for the process to actually be killed.


Three options for listing open ports are offered in jsh's whatisonport:

netstat -anp --numeric-ports | grep ":${PORT}\>.*:" 

fuser -v "${PORT}"/tcp

lsof -P -S 2 -i "tcp:${PORT}" | grep "\(:${PORT}->.*:\|:$PORT (LISTEN)$\)"

I prefer netstat because it is fast, concise, and can list ports opened by other users. (Although it will still need superuser/user privileges to list the names and PIDs of such processes.)

Outputs

$ netstat -anp --numeric-ports | grep ":80\>.*:" 
tcp6       0      0 :::80           :::*            LISTEN      1914/apache2    

$ fuser -v "80/tcp"
                     USER        PID ACCESS COMMAND
80/tcp:              root       1914 F.... apache2
                     www-data  12418 F.... apache2
...

$ lsof -P -S 2 -i "tcp:80" | grep "\(:80->.*:\|:80 (LISTEN)$\)"
apache2  1914     root    4u  IPv6   11920      0t0  TCP *:80 (LISTEN)
apache2 12418 www-data    4u  IPv6   11920      0t0  TCP *:80 (LISTEN)
...

The use of grep in two cases is to match only the port on the local side, and skip open connections to a remote port 80. (An alternative would be to use -l with netstat, or with lsof to use -sTCP:LISTEN, but I like the greps above because they will also catch outgoing connections from the given port, which may occasionally be of interest.)

With lsof we use -P to display :80 instead of :http to make the grep possible. The -S 2 option forces lsof to complete in a timely manner.

Killing the process

Assuming we want to use netstat, we could grab the PIDs like this:

$ netstat -anp --numeric-ports | grep ":80\>.*:" | grep -o "[0-9]*/" | sed 's+/$++'
1914
...

And we could even pass those PIDs to kill:

... | xargs -d '\n' kill -KILL

However there is often a potential for false positive when using regexps, so I would recommend just looking at the initial output of netstat and then manually deciding whether or not to run:

$ kill -KILL 1914

See also

I have another script called listopenports which may be of interest.

Tags:

Linux

Debian