netstat
the classic socket listingnetstat lists network connections and listening ports. Like ifconfig it belongs to net-tools and has been replaced — by ss — but "netstat -tulpn" is in so many guides that it is worth knowing, and it exists on Windows and macOS as well.
netstat -tulpn and ss -tulpn answer the same question: what is listening?
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
t TCP, u UDP, l listening, p the owning process, n numbers instead of names.
0.0.0.0:22 means SSH accepts connections on all interfaces — reachable from the network.
127.0.0.1:3306 means MySQL accepts connections only from this machine. Nothing outside can reach it.
UDP is connectionless, so the State column is empty. This is the local DNS stub.
Switches
A switch changes how the command behaves. These are the ones used day to day; man netstat lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-t- TCP sockets.
-u- UDP sockets.
-l- Listening sockets only.
-p- Show the owning process.
-n- Numeric: no name lookups.
-r- Show the routing table.
-i- Show interface statistics.
-a- All sockets, listening and connected.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
ss -tulpnThe modern equivalent, with the same switches.
netstat -rnShows the routing table numerically.
netstat -iShows per-interface packet and error counters.
netstat -an | grep ESTABLISHEDLists the connections currently open.
In cybersecurity
Every listening port is a service an attacker can try.
Each line should be something you can name and justify. An unknown process listening on a high port is a classic sign of a backdoor.
A database or admin panel bound to 0.0.0.0 is exposed to the network. Bind it to localhost unless other machines truly need it.
Without root the process column is blank for other users’ sockets, and the most interesting information is missing.