How to list and close open ports in linux

This guide will be demonstrating you how to get the status of the ports and to free up a port which is in use.
(Note : Following commands were tested on a ubuntu 11.10 installed pc)


List down all listening ports
netstat -lpn

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s