To kill a Linux course of you want its ID or its identify. If all you understand is the port it’s utilizing, can you continue to kill it? Sure, in a number of alternative ways.
Killing Processes
Sometimes a Linux course of can develop into unresponsive. It could cease working accurately, or it would proceed to work however ignore requests for it to close down, or begin gobbling reminiscence, CPU, or community bandwidth.
No matter your motives, there are methods to kill a course of from the Linux command line. The basic technique is to make use of the kill command with the method ID of the method you wish to terminate. The kill
command has some shut relations. The pkill
command will kill a course of by identify, and killall
will kill all processes it could possibly discover that share a part of a reputation.
If all you understand a few course of is it’s utilizing a port in your laptop, there are nonetheless methods to establish and kill it. In networking phrases, “port” can imply a bodily connection into which you insert a cable with a plug on the top, comparable to a CAT5 or 6 community lead, or it could possibly imply a software program port.
A software program port is the ultimate a part of a community connection. The IP deal with of a tool identifies the pc or different community equipment. The functions inside the pc use completely different ports. These present one other degree of granularity. The community site visitors has arrived on the appropriate laptop utilizing the IP deal with, and by utilizing port addressing it may be delivered to the right utility.
It’s like postal mail arriving at a lodge, then being sorted and delivered to the suitable rooms. The IP deal with is like the road deal with of the lodge, and the room numbers are just like the port numbers.
In case you see community exercise on a port and also you don’t acknowledge the method that’s producing it, or its conduct is problematic or suspicious, you may want wish to kill the method. Even when all you understand is the port quantity, you possibly can observe down the method and kill it.
Creating Connections With socat
In order that we now have some connections to kill, we’ll use socat
to create community connections utilizing completely different protocols. You’ll want to put in socat
. To put in it on Ubuntu, use this command:
sudo apt set up socat
On Fedora use dnf
:
sudo dnf set up socat
On Manjaro you might want to sort:
sudo pacman -S socat
The syntax for socat
is simple if a little bit long-winded. We have to present the supply and vacation spot addresses. For every of those, we have to present the protocol, IP deal with, and port quantity. We are able to substitute STDIN or STDOUT as a supply or vacation spot.
This command creates a connection between a TCP listening socket on port 7889, on the loopback IP deal with of 127.0.0.1, and STDOUT. The ampersand “&
” runs the command within the background, in order that we retain entry to the command line.
socat tcp-listen:7889,bind=127.0.0.1 stdout &
We’ll create two extra connections in order that we now have a small collection of sockets utilizing completely different protocols. We’ll create a UDP connection and an SCTP connection. The one a part of the command that adjustments is the protocol.
socat udp-listen:7889,bind=127.0.0.1 stdout &
socat sctp-listen:9999,bind=127.0.0.1 stdout &
RELATED: What is the Distinction Between TCP and UDP?
Utilizing Kill
In fact, we will use kill
to terminate the method, simply so long as we all know what the ID of the method is. To seek out the PID, we will use the lsof
command.
To listing the small print of the method on port 7889 which are utilizing the TCP protocol, we use the -i
(web deal with) possibility, like this.
lsof -i tcp:7889
The PID of this course of is 3141, and we will go forward and use that with kill
:
sudo kill 3141
We are able to save ourselves some effort if we use pipes. If we pipe the output of lsof
into awk
and inform awk
to seek for traces that comprise the port we’re serious about—7889—and print the second subject from that line, we’ll isolate the PID.
lsof -i tcp:7889 | awk '/7889/{print $2}'
We are able to then pipe the output from awk
into the kill
command utilizing xargs
. The xargs
command takes its piped enter and passes it to a different command as command line parameters. We’ll use xargs
with the kill
command.
lsof -i tcp:7889 | awk '/7889/{print $2}' | xargs kill
We don’t get any visible suggestions. Within the typical Linux method, no information is nice information. If you wish to verify that the method has been terminated you should use lsof
as soon as extra.
lsof -i tcp:7889
As a result of lsof
doesn’t report something, we all know there’s no such connection.
We are able to take away a course of utilizing the UDP protocol just by changing “tcp” with “udp” in our earlier command.
lsof -i udp:7889 | awk '/7889/{print $2}' | xargs kill
Nevertheless, lsof
doesn’t acknowledge the SCTP protocol.
lsof -i sctp:7889
We are able to use the ss
command to try this. We’re utilizing the -S
(SCTP) choice to seek for SCTP sockets, the -a
(all) possibility to go looking for every type of sockets (listening, accepting, linked, and so forth.), and the -p
(processes) choice to listing the small print of the method utilizing the socket.
ss -Sap
We are able to parse that output utilizing grep
and awk
. We might additionally parse it utilizing grep
and a few PERL regexes, however this manner is far simpler to grasp. In case you have been going to make use of this greater than a few times you’d most likely make an alias or shell operate out of it.
We’ll pipe the output from ss
into grep
and seek for our port quantity, 7889. We’ll pipe the output from grep
into awk
. In awk
, we’re utilizing the -F
(separator string) choice to set a comma “,
” as the sector delimiter. We seek for a string containing “pid=”, and print the second comma-delimited subject from that string.
ss -Sap | grep "7889" | awk -F',' '/pid=/{print $2}'
That has given us the string “pid=2859.”
We are able to pipe that into awk
once more, set the sector delimiter to the equals signal “=
” and print the second subject from that string, which would be the textual content behind the equals signal.
ss -Sap | grep "7889" | awk -F',' '/pid=/{print $2}' | awk -F'=' '{print $2}'
We’ve now remoted the method ID. We are able to use xargs
to cross the PID to kill
as a command line parameter.
ss -Sap | grep "7889" | awk -F',' '/pid=/{print $2}' | awk -F'=' '{print $2}' | xargs kill
That kills the method that was utilizing the SCTP protocol socket on port 7889.
The fuser Command
The fuser
command simplifies issues an awesome deal. The draw back is, that it solely works with TCP and UDP sockets. On the plus aspect, these are the 2 commonest varieties of sockets you’ll have to cope with. The fuser
command was already put in on the Ubuntu, Fedora, and Manjaro computer systems we checked.
All you might want to do is use the -k
(kill) possibility, and supply the port and protocol. You possibly can both use the -n
(namespace) possibility and supply the protocol and port, or use the “ahead slash shortcut format” and put the port quantity first.
fuser -n tcp 7889
fuser 7889/udp
The port quantity, protocol, and PID of the terminated course of are printed within the terminal window.
Strive fuser First
It’ll most likely be put in on the pc you’re engaged on, and the protocol is more likely to be TCP or UDP, so there’s an awesome probability the only method will give you the results you want.