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

19

3

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.

Mark

Posted 2011-07-07T12:05:01.157

Reputation: 307

Answers

10

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.)

user1686

Posted 2011-07-07T12:05:01.157

Reputation: 283 655

19

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.

aVeRTRAC

Posted 2011-07-07T12:05:01.157

Reputation: 461

7Throw on | grep ":9050" (or whatever port number) to easily filter the output – isaacparrot – 2014-06-10T00:28:25.097

9

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.

tony9099

Posted 2011-07-07T12:05:01.157

Reputation: 303

-3

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

daqop

Posted 2011-07-07T12:05:01.157

Reputation: 1

2

Sorry, I downvoted. You mention "on Windows". The "-b" parameter to netstat, which shows executable names, is specific to Microsoft Windows. (Unix netstat variations may have similar functionality, but is typically not using that same parameter.) However, the question said "Fedora", so the asker of this question was looking for information about Fedora 13 64bit, a different operating system. netstat's man page for Fedora indicates that using -p shows the PID (which can be used to identify the executable's name).

– TOOGAM – 2016-07-02T11:14:46.357

1There are several mistakes in this answer. The answer contains a grammatical error which could have been avoided. The answer also is not applicable to Fedora, which is clearly mentioned, in the first sentence of the question. It isn't like the question didn't contain this information when this answer was submitted 30 minutes ago, the question has not been edited since July 2011, which nearly 5 years ago. So I am taking this answer for what it is, a "me too" answer, which very rarely actually helpful. – Ramhound – 2016-07-02T11:36:50.940