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.

Linux laptop showing a bash prompt

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.

Installing socat on Ubuntu

`

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.

Installing socat on Fedora

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.

Installing socat on Manjaro

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.

Creating a listening TCP socket connection with socat

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.

Creating listening UDP and SCTP socket connections with socat

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.

Using lsof to show the details of a process using a specific port and protocol

`

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.

Piping the output of lsof into awk

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.

Using pipes to take the output of lsof into awk and from awk into xargs and kill

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.

Using lsof to search for details of a process using a specific port and protocol without sucess

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.

Using pipes to take the output of lsof into awk and from awk into xargs and kill, for a UDP socket

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.

lsof does not work with the SCTP protocol

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.

Printing the details of a process using an SCTP socket with ss

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.

Using pipes to connect ss, grep, and awk to extract the PID string

The port number, protocol, and PID of the terminated process are printed in the terminal window.

Related:How to Kill a Process on Mac

Using pipes to connect ss, grep, and awk twice, to extract the PID

Using pipes with ss, grep, awk, and xargs to terminate an SCTP socket process

Using the fuser command to delete the processes using TCP and UDP sockets