How to close or (unbind?) a port left open by a rogue application?

Posted on

Problem :

I am using Fedora 13 64bit. My eclipse program starts a server on port 9050 but I shutdown the program, eclipse fails to properly shutdown the server. Restarting eclipse has no effect.

How can I close or unbind a port forcibly from command line?

I know rebooting or log off and back on will clear the ports but this much slower process for me. I am looking for forcibly closing the port in command line.

Solution :

The only way to forcefully close a listening port is to kill the process that is listening on it. Use lsof, netstat, fuser – as root – to find out the process ID. Once the process exits (by itself or by being killed), the kernel will automatically clean up all sockets it had open. (However, there might be cases that programs will be denied listening on the same port for a few minutes; in this case all you can do is wait.)

lsof -n -i

Find the process you’re looking for and kill it.

From lsof manual:

-n This option inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host name lookup is not working properly.

-i [i] This option selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.

Same answer to this question I think.

You can user the fuser command with the -k switch.

For example, to close port 111, you can do the following: fuser -k 111/tcp
(this also saves you the hassle of find the ID of the process to kill it as fuser does in implicitly)

Note that some processes restart automatically once closed. For that, you need to check each process’s config file to come up with an appropriate workaround.

I had the same issue, just look for the process whicj is listening that port using cmd command on windows

netstat -a -b

then you can shut it down with tasks manager

Leave a Reply

Your email address will not be published. Required fields are marked *