Freeing up a TCP/IP port?

373,210

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.

Share:
373,210

Related videos on Youtube

informatik01
Author by

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, 2022

Comments

  • informatik01
    informatik01 about 2 years

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

  • Matej
    Matej over 10 years
    I found that sending a request to the port also cleans it (i am no linux expert though)
  • Korneel
    Korneel over 10 years
    To install fuser on Debian: sudo apt-get install psmisc (bitflop.com/document/107)
  • waqas
    waqas over 9 years
    shutting down a server machine is rare.
  • Marlon Bernardes
    Marlon Bernardes over 9 years
    It worked, but I had to install psmisc too on CentOs 7 (sudo yum install psmisc)
  • jplandrain
    jplandrain over 9 years
    did you get your "funniest" badge ?
  • user1747935
    user1747935 almost 9 years
    netstat -anp|grep <port> the last column has the process
  • Anil Chahal
    Anil Chahal almost 9 years
    No need for shutting down your computer.
  • Valentin Roudge
    Valentin Roudge over 8 years
    Best idea ever 10/10
  • Hanynowsky
    Hanynowsky over 8 years
    kill -9 $(fuser 80/tcp 2>/dev/null)
  • vinay chilakamarri
    vinay chilakamarri about 8 years
    This actually kills the process that opened the port and not the port itself.
  • Anentropic
    Anentropic almost 8 years
    unlike some of the other answers, this will surely work
  • Anentropic
    Anentropic almost 8 years
    this just hangs $ sudo tcpkill -9 port 5432 tcpkill: listening on lxcbr0 [port 5432]
  • Anentropic
    Anentropic almost 8 years
    obviously this doesn't work if the PID column is empty for that port
  • Anentropic
    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
    vtechmonkey over 6 years
    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.
  • Owl
    Owl almost 3 years
    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.
  • Cadoiz
    Cadoiz over 2 years
    Duplicate to the first two answers.
  • Cadoiz
    Cadoiz over 2 years
    This did not resolve my issue with ssh port forwarding. sudo fuser -k Port_Number/tcp did.