Closing Listened Ports on Linux and Windows
When a process terminates unexpectedly, it can leave a port in a LISTEN
state, preventing other applications from using it. This can be frustrating, especially during development or when encountering network connectivity issues. This tutorial explains how to identify and close these lingering ports on both Linux and Windows operating systems.
Understanding Port States
Before diving into the solutions, it’s helpful to understand the LISTEN
state. A port in this state indicates that a process is actively waiting for incoming connections on that specific port. If the process crashes or exits without properly releasing the port, it remains in the LISTEN
state until the operating system reclaims it (which can take time).
Identifying the Process Using the Port
The first step is to identify the process that is currently bound to the port.
On Linux:
Open a terminal and use the following command:
netstat -tulnp | grep :<port_number>
Replace <port_number>
with the actual port number you want to investigate.
-t
: Shows TCP connections.-u
: Shows UDP connections.-l
: Shows listening sockets.-n
: Displays numerical addresses (avoids DNS lookups).-p
: Displays the process ID (PID) and program name.
The output will show you the PID and the name of the process using the port. For example:
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1234/node
This indicates that process with PID 1234, named ‘node’, is listening on port 8080.
On Windows:
Open Command Prompt or PowerShell and use the following command:
netstat -ano | findstr :<port_number>
Replace <port_number>
with the port you want to investigate.
The output will be similar to:
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1234
This shows that process with PID 1234 is listening on port 8080.
Closing the Port
Once you’ve identified the PID, you can terminate the process and release the port.
On Linux:
Use the kill
command:
kill <PID>
Replace <PID>
with the process ID. If the process doesn’t terminate with the standard kill
command, you can use the -9
option to force termination (use with caution, as it doesn’t allow the process to clean up resources gracefully):
kill -9 <PID>
On Windows:
Use the taskkill
command:
taskkill /F /PID <PID>
/F
: Forces the termination of the process./PID <PID>
: Specifies the process ID.
Example
Let’s say you find that process with PID 1234 is using port 8080.
On Linux:
kill 1234
or (if necessary)
kill -9 1234
On Windows:
taskkill /F /PID 1234
After running the appropriate command, verify that the port is released by running the netstat
command again. You should no longer see any process listening on that port.
WSL (Windows Subsystem for Linux) Consideration
If you are using WSL, the process might be running within the WSL environment. To find the PID within WSL, use:
sudo lsof -t -i:<port_number>
Then, use sudo kill -9 <PID>
to terminate the process within WSL.
Best Practices
- Graceful Termination: Whenever possible, attempt to terminate the process gracefully (without
-9
or/F
) to allow it to clean up resources. - Understand the Process: Before terminating a process, understand what it does to avoid unintended consequences.
- Restart the Application: After terminating the process, consider restarting the application to ensure it’s running correctly.