Skip to main content

List listening ports and the programs using them

Before you publish a new container port, check what is already listening. ss shows everything; this tidies the output into one line per protocol and port.

Quick one-liner

sudo ss -Htulnp | awk '{split($5,a,":"); port=a[length(a)]; print $1, port, $7}' | sort -k2,2n -u

Example output:

tcp 22 users:(("sshd",pid=812,fd=3))
tcp 443 users:(("docker-proxy",pid=1450,fd=4))
udp 53 users:(("dnsmasq",pid=990,fd=5))
  • -H drops the header. -t/-u select TCP and UDP, -l listening sockets only, -n numeric ports, and -p the process (needs sudo).
  • sort -u on the port column removes the duplicate IPv4 and IPv6 lines.

Older systems and NAS devices

Where ss is missing, use netstat -tulnp (net-tools). The columns differ: the local address is field 4 and the program is field 7.

Colour (optional)

If you add colour, sort first and colour last. Sorting text that already contains ANSI codes gives the wrong order. Also, match(str, re, arr) in awk is a GNU awk (gawk) feature; busybox and mawk don't have it.

Docker-specific view

docker ps --format '{{.Names}}\t{{.Ports}}' | column -t