How To Stop A Process Running On A Specific Port In Linux

Avatar

By squashlabs, Last Updated: October 16, 2023

How To Stop A Process Running On A Specific Port In Linux

To stop a process running on a specific port in Linux, you can follow one of the following methods:

Method 1: Using the lsof command

1. Open a terminal window.

2. Run the following command to identify the process ID (PID) of the process running on the specific port:

   lsof -i :<port_number>

Replace <port_number> with the actual port number on which the process is running. For example, to find the process running on port 8080:

   lsof -i :8080

The output will display the process details along with the PID.

3. Once you have identified the PID, you can use the kill command to stop the process. Run the following command:

   kill <PID>

Replace <PID> with the actual process ID. For example, to stop the process with PID 12345:

   kill 12345

This will send a termination signal to the process running on the specific port, causing it to stop.

Related Article: How To Find Files Based On Wildcard In Linux

Method 2: Using the fuser command

1. Open a terminal window.

2. Run the following command to identify the process ID (PID) of the process running on the specific port:

   fuser -n tcp <port_number>

Replace <port_number> with the actual port number on which the process is running. For example, to find the process running on port 8080:

   fuser -n tcp 8080

The output will display the PID of the process.

3. Once you have identified the PID, you can use the kill command to stop the process. Run the following command:

   kill <PID>

Replace <PID> with the actual process ID. For example, to stop the process with PID 12345:

   kill 12345

This will send a termination signal to the process running on the specific port, causing it to stop.

Additional Notes:

– If the process does not stop after sending the termination signal, you can use the kill -9 command to forcefully kill the process. However, this should only be used as a last resort, as it does not allow the process to perform any cleanup operations.

– It is important to ensure that you have the necessary permissions to stop a process. If you are not running the commands as a privileged user, you may need to use sudo before the commands to run them with elevated privileges.

– If you are unable to identify the process running on a specific port using the above methods, it is possible that the process is running as a system service or daemon. In such cases, you can use the service management commands specific to your Linux distribution to stop the service. For example, on Ubuntu, you can use the systemctl command to stop a service:

  sudo systemctl stop <service_name>

Replace <service_name> with the name of the service you want to stop.

– It is generally a good practice to gracefully stop a process by sending the termination signal before resorting to forceful termination (kill -9). This allows the process to perform any necessary cleanup operations and exit gracefully.

Related Article: How to Copy a Folder from Remote to Local Using Scp in Linux

More Articles from the The Linux Guide: From Basics to Advanced Concepts series:

Tutorial: Using Unzip Command in Linux

This article provides a detailed guide on how to use the unzip command in Linux for file extraction. It covers various topics such as basic usage, listing files in a zip... read more

How to Apply Chmod 777 to a Folder and its Contents in Linux

Applying Chmod 777 permissions to a folder and its contents in Linux can be done easily by following a few simple steps. This step-by-step guide will walk you through... read more

Tutorial on Linux User Management: How to Create a User

User management is a crucial aspect of the Linux operating system, and this article provides a guide on creating and managing users. From adding users to groups and... read more

Using Linux Commands to Find File and Directory Sizes

Retrieving file and directory sizes in Linux is made simple with the du and ls commands. Learn how to use these commands to find the sizes of files and directories in a... read more

How to Sync Local and Remote Directories with Rsync

Learn how to use Rsync to synchronize local and remote directories in Linux with this step-by-step tutorial. Discover the installation and configuration process, as well... read more

How to Alter the Echo Output Colors in Linux

This article provides a simple guide on modifying the output color of echo in Linux using bash color. It covers two methods: using ANSI escape sequences and using tput.... read more