lvella
lvella

Reputation: 13491

How to retrieve ports in use in the system?

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?

Upvotes: 2

Views: 1322

Answers (2)

Pavel Skipenes
Pavel Skipenes

Reputation: 379

List all listening processes and what port they are listening too. Running without sudo will list only user processes.

sudo ss -tlpn

explanation:

  • -t tcp
  • -l listening
  • -p show process
  • -n numeric, don't replace port numbers with their "popular" name.

Upvotes: 0

lik
lik

Reputation: 229

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

Upvotes: 5

Related Questions