Freeing up a TCP/IP port?
Solution 1
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
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
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
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
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.
Related videos on Youtube
![informatik01](https://i.stack.imgur.com/Pub1I.png?s=256&g=1)
informatik01
I work as a senior software developer at French-Estonian IT and Telecommunications company Onoff Telecom. My main interests are Java, the related tools and frameworks (especially Spring Framework), and server side development in general. I love Computer Science. I am interested in learning and gaining more knowledge about programming languages, frameworks, platforms, techniques, algorithms and other Computer Science related topics. I find Stack Overflow a fantastic place to learn and find solutions to programming problems.
Updated on April 12, 2022Comments
-
informatik01 about 2 years
netstat -tulnap
shows me what ports are in use. How to free up a port in Linux? -
Matej over 10 yearsI found that sending a request to the port also cleans it (i am no linux expert though)
-
Korneel over 10 yearsTo install fuser on Debian: sudo apt-get install psmisc (bitflop.com/document/107)
-
waqas over 9 yearsshutting down a server machine is rare.
-
Marlon Bernardes over 9 yearsIt worked, but I had to install psmisc too on CentOs 7 (
sudo yum install psmisc
) -
jplandrain over 9 yearsdid you get your "funniest" badge ?
-
user1747935 almost 9 yearsnetstat -anp|grep <port> the last column has the process
-
Anil Chahal almost 9 yearsNo need for shutting down your computer.
-
Valentin Roudge over 8 yearsBest idea ever 10/10
-
Hanynowsky over 8 years
kill -9 $(fuser 80/tcp 2>/dev/null)
-
vinay chilakamarri about 8 yearsThis actually kills the process that opened the port and not the port itself.
-
Anentropic almost 8 yearsunlike some of the other answers, this will surely work
-
Anentropic almost 8 yearsthis just hangs
$ sudo tcpkill -9 port 5432 tcpkill: listening on lxcbr0 [port 5432]
-
Anentropic almost 8 yearsobviously this doesn't work if the PID column is empty for that port
-
Anentropic almost 8 years...and that happens if you don't have permission to see the process... try
sudo netstat
to actually see the PIDs :) -
vtechmonkey over 6 yearsI was trying to kill a port on an amazon ec2 instance via putty cli. Forever said it had no processes running but the port(4200 for an angular app) was still open.This is the only command that worked for me.
-
Owl almost 3 yearsBEWARE: fuser kills the process not the port. Which means if the port's stuck in limbo, can it work at all? This is not the right answer.
-
Cadoiz over 2 yearsDuplicate to the first two answers.
-
Cadoiz over 2 yearsThis did not resolve my issue with ssh port forwarding.
sudo fuser -k Port_Number/tcp
did.