Freeing up a TCP/IP port?

LinuxNetworkingTcpPort

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

To kill a specific port in Linux use the below command

sudo fuser -k Port_Number/tcp

replace Port_Number with your occupied port.

Solution 3 - Linux

In terminal type :

netstat -anp|grep "port_number"

It will show the port details. Go to last column. It will be in this format . For example :- PID/java

then execute :

kill -9 PID

For MAC:

lsof -n -i :'port-number' | grep LISTEN

Sample Response :

java   4744 (PID)  test  364u  IP0 asdasdasda   0t0  TCP *:port-number (LISTEN)

and then execute :

kill -9 PID 

Worked on Macbook

Solution 4 - Linux

You can use tcpkill (part of the dsniff package) to kill the connection that's on the port you need:

sudo tcpkill -9 port PORT_NUMBER

Solution 5 - Linux

To check all ports:

netstat -lnp

To close an open port:

fuser -k port_no/tcp

Example:

fuser -k 8080/tcp

In both cases you can use the sudo command if needed.

Solution 6 - Linux

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

Depending on what you're actually trying to achieve, solutions to that problem will vary based on the processes holding those ports. For example, you may need to disable services (assuming they're unneeded) or configure them to use a different port (if you do need them but you need that port more).

Solution 7 - Linux

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

Solution 8 - Linux

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.

Solution 9 - Linux

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

Solution 10 - Linux

sudo killall -9 "process name"

Solution 11 - Linux

Shutting down the computer always kills the process for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbapiView Question on Stackoverflow
Solution 1 - LinuxuziView Answer on Stackoverflow
Solution 2 - LinuxAnil ChahalView Answer on Stackoverflow
Solution 3 - Linuxuser2332505View Answer on Stackoverflow
Solution 4 - LinuxAlexTView Answer on Stackoverflow
Solution 5 - LinuxVinayakView Answer on Stackoverflow
Solution 6 - LinuxpaxdiabloView Answer on Stackoverflow
Solution 7 - LinuxGlebView Answer on Stackoverflow
Solution 8 - LinuxPaul de VriezeView Answer on Stackoverflow
Solution 9 - LinuxTilo PrützView Answer on Stackoverflow
Solution 10 - LinuxZonxwedopView Answer on Stackoverflow
Solution 11 - LinuxZonxwedopView Answer on Stackoverflow