netstat (network statistics) is a command-line tool for monitoring network connections, routing tables, interface statistics, and more. While considered legacy on modern systems, it remains widely used and available.
Key Features
Display network connections
Show routing tables
List interface statistics
View network protocol statistics
Monitor listening ports
Show multicast memberships
Installation
Ubuntu (22.04/24.04)
macOS
Basic Syntax
Display All Connections
Listening Ports
Interface Statistics
Common Use Cases
1. Monitor TCP Connections
2. Check Listening Services
3. Network Statistics
4. Routing Table Information
Advanced Options
Continuous Output
Custom Display Format
Socket Information
Modern Alternatives
SS Command (Recommended)
IP Command
Common Flags Reference
Essential Options
-a: Show all connections
-t: TCP connections
-u: UDP connections
-l: Listening sockets
-p: Show process information
-n: Show numeric addresses
-r: Show routing table
-s: Show statistics
-i: Show interface statistics
-c: Continuous output
-v: Verbose output
Output Format
Troubleshooting
Common Issues and Solutions
Permission Denied
High CPU Usage
Slow Command Response
Debug Tips
Find Programs Using Ports
Monitor Connection States
Example Scripts
Connection Monitor
Port Scanner
Best Practices
Performance Considerations
Use -n to avoid DNS lookups
Filter output with grep when possible
Use modern alternatives (ss) for better performance
Security Monitoring
Regular Monitoring
Monitor connection states
Track listening ports
Check for unauthorized services
Quick Reference
Most Used Commands
Common Combinations
Remember:
Always use sudo when process information is needed
Consider using modern alternatives like ss for better performance
Use -n flag to speed up output when hostname resolution isn’t needed
This guide covers the most common and useful netstat commands. For system-specific variations, always consult the man pages (man netstat).