Linux netstat Command
The netstat
(network statistics) command in Linux is a network management tool used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. It is helpful for diagnosing network issues, monitoring active network connections, and understanding network traffic behavior.
Note that netstat
has been deprecated in favor of the ss
command on some newer Linux distributions, but it is still widely used.
Basic Syntax of netstat
:
- options: Various flags to control the type of information you want to display.
Commonly Used netstat
Options
-a
: Show all sockets (both listening and non-listening).- Displays all active connections and the listening ports.
- Example:
Sample Output:
LISTEN
: The socket is listening for incoming connections.ESTABLISHED
: The connection is active.
-t
: Show TCP connections only.- Filters and displays only TCP connections.
- Example:
Sample Output:
-u
: Show UDP connections only.- Filters and displays only UDP connections.
- Example:
Sample Output:
-l
: Show only listening sockets.- Displays only the sockets that are in a
LISTEN
state (waiting for incoming connections). - Example:
Sample Output:
- Displays only the sockets that are in a
-n
: Show numerical addresses instead of resolving hostnames.- By default,
netstat
will resolve IP addresses to hostnames, but with-n
, it will display raw IP addresses and port numbers. - Example:
Sample Output:
- By default,
-r
: Display the routing table.- Shows the routing table, which contains information about how packets are routed within the network.
- Example:
Sample Output:
-i
: Show network interface statistics.- Displays information about network interfaces, including the number of packets received, transmitted, and errors.
- Example:
Sample Output:
-p
: Show PID and program name (if available).- This will display the Process ID (PID) and the name of the program associated with each connection.
- Example:
Sample Output:
-a
+-n
: Display all connections with numerical addresses.- Example:
Sample Output:
- Example:
-s
: Display network statistics by protocol.- This option displays the statistics for each network protocol (TCP, UDP, ICMP, etc.).
- Example:
Sample Output:
Example Scenarios
Display all network connections and listening ports:
Show all active TCP connections with program names:
View the routing table:
Display network interface statistics:
Show all connections with numeric IPs and ports:
Check protocol-specific statistics (e.g., TCP):
Summary of Common netstat
Options:
Option | Description |
---|---|
-a | Show all sockets (listening and non-listening) |
-t | Show only TCP connections |
-u | Show only UDP connections |
-l | Show only listening sockets |
-n | Show numerical addresses (no DNS lookup) |
-r | Display the routing table |
-i | Display interface statistics |
-p | Show PID and program name for each connection |
-s | Display statistics by protocol |
-c | Continuously update the output (useful for monitoring) |
Conclusion
The netstat
command is a powerful tool for monitoring and troubleshooting network-related issues. It provides detailed information about network connections, interface statistics, routing, and more, making it a valuable utility for system administrators and anyone needing to troubleshoot network configurations.