Logo

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

To kill a process running on a specific port in Linux, you can first identify the process ID (PID) listening on that port, then terminate it. Below are a few common approaches:

1. Using lsof and kill

  1. Find the PID of the process bound to a particular port, for example 3000:
    lsof -i :3000
    This displays the process name, PID, and other details.
  2. Kill the process by PID:
    kill <PID>
    For a more forceful kill, use kill -9 <PID>.

Alternatively, you can chain the commands:

kill -9 $(lsof -t -i :3000)
  • -t: Output only the process IDs.
  • -i :3000: Lists processes using port 3000.

2. Using fuser

fuser can also identify processes using a given port. For example:

fuser -n tcp 3000

Then kill it with:

fuser -k -n tcp 3000
  • -k: Kills the process using the specified protocol/port combination.
  • -n tcp 3000: Looks for TCP processes on port 3000.

3. Confirming the Process is Terminated

After running the command, re-check the port to ensure no process is bound:

lsof -i :3000

If no output appears, the process is no longer running on that port.

Further Learning

Mastering these kinds of Linux commands is one piece of becoming a stronger developer. If you also want to solidify your coding fundamentals and problem-solving skills, consider these two courses from DesignGurus.io:

With strong system know-how (like killing processes by port) plus solid coding skills, you’ll be well-prepared to handle both low-level operations and high-level development tasks.

CONTRIBUTOR
TechGrind