203

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

informatik01
  • 16,038
  • 10
  • 74
  • 104

11 Answers11

277

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.

hmatar
  • 2,437
  • 2
  • 17
  • 27
uzi
  • 5,085
  • 2
  • 16
  • 11
117

To kill a specific port in Linux use the below command

sudo fuser -k Port_Number/tcp

replace Port_Number with your occupied port.

hmatar
  • 2,437
  • 2
  • 17
  • 27
Anil Chahal
  • 2,544
  • 2
  • 22
  • 19
36

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

alexkr
  • 4,580
  • 1
  • 24
  • 21
user2332505
  • 619
  • 2
  • 10
  • 19
  • 1
    obviously 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
20

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.

Ferdi
  • 540
  • 3
  • 12
  • 23
Vinayak
  • 439
  • 4
  • 10
  • 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
  • Duplicate to the first two answers. – Cadoiz Dec 08 '21 at 12:53
19

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
AlexT
  • 1,145
  • 10
  • 11
11

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).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
6

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

Gleb
  • 2,404
  • 1
  • 13
  • 7
1

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
  • 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
-2

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

Tilo Prütz
  • 1,766
  • 3
  • 16
  • 27
-8

sudo killall -9 "process name"

Zonxwedop
  • 387
  • 1
  • 3
  • 12
-14

Shutting down the computer always kills the process for me.

Zonxwedop
  • 387
  • 1
  • 3
  • 12