+3 votes
74 views
Terminate Process of a Linux Port

in Linux/Unix by (552k points)
reopened | 74 views

1 Answer

+4 votes
Best answer

1 Terminate Linux Port Process with SOCAT / KILL
2 Terminate Process of a Linux Port with FUSER

In Linux the processes are part of the day to day of the system since they allow both the applications and each service to work in the way that it is, if a process fails we can have serious problems but if a process has some bad configuration or is with errors temporary we can find centralized failures in the global system or in our tasks, now, many processes are linked to a specific port for the destination tasks and that is why on many occasions it may be necessary to terminate the process of a port for reasons such as :

 

Reasons to end process

 

  • Release the port for another use
  • Blocking the application, program or service running on that port
  • System performance glitches

 

 

There are some methods to finish the process of a port and TechnoWikis will explain two of them in detail..

 

 

To stay up to date, remember to subscribe to our YouTube channel!   SUBSCRIBE

 

 


1 Terminate Linux Port Process with SOCAT / KILL


SOCAT is a utility that allows bidirectional data transfers using two independent data channels in Linux, its usage syntax is as follows:
 socat [options] <address 1|> <address 2> 
Channels with socat
The channels that socat can connect are:

 

  • Records
  • pipes
  • Devices (serial, pseudo-terminal)
  • Sockets (UNIX, IP4, IP6 - raw, UDP, TCP)
  • SSL sockets
  • GNU line editor
  • programs
  • proxy connections
  • file descriptors

 

socat utilities
Although it is not such a popular utility, it can help us in situations such as:

 

  • Redirect TCP-oriented programs to a serial line
  • Use IP6 relay
  • To act as a TCP port forwarder
  • Work as a firewall tool

 

 

socat usage options
The options for using socat on Linux are:

 

  • -V: allows to see the version and information of functions in the standard exit
  • -hh : List all common address option names
  • -d[ddd]: increase the verbosity or way of reading the result
  • -D: parse file descriptors before execution
  • -ls: send an event to stderr
  • -lp<program>: defines the program used to log in
  • -lu: makes use of microseconds to record timestamps
  • -v – Create a verbose text dump of the data traffic
  • -lh: add the hostname in the message
  • -u: use unidirectional mode from left to right
  • T<timeout>: is the total idle timeout in seconds
  • -U unidirectional mode from right to left)
  • -g: do not check option groups
  • -4: sets as preference prefers IPv4
  • -6: set IPv6 as preference

 

 

Step 1

In this case we will use Ubuntu, open the terminal and install Socat:
 sudo apt install socat 
image

 

Step 2

For Fedora we run:
 sudo dnf install socat 
Step 3

In Manjaro we can run:
 sudo pacman -S socat 
Step 4

We will create a TCP connection to a port, for this we need the source and destination addresses (we can use STDIN or STDOUT) and additionally we must add the protocol, the IP address and the port number:
 socat tcp-listen:"port",bind=127.0.0.1 stdout & 
image

 

step 5

In this case, a connection has been created between a listening TCP socket using port 7889 and taking the loopback IP address of 127.0.0.1 with STDOUT, when adding the "&" sign we refer to it running in the background.

 

We create UDP and SCTP connections:

 socat udp-listen:"port",bind=127.0.0.1 stdout & socat sctp-listen:"port",bind=127.0.0.1 stdout & 
image

 

Step 6

With this, these processes have been activated on said port, we are going to use the KILL command to "kill" or terminate said process, first we list the details of the process:
 lsof -i tcp:"port" 
image

 

Step 7

Keeping the PID in mind, we finish the process with the order:
 sudo kill PID 
step 8

We enter the password if necessary:

 

 

image

 

step 9

We confirm that the process has finished:
 lsof -i tcp:"port" 
image

 

step 10

As an extra point, it is possible to pipe the output of lsof to awk and indicate that a search be made for the lines that contain the selected port and print the second field of that line, without showing the PID, in this case it executes:
 lsof -i tcp:port | awk '/port/{print $2}' 
step 11

We now list the SCTP process:
 lsof -i sctp:"port" 
step 12

We will see the following message:

 

 

image

 

 

SCTP (Stream Control Transmission Protocol) is a protocol developed to allow connection in order to transmit multiple data streams simultaneously between two endpoints, to manage this protocol we will use the ss command:

 ss-sap 
image

 

step 13

The parameters used have been:
  • -S (SCTP) : allows to search for SCTP sockets
  • -a (all - all): allows to search all types of sockets
  • -p (processes): allows to list the details of the process through the socket
step 14

Now run the following:

 

With this command the result of ss has been linked to grep and then the port number is searched, then the output of grep is connected to awk, once there the -F parameter creates a separating string using the comma "," acting as a field delimiter, a string with the value of “pid=” is searched for, and then the second field is printed.

 ss -Sap | grep "port" | awk -F',' '/pid=/{print $2}' 
image

 

 

 


2 Terminate Process of a Linux Port with FUSER

 

Fuser Options
Fuser is a command to identify processes through files or sockets by displaying the PIDs of the linked processes, some of its usage options are:
  • -a : show all specified files
  • -F: ignored errors
  • -k: kill processes accessing the defined file
  • -i: throw a confirmation to the user before killing the process
  • -m – Indicates the file on a mounted file system or block device that is mounted for processing
  • -n space: allows to select a different namespace
  • -u : add the username of the process owner to each PID

 

 

Step 1

We open the terminal and access port details:
 fuser -n tcp port fuser port/udp 
image

 

Step 2

We finish the process:
 sudo fuser -k port/protocol 
image

 

Step 3

We can see in some cases a more complete result:

 

 

image

 

With these options it is possible to terminate port processes in Linux..

 


by (3.5m points)
edited

Related questions

+4 votes
1 answer
+3 votes
1 answer
asked May 22, 2019 in Linux / Unix by backtothefuture (552k points) | 280 views
+5 votes
1 answer
asked Oct 18, 2022 in Linux/Unix by backtothefuture (552k points) | 89 views
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 225 views
+3 votes
1 answer
asked Nov 9, 2019 in Linux / Unix by backtothefuture (552k points) | 276 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users