Quick Links
To kill a Linux process you need its ID or its name.
If all you know is the port it’s using, can you still kill it?
Yes, in several different ways.
fatmawati achmad zaenuri/Shutterstock.com
Killing Processes
Occasionally aLinux processcan become unresponsive.
The classic method is to use thekill command with the process IDof the process you want to terminate.
Thekillcommand has some close relatives.
`
A software port is the final part of a internet connection.
TheIP addressof a gadget identifies the computer or other internet appliance.
The applications inside the computer use different ports.
These provide another level of granularity.
It’s like postal mail arriving at a hotel, then being sorted and delivered to the appropriate rooms.
You’ll need to installsocat.
We need to provide the source and destination addresses.
For each of these, we need to provide the protocol, IP address, and port number.
We can substitute STDIN or STDOUT as a source or destination.
We’ll create two more connections so that we have a small selection of sockets using different protocols.
We’ll create aUDP connectionand anSCTP connection.
The only part of the command that changes is the protocol.
Related:What’s the Difference Between TCP and UDP?
To find the PID, we can usethelsofcommand.
We can then pipe the output fromawkinto thekillcommand usingxargs.
`
Thexargscommand takes its piped input and passes it to another commandas command line parameters.
We’ll usexargswith thekillcommand.
We don’t get any visual feedback.
In the typical Linux way, no news is good news.
Becauselsofdoesn’t report anything, we know there’s no such connection.
However,lsofdoesn’t recognize the SCTP protocol.
We can usethesscommandto do that.
), and the-p(processes) option to list the details of the process using the socket.
We can parse that output usinggrepandawk.
We could also parse it usinggrepand some PERL regexes, but this way is much easier to understand.
We’ll pipe the output fromssintogrepand search for our port number, 7889.
We’ll pipe the output fromgrepintoawk.
We search for a string containing “pid=”, and print the second comma-delimited field from that string.
That has given us the string “pid=2859.”
We’ve now isolated the process ID.
We can usexargsto pass the PID tokillas a command line parameter.
That kills the process that was using the SCTP protocol socket on port 7889.
The fuser Command
Thefusercommand simplifies things a great deal.
The downside is, that it only works withTCP and UDPsockets.
Thefusercommand was already installed on the Ubuntu, Fedora, and Manjaro computers we checked.
All you gotta do is use the-k(kill) option, and provide the port and protocol.
The port number, protocol, and PID of the terminated process are printed in the terminal window.
Related:How to Kill a Process on Mac