Monday, August 22, 2011

How to close, open ports in linux


If you want to close the port 8080, this is an one way of doing this, if you are on ubuntu.

type following command:
> netstat -lpn

This will list all listening ports


Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:49027           0.0.0.0:*               LISTEN      13098/java    
tcp        0      0 127.0.0.1:9160          0.0.0.0:*               LISTEN      13098/java    
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -            
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      13098/java    
tcp        0      0 0.0.0.0:33395           0.0.0.0:*               LISTEN      13098/java    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -            
tcp        0      0 127.0.0.1:7000          0.0.0.0:*               LISTEN      13098/java  


Use grep for filtering out the 8080 port.

You can use following command:
> netstat -lpn | grep 8080

You'll get output something like this

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      13098/java

Here my process id is 13098 and it is the process that is using port 8080

Kill the process using following command:
> sudo kill 13098

Now port 8080 is free.

7 comments:

  1. Great!! it helped me at this hour :)

    ReplyDelete
  2. thanxx a lot it worked very much :)

    ReplyDelete
  3. No return message but the process is still there or port is still open

    ReplyDelete
  4. Hey Shared courner, you can force kill the process using that port by
    kill -9
    Try that out!

    ReplyDelete
  5. Thank you But how do you Keep it From Starting again on Start up?

    ReplyDelete
  6. Thank you my server went coco and I had to restart manuly, sadly when I turn it back on it tells me the port is in use. YOU SAVED ME FROM NOT BEING ABLE TO USE THAT PORT EVER AGAIN. Thanks

    ReplyDelete