Saturday 22 January 2022

Open Ports

On several occasions lately I've not been able to start a server application cause I already had another "forgotten" process listening on that port. I'll write here the steps to locate and kill that initial process.

Windows. I want to kill the process listening on port 3000. Using standard Windows tools (no need to install some bash version or anything) we can use "netstat -ano" to list TCP and UDP listening ports and established connections, and then do some filtering on the output, like this:
netstat -ano | fidstr LISTENING | findstr 3000 to get the process ID
and then taskkill /F /PID 12345 to kill the process (with PID 12345)

Linux. netstat is considered deprecated in most Linux distros and we should use the ss command instead.
We can use "ss -ltunp" to list TCP and UDP listening ports, and then do some filtering on the output, like this:
ss -ltunp | grep LISTEN | grep 3000 to get the process ID
and then kill -9 12345 to kill the process (with PID 12345)

I had almost forgotten that UDP is a connectionless protocol and somehow this seems to have motivated netstat and ss authors to display a different State value for listening UDP ports. netstat shows TCP listening ports as "LISTENING" status, and UDP ports as empty. ss shows TCP listening ports as "LISTEN" and UDP ports as "CONN".

No comments:

Post a Comment