How to Terminate a Process on a Specific Port in Ubuntu

Avatar

By squashlabs, Last Updated: October 16, 2023

How to Terminate a Process on a Specific Port in Ubuntu

To terminate a process running on a specific port in Ubuntu, you can follow these steps:

Step 1: Identify the process running on the port

First, you need to identify the process ID (PID) of the process running on the specific port. You can do this by using the netstat command with the -tuln options to list all the network connections and their associated processes.

Open a terminal and run the following command:

netstat -tuln | grep <port>

Replace <port> with the port number you want to terminate the process on. This command will display the processes running on the specified port along with their PIDs.

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

Step 2: Terminate the process

Once you have identified the PID of the process running on the port, you can use the kill command to terminate it. The kill command sends a signal to the process, causing it to exit.

In the terminal, run the following command:

kill <PID>

Replace <PID> with the process ID you obtained from the previous step. This command will send the default SIGTERM signal to the process, which allows it to gracefully shut down. If the process does not respond to the SIGTERM signal, you can use the SIGKILL signal to forcefully terminate it by running the following command:

kill -9 <PID>

Again, replace <PID> with the process ID.

Alternative: Using lsof

Another way to identify and terminate a process on a specific port is by using the lsof command. The lsof command lists open files and the processes that have them open. This can be useful for finding processes associated with a particular port.

To list the processes running on a specific port, open a terminal and run the following command:

sudo lsof -i :<port>

Replace <port> with the port number you want to terminate the process on. This command will display the processes running on the specified port along with their PIDs.

To terminate a process using lsof, you can combine it with the kill command. For example, to terminate a process running on port 8080, you can run the following command:

sudo kill $(sudo lsof -t -i :8080)

This command uses command substitution to pass the output of lsof -t -i :8080 (which gives the PID of the process) as an argument to the kill command.

Best Practices

Here are some best practices to consider when terminating a process on a specific port:

1. Use the SIGTERM signal first: When terminating a process, it is generally recommended to use the SIGTERM signal first. This allows the process to perform any necessary cleanup tasks before exiting. If the process does not respond to the SIGTERM signal, then you can resort to using the SIGKILL signal.

2. Verify the process before terminating: Before terminating a process, it’s a good practice to verify that it is indeed the process you want to terminate. You can check the process name and other details using the ps command, along with the PID obtained from netstat or lsof.

3. Use sudo when necessary: Some processes may require root privileges to be terminated. In such cases, you can use the sudo command to run the kill or lsof commands with elevated privileges.

4. Automate the process termination: If you frequently need to terminate processes on specific ports, you can consider automating the process using scripts or tools. For example, you can write a shell script that takes the port number as an argument and performs the necessary steps to terminate the process.

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