Freeing up a TCP/IP port?
LinuxNetworkingTcpPortLinux 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.