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.
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
sudo ss -lp '( dport = :80 )' src 127.0.0.1
then we can only get the
grep and kill them all using
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.
On linux, as root, you can use either
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
The only IP a process on the local machine can run on is
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)
-i i select by IPv address: [proto][@host|addr][:svc_list|port_list]