brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: lsof -nP -iTCP -sTCP:LISTEN lists every service waiting for connections

Network and DNS
~/tips/tip-lsof-listening-ports

sudo lsof -nP -iTCP -sTCP:LISTEN

List every process that is listening on a TCP port.

Sometimes I do not know which port is the problem, I just want to know what on this machine is listening at all. lsof can answer that in one line.

Run it with sudo so it can see processes of other users:

sudo lsof -nP -iTCP -sTCP:LISTEN

-iTCP limits the list to TCP sockets and -sTCP:LISTEN keeps only the ones waiting for connections. -n stops lsof from looking up host names and -P shows port numbers instead of service names, so the output stays fast and exact. Each row shows the command, the PID, the user and the address with its port.

Swap -iTCP for -iUDP to see UDP sockets. A service listening on 127.0.0.1 is only reachable from the machine itself, while * means it accepts connections from the network.

A caveat: without sudo you only see your own processes, so a quiet list can be misleading.

One more detail: add -i :443 after the same options to narrow the list to a single port, or add -i4 to leave out IPv6 sockets. Keep in mind that this shows what is open, not what a firewall lets through.

Further reading: lsof(8) man page

More Network and DNS tips tcpdump -nn -r capture.pcap port 53 nmap --packet-trace -p 22,80 192.0.2.10 nmap -p 22,80,443 -oG scan.gnmap 192.0.2.0/24 wg genpsk > peer1.psk Table = off All Network and DNS tips (90) All topics

Link