How to kill a process running on particular port in Linux?

To kill a process running on a specific port in Linux, you need to identify the Process ID (PID) associated with that port and then terminate it. Below are detailed methods with examples for various scenarios.

1. Key Commands and Tools

  • lsof: List open files and ports.
  • ss: Socket statistics (modern replacement for netstat).
  • fuser: Identify processes using files or sockets.
  • kill/pkill: Terminate processes.

2. Step-by-Step Methods

Method 1: Using lsof (List Open Files)

  1. Find the PID associated with the port:
   sudo lsof -i :<PORT>

Example for port 8080:

   sudo lsof -i :8080

Output:

   COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
   node    1234  user   14u  IPv6 123456      0t0  TCP *:8080 (LISTEN)
  1. Kill the process using the PID:
   sudo kill -9 1234  # Replace 1234 with the actual PID

One-liner:

   sudo kill -9 $(sudo lsof -ti :8080)

Method 2: Using ss (Socket Statistics)

  1. Find the PID and process:
   sudo ss -tulpn | grep ':8080'

Output:

   LISTEN 0      511              *:8080            *:*    users:(("node",pid=1234,fd=14))
  1. Kill the process:
   sudo kill -9 1234

One-liner:

   sudo kill -9 $(ss -tulp | grep -oP 'pid=\K\d+' | head -1)

Method 3: Using netstat (Legacy Tool)

  1. Find the PID:
   sudo netstat -tulpn | grep ':8080'

Output:

   tcp6  0  0 :::8080  :::*  LISTEN  1234/node
  1. Kill the process:
   sudo kill -9 1234

Method 4: Using fuser (File User)

  1. Find and kill the process in one step:
   sudo fuser -k 8080/tcp

Output:

   8080/tcp:            1234

3. Advanced Examples

Example 1: Kill All Processes on Port 3000

sudo kill -9 $(sudo lsof -ti :3000)

Example 2: Force-Kill a Stubborn Process

If a process ignores SIGTERM, use SIGKILL (signal 9):

sudo kill -9 $(lsof -ti :8080)

Example 3: Kill a Process Using pkill

sudo pkill -f "node.*8080"  # Kills all node processes on port 8080

Example 4: Check If the Port Is Freed

Verify after killing:

sudo lsof -i :8080  # Should show no output

4. Handling Common Errors

Error 1: “Operation Not Permitted”

  • Cause: Lack of permissions.
  • Fix: Use sudo:
  sudo kill -9 <PID>

Error 2: “No Such Process”

  • Cause: The PID no longer exists.
  • Fix: Refresh the PID list with lsof or ss.

Error 3: Port Still in Use After Killing

  • Cause: The process may restart (e.g., a service).
  • Fix: Stop the service first:
  sudo systemctl stop nginx  # Replace with your service name

5. Summary Table of Commands

CommandDescription
sudo lsof -i :<PORT>List processes using the port.
sudo ss -tulpn | grep ':PORT'Modern alternative to netstat.
sudo kill -9 $(lsof -ti :PORT)One-liner to kill processes on the port.
sudo fuser -k PORT/tcpDirectly kill processes using the port.

6. Key Notes

  • Permissions: Use sudo if you encounter permission issues.
  • Signal Types:
  • -9 (SIGKILL): Force termination (use as a last resort).
  • -15 (SIGTERM): Graceful termination (default).
  • Safety: Confirm the PID matches the intended process to avoid accidental termination.

7. Troubleshooting

If a port remains occupied after killing the process:

  1. Check for child processes:
   ps -ef | grep <PARENT_PID>
  1. Wait: Some ports enter TIME_WAIT state after closing (resolves automatically).

By following these methods, you can reliably free up ports in Linux!

Leave a Reply

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