Have you encountered the “Address already in use” error when starting a service? This occurs when another process occupies the required port. Freeing up the port is crucial for smooth system operation, especially with Apache, Nginx, MySQL, PostgreSQL, and Docker. This guide shows how to kill process on port using commands like lsof, ss, netstat, and fuser.
Common Scenarios Where Port Conflicts Occur
- Starting a web server (Apache, Nginx, or Node.js) and finding the port already in use
- Launching a database (MySQL, PostgreSQL, or MongoDB) and encountering a binding error
- Deploying a containerized application where a process still runs on the host machine
Identify the Process ID Using the Port Number
Before you can kill process on port, identify the Process ID (PID). The PID is a unique number assigned to each running program. Identifying this number prevents accidental termination of unrelated processes.
Using lsof (List Open Files Command)
The lsof command lists open files and network connections. Run this command to find which process occupies a specific port:
lsof -i :PORT_NUMBER
Replace PORT_NUMBER with your actual port (example: 8080).
The output displays:
| Column | Description |
|---|---|
| COMMAND | Program name |
| PID | Process identifier needed for termination |
| USER | Account running the program |
| TYPE | Connection protocol (IPv4 or IPv6) |
lsof using your package manager if not already present. On Ubuntu/Debian: sudo apt install lsof
Using ss (Socket Statistics Command)
The ss command is a modern replacement for netstat. It provides faster execution and more detailed output. Run this command:
ss -tulnp | grep PORT_NUMBER
This shows the process listening on your specified port with PID and process name.
Using netstat (Older Method)
While deprecated in some distributions, netstat remains useful:
netstat -tulnp | grep PORT_NUMBER
The final column displays the PID you need.
Methods to Kill Process on Port Using Its PID
Once you identify the PID, terminate it using one of these commands.
Using kill (Graceful Termination)
The kill command terminates processes in Linux. By default, it sends SIGTERM (signal 15), allowing the process to save data before exiting:
kill PID
This method allows proper shutdown and cleanup.
Using fuser to Kill Process on Port
The fuser command directly finds and terminates processes on a specific port:
fuser -k -n tcp PORT_NUMBER
The -k flag kills the process, and -n tcp specifies the TCP protocol. Use -n udp for UDP ports. This one-liner eliminates manual PID searching.
Using lsof to Kill Process on Port
Combine lsof with kill for instant termination:
kill -9 $(lsof -t -i :PORT_NUMBER)
This command extracts the PID and sends SIGKILL for immediate termination.
kill -9 only when standard termination fails. The forceful signal prevents cleanup operations.
Verify That the Port is Free
After termination, confirm the port is available. Run the identification command again:
lsof -i :PORT_NUMBER
Empty output confirms successful termination. The port now accepts new connections.
Troubleshooting Common Issues
Permission Denied
System processes require elevated privileges. Add sudo before commands on Linux:
sudo kill -9 PID
On Windows, run Command Prompt as Administrator.
Process Keeps Restarting
Some services restart through system managers. Stop these using service commands:
sudo systemctl stop SERVICE_NAME
Examples:
- Apache:
sudo systemctl stop apache2 - Nginx:
sudo systemctl stop nginx - MySQL:
sudo systemctl stop mysql
Process Not Found
If the process is not listed, it may have already exited. Another application might have replaced it. Run the identification command again to check current port usage.
Conclusion
Knowing how to kill process on port is essential for managing server applications and troubleshooting conflicts. Whether you use lsof, ss, fuser, or netstat, these commands help you quickly identify and terminate unwanted processes. Master these techniques to maintain smooth system operations and resolve port conflicts efficiently.
FAQs
Run lsof -i :8080 to find the PID, then execute kill -9 PID. Alternatively, use fuser -k -n tcp 8080 for a one-line solution.
Use fuser -k -n tcp PORT_NUMBER to kill all processes on the specified port instantly without manual PID identification.
Run lsof -i :PORT_NUMBER or ss -tulnp | grep PORT_NUMBER. Empty output confirms the port is now available for new connections.
System processes require elevated privileges. Prefix your command with sudo (example: sudo kill -9 PID) to gain necessary permissions for termination.
kill sends SIGTERM for graceful termination, allowing cleanup. kill -9 sends SIGKILL for immediate termination without cleanup. Use graceful termination first.