Ubuntu
Process Management
Operating Systems
Terminal Commands
Debugging

How to kill a process on a port on ubuntu

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If a port is already in use on Ubuntu, the job is really two steps: find the process that owns the port, then stop that process safely. The safest workflow is to identify the PID first, send a normal termination signal, and only escalate to a force kill if the process refuses to exit. Tools such as ss, lsof, and fuser make this straightforward.

Find the Process with ss

ss is the modern built-in tool for inspecting sockets.

bash
sudo ss -ltnp | grep ':8080'

This shows listening TCP sockets on port 8080 along with the owning process information when permissions allow it.

If you are looking for a different port, replace 8080 with that port number.

lsof Is Often Even Easier to Read

Many developers prefer lsof because it shows the mapping from open network endpoint to PID clearly.

bash
sudo lsof -i :8080

Typical output includes the process name and the PID. Once you have the PID, you can stop it directly.

Kill Gracefully First

If the PID is 1234, send SIGTERM first.

bash
kill 1234

or, if root privileges are required:

bash
sudo kill 1234

This gives the process a chance to release resources cleanly.

Use -9 Only If It Refuses to Exit

If the process ignores normal termination, force it.

bash
sudo kill -9 1234

-9 sends SIGKILL, which the process cannot handle or ignore. It is effective, but it is also abrupt. Use it only after a normal kill fails.

fuser Can Find and Kill in One Command

If you already know the port and just want the process gone, fuser is very convenient.

bash
sudo fuser -k 8080/tcp

This kills whatever process is using TCP port 8080.

It is efficient, but it is less explicit than checking the PID first, so be careful in shared or production environments.

Understand What You Are Killing

A port conflict is not always a rogue process. It may be:

  • your own app from a previous run
  • a system service such as Nginx or PostgreSQL
  • Docker or another container runtime proxy
  • a development server started by an IDE

Before killing anything, verify that the process really should be stopped.

Common Development Shortcut

A frequent local development pattern is:

bash
sudo lsof -ti :8080 | xargs -r kill

This finds the PID using the port and sends a normal termination signal. It is useful for local workflows, but the expanded two-step version is easier to reason about when you are debugging something sensitive.

Common Pitfalls

  • Using kill -9 immediately instead of trying a normal termination signal first.
  • Killing the process on a port without confirming whether it is a critical system service.
  • Grepping the wrong port or protocol and stopping the wrong process.
  • Forgetting that ss and lsof may need sudo to show ownership details.
  • Treating the port conflict as the root problem when the real issue is a repeatedly restarting service.

Summary

  • First identify the process using the port with ss, lsof, or fuser.
  • Prefer a normal kill before escalating to kill -9.
  • 'fuser -k is the quickest one-command solution when you know exactly what you are doing.'
  • Always confirm what service owns the port before stopping it.
  • Port-based process killing is simple, but it should still be done deliberately.

Course illustration
Course illustration

All Rights Reserved.