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

- 16,038
- 10
- 74
- 104
11 Answers
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.
-
3I found that sending a request to the port also cleans it (i am no linux expert though) – Matej Jan 18 '14 at 16:31
-
3To install fuser on Debian: sudo apt-get install psmisc (http://bitflop.com/document/107) – Korneel Mar 18 '14 at 16:30
-
1It worked, but I had to install psmisc too on CentOs 7 (`sudo yum install psmisc`) – Marlon Bernardes Jan 05 '15 at 20:59
-
5`kill -9 $(fuser 80/tcp 2>/dev/null)` – Hanynowsky Feb 11 '16 at 08:34
To kill a specific port in Linux use the below command
sudo fuser -k Port_Number/tcp
replace Port_Number
with your occupied port.

- 2,437
- 2
- 17
- 27

- 2,544
- 2
- 22
- 19
-
12This actually kills the process that opened the port and not the port itself. – vinay chilakamarri May 26 '16 at 18:02
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

- 4,580
- 1
- 24
- 21

- 619
- 2
- 10
- 19
-
1obviously this doesn't work if the PID column is empty for that port – Anentropic Sep 23 '16 at 17:08
-
3...and that happens if you don't have permission to see the process... try `sudo netstat` to actually see the PIDs :) – Anentropic Sep 23 '16 at 17:25
-
I 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. – vtechmonkey Oct 11 '17 at 20:07
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.
-
BEWARE: 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. – Owl Sep 08 '21 at 00:51
-
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

- 1,145
- 10
- 11
-
12this just hangs `$ sudo tcpkill -9 port 5432 tcpkill: listening on lxcbr0 [port 5432]` – Anentropic Sep 23 '16 at 17:06
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).

- 854,327
- 234
- 1,573
- 1,953
Kill the process that is listening to the port in question. I believe netstat shows you process ids.

- 2,404
- 1
- 13
- 7
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.

- 4,888
- 1
- 24
- 29
-
This did not resolve my issue with ssh port forwarding. [`sudo fuser -k Port_Number/tcp`](https://stackoverflow.com/a/750705/4575793) did. – Cadoiz Dec 08 '21 at 12:56
I think the only way will be to stop the process which has opened the port.

- 1,766
- 3
- 16
- 27