Ubuntu: How to find and kill a process that is using a specific port

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

In the world of software development and network administration on Ubuntu or any UNIX-like system, working with network ports is a common task. Alternatively, processes may start and stop unexpectedly, leaving certain ports bound. This can prevent other services from starting, which typically listen on these ports. The need to find which process is using a specific port and sometimes forcefully terminating that process is a necessary skill.

This tutorial will take you through various steps and tools available in Ubuntu to identify and kill processes that are using a specific port. We will cover the basic `netstat` and `lsof` commands for beginners, delve into the more advanced `fuser` and `ss` utilities, and explore scripting for automation purposes.

Finding the Culprit with Netstat

One of the simplest ways to find what process is using a specific port is by using the `netstat` command. While `netstat` is deprecated and not installed by default on newer Ubuntu releases, you can still install it with sudo apt install net-tools. To find the process using a specific port, you can use:

sudo netstat -tulnp | grep ':PORT'

Replace ‘PORT’ with the actual port number you are investigating. The flags used here are explained as follows:

  • -t – Display TCP connections
  • -u – Display UDP connections
  • -l – Show only listening ports
  • -n – Show numerical addresses
  • -p – Show the PID and name of the program to which each socket belongs

The output will display the process ID (PID) that is bound to the port, which you can then terminate using the kill command:

sudo kill -9 PID

Employing Lsof for a Deep Dive

The `lsof` command, which stands for List Open Files, is another versatile tool that can be used to find processes using a specific port. Firstly, ensure that `lsof` is installed on your system:

sudo apt install lsof

Once installed, you can run the following to view processes using a particular port:

sudo lsof -i TCP:PORT

If you don’t know whether the connection is TCP or UDP, or want to search both, omit the protocol:

sudo lsof -i :PORT

To kill the found process, make use of the kill command as before:

sudo kill -9 $(sudo lsof -t -i:PORT)

Advanced Process Hunting with Fuser

The `fuser` utility can not only find the process using a port but also allow you to directly kill the process in question. Use the `fuser` command as follows to find the process:

sudo fuser PORT/tcp

And to directly kill the process using that port, append the -k flag:

sudo fuser -k PORT/tcp

Meeting ss: Net-Stat’s Modern Successor

The `ss` command is part of the iproute2 package and designed to replace `netstat`. It is more efficient and faster at displaying network statistics. To find which process holds the port, do:

sudo ss -lptn 'sport = :PORT'

This displays listening sockets (-l), with process information (-p), without resolving addresses (-n), for a specific source port (-s) with your specified PORT.

Scripting for Automation

If you frequently find yourself checking for processes tied to specific ports, consider writing a shell script. An example script:

#!/bin/bash
# Check for process using a port and kill it

PORT=$1
PID=$(sudo ss -lptn 'sport = :$PORT' | tail -n +2 | awk '{print $6}' | cut -d ',' -f 1)

if [[ -n $PID ]]; then
    sudo kill -9 $PID
    echo "Killed process $PID using port $PORT."
else
    echo "No process found using port $PORT."
fi

To use this, save it to a file like killport.sh, give it execution permissions with chmod +x killport.sh and run with ./killport.sh PORT.

Conclusion

Managing ports and the processes bound to them is a critical operation in Ubuntu server management. This guide presented a variety of methods to identify and kill processes using a specific port, increasing efficiency in problem-solving and potentially creating opportunities for automation that save time and prevent errors.