How to Find and Kill a Process Using a Specific Port in Linux

Problem

A port is already in use, and your service fails to start.
You need to quickly find which process is using that port and stop it.

Command / Script

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

Explanation

  • lsof -t -i :PORT → returns PID using the port
  • kill -9 → force kills that process

Replace PORT with your target (e.g., 3000, 8080).

Example

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

Kills any process blocking port 3000.

Automation (Optional but preferred)

#!/bin/bash

PORT="$1"

if [ -z "$PORT" ]; then
  echo "Usage: killport <PORT>"
  exit 1
fi

PID=$(lsof -t -i :$PORT)

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

Usage:

chmod +x killport
./killport 3000

Quick Note

kill -9 is forceful. Use carefully — no graceful shutdown.

Final Resolution

The process blocking the port is now removed and the port is free again. Your service can start normally without port conflict.