Finding Open Sockets

What is a socket?

 

A socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process) into a single identity, much like one end of a telephone connection is the combination of a phone number and a particular extension. Here are few commands based on lsof to list the open sockets in linux.

 

Note : Lsof is a program use to list the list of open sockets files etc. Use yum install lsof to install the packages.

 

To list all the open files/sockets (login as root)

 

  • # lsof
  • # watch lsof -i

 

You can also redirect the results to a file using lsof > filename.txt

 

To list all open Internet files

 

  • # lsof -i U

 

This is particularly useful to find which process is eating must sockets if you are getting ” Outof socket memory error

 

To know about the specific port for example TCP/UDP port 80

 

  • # lsof -i tcp:80
    # lsof -i udp:80

 

To list the filesystem usage (for example on /var partition)

 

  • # lsof /var

 

The above will be useful to unmount the disk.

 

To find the overall socket status on Linux (ipv4 and ipv6)

 

  • # cat /proc/net/sockstat
    # cat /proc/net/sockstat6

 

Socket Buffer size can be well optimized by tuning the tcp default/rmem/wmem limits. More on http://www.cyberciti.biz/faq/linux-tcp-tuning/