How to retrieve ports in use in the system?

0

Issue

In a Linux specific way, how can I get the list of all open UDP ports and all TCP ports currently accepting connections in some interface?

Solution

The most common way is to use netstat console utility with the following flags:

netstat -plan

where:

-p : Show the PID and name of the program to which each socket belongs;
-l : Show only listening sockets;
-a : Show both listening and non-listening sockets;
-n : Show numerical addresses instead of trying to determine symbolic host, port or user names.

For additional output options and flags please check man pages man netstat. Based on your particular needs, only TCP or UDP (for example) protocol connections can be examined:

netstat -4 --tcp --udp --all

Alternatively, lsof -i might be helpful.

Most likely you are interested in the following information (special /proc filesystem):

/proc – Mount point for the proc filesystem, which gives access to kernel status information via the following files:

  • /proc/net/dev – device information
  • /proc/net/raw – raw socket information
  • /proc/net/tcp – TCP socket information
  • /proc/net/udp – UDP socket information
  • /proc/net/igmp – IGMP multicast information
  • /proc/net/unix – Unix domain socket information
  • /proc/net/ipx – IPX socket information
  • /proc/net/ax25 – AX25 socket information
  • /proc/net/appletalk – DDP (appletalk) socket information
  • /proc/net/nr – NET/ROM socket information
  • /proc/net/route – IP routing information
  • /proc/net/ax25_route – AX25 routing information
  • /proc/net/ipx_route – IPX routing information
  • /proc/net/nr_nodes – NET/ROM nodelist
  • /proc/net/nr_neigh – NET/ROM neighbours
  • /proc/net/ip_masquerade – masqueraded connections
  • /proc/net/snmp – statistics

Answered By – lik

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More