What is netstat and how to inspect connections
· Category: Networking
Short answer
Netstat displays active network connections, listening ports, routing tables, and interface statistics. Modern Linux systems prefer ss for better performance.
Steps
-
List all connections: Run
netstat -anorss -tunapto view TCP and UDP sockets. -
Show listening ports: Use
netstat -tlnporss -tlnpto see which services are waiting for connections. -
Find process names: Add the
-pflag to map ports to process IDs and program names. -
View routing table: Use
netstat -rorip routeto inspect how traffic is routed. -
Filter by protocol: Append
-tfor TCP,-ufor UDP, or-xfor Unix sockets.
Tips
- Use
sson Linux; it is faster and more detailed than netstat. - Combine with
grepto find specific ports or IPs quickly. - Run with sudo to see processes owned by other users.
Common issues
- Ports in TIME_WAIT consuming resources.
- Unexplained listening ports indicating unauthorized services.
- Interpreting CLOSE_WAIT as an active connection rather than a cleanup state.