Close open ports manually in ubuntu

By | September 15, 2012

Open ports

When doing a lot of socket programming, it often happens that a server program when recompiled/rerun fails to bind to a particular port number because that port number is already in use.

To close the port number manually first the process name/id has to be found out that is holding the port open and then use the kill command on that process.

lsof

$ lsof -i :8888
COMMAND  PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    8461 enlightened   11u  IPv6 138527      0t0  UDP *:8888

In the above example it is seen that port 8888 is being held in use by the command java with pid 8461.
Now kill the process by doing any of the following

$ kill 8461
$ killall -9 8461
$ killall -9 java

netstat

The netstat command can also be used to find out which process is holding a certain port number

$ netstat -u -ap
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp        0      0 *:18347                 *:*                                 -
udp        0      0 localhost:11211         *:*                                 -
udp        0      0 localhost:36254         localhost:36254         ESTABLISHED -
udp        0      0 localhost:domain        *:*                                 -
udp        0      0 *:ipp                   *:*                                 -
udp        0      0 *:42038                 *:*                                 -
udp        0      0 *:17500                 *:*                                 4090/dropbox
udp        0      0 *:mdns                  *:*                                 -
udp        0      0 localhost:58797         localhost:7777          ESTABLISHED 9831/ncat
udp        0      0 localhost:42724         localhost:domain        ESTABLISHED -
udp6       0      0 [::]:46282              [::]:*                              -
udp6       0      0 [::]:mdns               [::]:*                              -
udp6       0      0 [::]:9999               [::]:*                              11598/java

The port we want to close here is 9999. And netstat shows that the pid = 11598 and command name = java
Over here we used the -u for udp port. If its a tcp port then the u switch is not needed.

$ sudo netstat -ap | grep :9050
tcp        0      0 localhost:9050          *:*                     LISTEN      1613/tor

Once the process id/name is found end it with the kill command.

$ kill 11598

fuser

The fuser command can also be used to find out the pid of the program. The sytanx is

fuser -k -n protocol portno

Quick example

$ fuser -k -n udp 7777
7777/udp:            11774
About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *