Using netstat to View Networking Information
Objective
Learn how to use the netstat command to monitor and troubleshoot network connections, routing tables, and interface statistics on your system.
Prerequisites
- Operating System:
- Available on Linux, macOS, and Windows.
- Ensure you have administrative privileges for certain commands.
- Basic Networking Knowledge:
- Familiarity with IP addresses, ports, protocols (TCP/UDP), and routing.
- Access to a Command-Line Interface:
- Open a terminal (Linux/macOS) or Command Prompt/PowerShell (Windows).
Step 1: Viewing Active Network Connections
- Open a terminal or Command Prompt.
- Run the basic
netstat
command:netstat
- Displays active TCP connections on the system.
- Analyze the output:
- Proto: Protocol (e.g., TCP, UDP).
- Local Address: IP address and port of the local system.
- Foreign Address: IP address and port of the remote system.
- State: Status of the connection (e.g., ESTABLISHED, LISTENING).
Step 2: Displaying Listening Ports
- Show listening ports:
netstat -l
- Lists all listening TCP and UDP ports.
- For more detailed output, include numeric addresses:
netstat -ln
- On Windows, use:
netstat -an
Insight: Listening ports indicate services awaiting incoming connections.
Step 3: Including Process Information
- View processes associated with network connections:
netstat -p
- Requires administrative privileges.
- On Linux:
sudo netstat -tulnp
-t
: Show TCP connections.-u
: Show UDP connections.-n
: Display numeric addresses.-p
: Include process IDs and names.
- On Windows:
netstat -o
- Shows the PID (Process Identifier) of each connection.
Step 4: Monitoring Network Statistics
- Display protocol-specific statistics:
netstat -s
- Lists packet counts, errors, and protocol-specific information (e.g., TCP retransmissions).
- Filter statistics for specific protocols:
- Linux:
netstat -st
- Windows:
netstat -sp tcp
Tip: Use this to analyze network health and troubleshoot issues.
- Linux:
Step 5: Viewing Routing Tables
- Show the routing table:
netstat -r
- Displays the system’s routing table, including destination networks, gateways, and interfaces.
- Analyze the output:
- Destination: Target network or host.
- Gateway: Router used to reach the destination.
- Iface: Network interface handling the traffic.
- On Windows, include:
route print
- Similar output but includes additional details.
Step 6: Real-Time Monitoring
- Continuously monitor network connections:
- Linux:
watch -n 2 netstat -tulnp
- Refreshes every 2 seconds.
- macOS:
netstat -w 2
- Windows:
netstat -e 5
- Refreshes every 5 seconds.
- Linux:
- Analyze changing states, new connections, or high traffic rates.
Step 7: Troubleshooting with netstat
- Identify Open Ports:
- Use
netstat -l
to find services listening for incoming connections. - Cross-check with application logs to verify legitimacy.
- Use
- Diagnose High Traffic:
- Use
netstat -tulnp
to identify processes generating excessive traffic.
- Use
- Detect Unauthorized Access:
- Review foreign addresses for unusual connections.
- Verify Service Availability:
- Confirm critical services are listening on expected ports.
Additional Tips and Insights
- Filter Output:
- Combine
netstat
withgrep
(Linux/macOS) orfindstr
(Windows) for targeted queries.- Example:
netstat -tulnp | grep 80
- Example:
- Combine
- Use Modern Alternatives:
- On Linux, use
ss
for enhanced features and faster output:ss -tulnp
- On Linux, use
- Security Considerations:
- Regularly monitor open ports and active connections to detect potential vulnerabilities.
- Combine with Other Tools:
- Use
tcpdump
or Wireshark alongsidenetstat
for deeper traffic analysis.
- Use
Key Takeaways
netstat
is a versatile tool for monitoring network connections, routing, and traffic statistics.- Understanding its options helps troubleshoot and secure network configurations.
- Regular use ensures better awareness of system network activity and potential security threats.