This guide shows you how to find and terminate a Linux process that is occupying a specific port, such as when a service fails to start because the port is already in use. You’ll learn the exact commands to identify the process ID (PID) from a port number and safely kill it, along with common pitfalls and troubleshooting tips.
What You’ll Need
- A Linux system with a terminal (bash, zsh, etc.)
- Root or sudo privileges (required to kill processes owned by other users)
- The port number you want to free (e.g., 8080)
- Basic familiarity with the command line
Step-by-Step
- Identify the port number. Confirm the exact port you need to free. It could be a TCP or UDP port. For example,
8080is commonly used by web servers. - Find the PID using the port. Use one of these commands to list the process bound to the port. Replace
8080with your port number.lsof -i :8080– shows the PID and process name.fuser 8080/tcp– shows the PID(s) using the TCP port.ss -ltnp | grep ':8080'– modern alternative to netstat, shows listening processes.netstat -ltnp | grep ':8080'– older but still works on many systems.
The output will include a PID column. Note it down (e.g.,
1234). If no output appears, the port is not in use. - Verify the process details. Before killing, confirm the PID is what you expect. Run
ps -p 1234 -fto see the full command. This prevents accidentally killing an unrelated process. - Kill the process. Use the
killcommand with the PID. Start with a graceful termination:kill 1234
If the process doesn’t stop after a few seconds, escalate to a forceful kill:
kill -9 1234
The
-9signal (SIGKILL) cannot be ignored by the process and will terminate it immediately. - Confirm the port is free. Re-run the command from step 2. If nothing appears, the port is now available. For example:
lsof -i :8080should return no output. - Restart your service. Now you can start your application or service that needed the port.
Common Mistakes
- Killing the wrong process. Always verify the PID with
psbefore running kill. Ports can be reused by different processes over time. - Using the wrong signal.
kill -9should be a last resort. It doesn’t allow the process to clean up resources (like closing files or releasing locks), which can cause data corruption. - Ignoring UDP ports. If your service uses UDP (e.g., DNS on port 53), use
lsof -iUDP:53orfuser 53/udp. - Forgetting sudo. If the process runs under another user (like root), you’ll get a permission error. Prefix the command with
sudo. - Confusing PID with port number. The output of
lsofcan be confusing; the PID is in the second column, not the port number.
Tips
- Use
fuser -kto kill directly. If you’re confident,fuser -k 8080/tcpwill kill the process without needing a separate PID step. - Check for multiple processes. A port can have multiple connections (e.g., established connections).
lsof -i :8080will show all of them; you may need to kill several PIDs. - Use
ssovernetstat.ssis faster and more modern;netstatmay not be installed by default on newer distributions. - Consider using
systemctlif it’s a service. If the process is managed by systemd, usesystemctl stop service-nameinstead of killing the PID directly. This ensures proper cleanup. - Remember that ports can be in TIME_WAIT state. After killing a process, the port may remain in TIME_WAIT for a short while. This is normal and will clear on its own.
Troubleshooting
- No output from lsof/ss/netstat. The port might be open but not listening (e.g., an outbound connection). Use
lsof -i :8080without the-tflag to see all connections. If still nothing, the port is free. - Permission denied when killing. Run the kill command with
sudo. If you’re already root, check if the process is in an uninterruptible sleep state (D state) – it may not respond to SIGKILL until it finishes I/O. - Port still shows as used after kill. Wait a few seconds and re-check. If it persists, use
ss -tan | grep :8080to see if it’s in TIME_WAIT. If it’s a zombie process, you may need to kill its parent. - Killing the PID doesn’t free the port. The process might have spawned child processes that still hold the port. Use
pkill -f 'pattern'to kill all processes matching a command-line pattern, or usefuser -kwhich targets all processes on that port. - Command not found (lsof, fuser). Install them: on Debian/Ubuntu use
sudo apt install lsof psmisc; on RHEL/CentOS usesudo yum install lsof psmisc.
Frequently Asked Questions
What is the difference between kill and kill -9?
kill sends SIGTERM, which asks the process to terminate gracefully. kill -9 sends SIGKILL, which forces immediate termination and cannot be caught or ignored. Use SIGTERM first, then SIGKILL if the process doesn’t stop.
How do I find the PID of a process on a specific port without installing anything?
Use ss -ltnp | grep :port – ss is included in most modern Linux distributions. If ss isn’t available, try netstat -ltnp (often part of net-tools).
Can I kill a process on a port as a non-root user?
Only if the process is owned by your user. If it’s owned by root or another user, you’ll need sudo privileges. Use sudo lsof -i :port to see the owner.
What if the port is used by multiple processes?
That’s normal for TCP connections. lsof will list all PIDs. You need to kill the process that is listening (usually the one with the LISTEN state) to free the port for new connections. Killing established connections may not be necessary.
Is it safe to kill a process using a port?
It’s safe if you know the process is not critical. Always verify with ps -p PID -f to see the command. Killing a database or web server will cause downtime, so plan accordingly.
How do I kill a process on a port on a remote server?
SSH into the server first, then run the same commands. For example: ssh user@server then lsof -i :8080 and kill PID.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Cool Down an Overheating Laptop
- Good Password: How to Create One
- Remove Write Protection on External Hard Drive
- How to Disable Chkdsk at Windows Startup
- Restore WiFi and Secure Your Netgear Router After Reset
- Fix Error 0x80004005: Top Easy Ways That Work