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.