views:

788

answers:

7

netstat -tulnap shows me what ports are in use. How to free up a port, in Linux?

+6  A: 

Kill the process that is listening to the port in question. I believe netstat shows you process ids.

Gleb
+2  A: 

I think the only way will be to stop the process which has opened the port.

Tilo Prütz
+2  A: 

Stop/kill the process attached to it.

Tobias
+3  A: 

The "netstat --programs" command will give you the process information, assuming you're root. Then you will have to kill the "offending" process which may well start up again just to annoy you :-).

What are you actually trying to achieve here? Solutions will vary based on the processes holding those ports.

paxdiablo
+4  A: 

As the others have said, you'll have to kill all processes that are listening on that port. The easiest way to do that would be to use the fuser(1) command. For example, to see all of the processes listening for http requests on port 80 (run as root or use sudo):

# fuser 80/tcp

If you want to kill them, then just add the -k option.

uzi
A: 

I can write the below code to kill the process kill pidnumber

But it does not kill immediately it takes 1 minute to release the port. So how to release the port immediately?

I find your question disturbing. It is wrong to be killing processes to get hold of ports. You should be disabling their startup or modifying their config such that they use an alternative port.
Blank Xavier
Use `setsockopt(..., SOL_SOCKET, SO_REUSEADDR, ...)` if you want to bind to the port as soon as possible.
ephemient
+1  A: 

If you really want to kill a process immediately, you send it a KILL signal instead of a TERM signal (the latter a request to stop, the first will take effect immediately without any cleanup). It is easy to do:

kill -KILL <pid>

Be aware however that depending on the program you are stopping, its state may get badly corrupted when doing so. You normally only want to send a KILL signal when normal termination does not work. I'm wondering what the underlying problem is that you try to solve and whether killing is the right solution.

Paul de Vrieze