Linux
Process Management
Command Line
System Administration
Troubleshooting

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

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

To manage processes effectively on a Linux system, it is common to encounter situations where a specific port is occupied by an undesired or errant process. Understanding how to safely kill a process running on a particular port is crucial for system administration, development, debugging, and ensuring system security. Here's a detailed guide on how to achieve this safely and effectively.

Identifying the Process

Before terminating a process, we first need to identify which process is using the particular port. The netstat and ss tools are commonly used for this purpose.

Using netstat

netstat (Network Statistics) is a versatile networking tool for Linux. Although it's being replaced by more powerful tools like ss, many systems still include netstat. To find the process ID (PID) using a specific port, use:

bash
netstat -ltnp | grep ':PORT'

Replace PORT with the actual port number. Here, the flags used are:

  • -l for only services that are listening.
  • -t for TCP connections.
  • -n to show numerical addresses.
  • -p to show the PID and the name of the program.

Using ss

ss is a utility to investigate sockets. It's intended to be faster and more informative than netstat. To find the process using a specific port:

bash
ss -ltnp | grep ':PORT'

The flags used with ss are similar to those used with netstat.

Killing the Process

Once you identify the PID of the process using the port, you can terminate it. The primary command used for stopping a process in Linux is kill. It sends a specific signal to the process. By default, the terminate signal is sent, but you can specify different signals, depending on how gracefully you want to end the process.

Gracefully Stopping the Process

Sending the SIGTERM signal allows the process to terminate gracefully, releasing resources and saving state if appropriate:

bash
kill PID

Or:

bash
kill -SIGTERM PID

Forcibly Killing the Process

If the process doesn't respond to SIGTERM, use SIGKILL, which forces the process to stop immediately:

bash
kill -SIGKILL PID

Using killall and pkill

If you prefer using the process name instead of the PID:

  • killall kills processes by name, e.g., killall nginx.
  • pkill can match partial names, e.g., pkill ngi.

Automating the Process

For automation or scripting, you might want to combine the steps into a single command. Here’s how you can do it using ss and kill:

bash
kill $(ss -ltnp | grep ':PORT' | awk '{print $6}' | sed 's/.*pid=\([^,]*\).*/\1/')

Replace PORT with your specified port. This command sequence does the following:

  • ss -ltnp | grep ':PORT' finds the process using the port.
  • awk '{print $6}' extracts the column that includes PID info.
  • sed 's/.*pid=$[^,]*$.*/\1/' parses out the PID.
  • kill $(...) sends the terminate signal to the PID.

Summary Table

Here is a summary of the commands used:

CommandPurpose
ss -ltnpList all listening TCP connections with PIDs
netstat -ltnpLegacy alternative to ss
kill PIDSends SIGTERM signal (graceful stop)
kill -SIGKILL PIDSends SIGKILL signal (forceful stop)
killall nameKills all processes with the exact name
pkill nameKills processes matching the name pattern

Conclusion

Managing system resources effectively includes the ability to terminate unwanted processes. While killing a process can solve immediate problems such as a hanging service or a misconfigured application, it's essential to understand why the process was misbehaving to prevent future issues. Always approach process termination cautiously, ensuring that system stability and data integrity are not compromised.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design