Introduction
netstat is a command-line network utility that displays network connections for Transmission Control Protocol (TCP), routing tables, and a number of network interface and network protocol statistics.
The following cheatsheet goes over a few common netstat commands. For more advanced commands you need to consult the official man pages.
https://linux.die.net/man/8/netstat
# To view which users/processes are listening to which ports:
sudo netstat -lnptu
# To view routing table (use -n flag to disable DNS lookups):
netstat -r
# Which process is listening to port <port>
netstat -pln | grep <port> | awk '{print $NF}'
# Example output: 1507/python
# Fast display of ipv4 tcp listening programs
sudo netstat -vtlnp --listening -4
# Displays network-related information such as open connections, open socket ports, etc.
# List all ports:
netstat -a
# List all listening ports:
netstat -l
# List listening TCP ports:
netstat -t
# Display PID and program names:
netstat -p
# List information continuously:
netstat -c
# List routes and do not resolve IP to hostname:
netstat -rn
# List listening TCP and UDP ports (+ user and process if you're root):
netstat -lepunt
# Print the routing table:
netstat -nr