How to Find and Kill a Process by Port in Ubuntu

If you manage an Ubuntu server or use Linux for local development, you have almost certainly encountered the dreaded “Address already in use” error. This happens when you try to start a service (like a Node.js app, an Apache web server, or a Docker container), but the port it needs is already occupied by a rogue background process.

To fix this, you need to identify which process is holding the port hostage and terminate it. While there are many ways to manage processes in Linux, three command-line tools stand out for this specific task: fuser, lsof, and ss.

Here is exactly how to find and kill a process by port in Ubuntu using these built-in utilities.

Method 1: The Fastest Way (Using fuser)

The fuser command is highly recommended because it allows you to identify and kill the process in a single, clean command. It is installed by default on most Ubuntu distributions.

To identify the process:

If you just want to see the Process ID (PID) without killing it, run:

sudo fuser 8080/tcp

(Note: Replace 8080 with your target port number. Use /udp instead if you are troubleshooting a UDP connection).

To kill the process immediately:

Add the -k (kill) flag. This automatically sends a SIGKILL signal to whatever application is hogging the port.

sudo fuser -k 8080/tcp

If you want Ubuntu to ask for your confirmation before it kills the process, add the -i (interactive) flag:

sudo fuser -k -i 8080/tcp

Method 2: The Detailed Way (Using lsof)

The “List Open Files” (lsof) command is a classic Linux sysadmin tool. It is excellent because it provides far more context than fuser, showing you the name of the application, the user running it, and the exact PID.

To identify the process:

sudo lsof -i :8080

This will output a formatted table. Look for the number under the PID column.

To kill the process:

Once you have identified the PID (for example, let’s say the PID is 1234), you must manually terminate it using the kill command:

sudo kill -9 1234

(The -9 flag sends a SIGKILL, which forces the process to stop immediately without waiting for a graceful shutdown).

The lsof One-Liner Shortcut

If you use lsof often, you can string the find and kill commands together. By using the -t flag, lsof strips away the table formatting and outputs only the PID, which is then fed directly into the kill command:

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

Method 3: The Modern Way (Using ss)

Historically, Linux users relied on the netstat command to view network sockets. However, netstat is now considered obsolete and has been replaced by the much faster ss (Socket Statistics) utility.

While ss cannot kill a process natively, it is the best tool for analyzing complex network states before you decide to terminate a service.

To identify the process:

sudo ss -ltnp | grep :8080

Here is what those flags mean:

  • -l: Show only listening sockets.
  • -t: Show TCP connections.
  • -n: Show numerical addresses (this stops Ubuntu from slowly trying to resolve hostnames).
  • -p: Show the process PID using the socket.

The output will end with a string similar to users:(("node",pid=1234,fd=6)). Once you have that PID (1234), you can terminate it normally:

sudo kill -9 1234

By keeping these three methods in your terminal toolkit, you can quickly clear port conflicts and get your Ubuntu services back online without needing to reboot the entire server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.