1

I was wondering, is there any way to kill a process that is running on a specific IP and port on Ubuntu 14.04 on a local IP and port? Preferably, this would be in one command, but if not, a bash script would be perfectly fine as well.

  • 4
    by "running" do you mean "listening" or "connected to", and by IP do you mean local or remote, and by port, do you mean local or remote? What Operating System? – Jeff Schaller May 17 '17 at 19:44
  • linking: unix.stackexchange.com/q/140482/117549 – Jeff Schaller May 17 '17 at 19:46
  • I already know about fuser, and by running I mean listening to. It is Ubuntu 14.04, and IP and port are local. – Stephen May 17 '17 at 19:48
  • 2
    please edit your question (and tags) to clarify – Jeff Schaller May 17 '17 at 19:49
6

There are likely cleaner ways, but something along the lines of:

netstat -lnp | grep 'tcp .*127.0.0.1:9984' | sed -e 's/.*LISTEN *//' -e 's#/.*##' | xargs kill
5

Using ss we can get details of process/connections which are listening on a specific IP and port, for src 127.0.0.1 and port 80:

sudo ss -lp  '( dport = :80 )' src 127.0.0.1

then we can only get the PID's using grep and kill them all using xargs and kill.

sudo ss -lp  '( dport = :80 )' src 127.0.0.1 | grep -Po "(?<=pid=).*(?=,)"\
| sort | uniq | xargs kill

You can also use -a switch instead of -l to get a list of all listening and non-listening sockets.

3

fuser on Linux has an option which does exactly what you are looking to do.

For example, fuser 80/tcp will print the PID of the process bound on that port and fuser -k 80/tcp will kill that process.

  • Please provide the answer with explanation so that your answer stands alone. – Tom Hale May 18 '17 at 2:38
1

On linux, as root, you can use either

ss -ptan 

or

netstat -ntulp

to get a list of all running processes that "listen"(in this case tcp, replace the "t" with "u" for udp).

So one liner, to kill process listening on port 80, would look something like that:

ss -tanp | grep 80 | head -1 | sed 's_.*pid=\(.*\),.*_\1_' | xargs kill
  • grep 80 would match something like 172.217.22.280:45581. I don't think that's intentional. You probably want to do a match like ss -tanp | awk '$1=="LISTEN" && $4=="<IP>:<PORT>" { print $6 }' – David Foerster May 17 '17 at 20:28
  • Yes it will... I need a better regex. Will update it. – man0v May 17 '17 at 20:30
-1

The only IP a process on the local machine can run on is 127.0.0.1, lo, the loopback device, that's known as localhost. Ports there can be forwarded to a public IP address with network configurations.

I usually use sudo lsof -i :<port> to see what's causing an EADDRINUSE because it's quicker to type.

sudo lsof -i :443 | tail -1 | cut -f2 -d' ' | xargs kill

Will kill the process running on port 443, if your lsof gives output like:

COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python3 32233  cat    4u  IPv4 24123106      0t0  TCP *:http-alt (LISTEN)

From lsof --help:

-i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
  • What's wrong with this answer? Explain your votes, se vous plaît – cat May 26 '17 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.