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.
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:
Replace PORT with the actual port number. Here, the flags used are:
-lfor only services that are listening.-tfor TCP connections.-nto show numerical addresses.-pto 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:
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:
Or:
Forcibly Killing the Process
If the process doesn't respond to SIGTERM, use SIGKILL, which forces the process to stop immediately:
Using killall and pkill
If you prefer using the process name instead of the PID:
killallkills processes by name, e.g.,killall nginx.pkillcan 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:
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:
| Command | Purpose |
| ss -ltnp | List all listening TCP connections with PIDs |
| netstat -ltnp | Legacy alternative to ss |
| kill PID | Sends SIGTERM signal (graceful stop) |
| kill -SIGKILL PID | Sends SIGKILL signal (forceful stop) |
| killall name | Kills all processes with the exact name |
| pkill name | Kills 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
- How to kill off a pending APM operation
- How to know a Pod's own IP address from inside a container in the Pod?
- How to know if a docker container is running in privileged mode
- How to know if docker is already logged in to a docker registry server
- How to kill all processes with a given partial name?
- How to kill pods on Kubernetes local setup
- How to know more details about a previous rollout revision using kubectl?
- How to know RDS free storage

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.