Sling Academy
Home/DevOps/Ubuntu: How to find and kill a process that is using a specific port

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

Last updated: January 28, 2024

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.

Next Article: Ubuntu: How to find a file/directory by name

Previous Article: Ubuntu: How to Change the Root Password

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide