In the world of Linux system administration, understanding what is happening behind the scenes of your network stack is crucial for troubleshooting connectivity issues, monitoring bandwidth usage, or simply auditing open ports. Two primary commands have historically served this purpose: netstat (network statistics) and ss (socket statistics). While netstat has been a staple for decades, it is now considered deprecated in many distributions due to its reliance on the older libnsl library and slower performance. Conversely, ss is the modern, faster successor developed by the Linux kernel team. This guide will walk you through using both tools effectively, explaining their syntax differences, interpreting their output, and choosing the right tool for your specific troubleshooting scenario.
Why You Need to Monitor Network Connections
Before diving into the commands, it is important to understand why viewing real-time connections matters. When a server becomes unresponsive or a client cannot connect to a service, the issue often lies in the socket layer. Are there too many ephemeral ports exhausted? Is a specific process listening on port 80 when it shouldn’t be? Or has a connection been stuck in a TIME_WAIT state, consuming resources unnecessarily?
Real-time monitoring allows you to distinguish between active connections (data currently flowing), passive listening sockets (waiting for incoming requests), and established but idle connections. By utilizing these tools, administrators can quickly isolate whether a problem is application-level or network-layer level without needing to install additional software packages.
Understanding the Modern Standard: The ‘ss’ Command
The ss command is currently the recommended utility for most Linux distributions, including Ubuntu 18.04+, Debian, and RHEL/CentOS versions that have removed netstat by default. It provides functionality similar to netstat but operates directly on kernel data structures, making it significantly faster and more memory-efficient.
To view all active connections, you must use the -t flag for TCP sockets. Without this flag, ss only displays UDP sockets by default, which can be misleading if your issue is related to web traffic or SSH. The basic command to list all TCP connections is:
ss -tunap
Here is a breakdown of the flags used:
* -t: Displays TCP sockets.
* -u: Displays UDP sockets (often combined with -t for completeness).
* -n: Shows numeric addresses and port numbers without attempting to resolve hostnames or service names. This speeds up execution and prevents DNS lookup errors.
* -a: Lists both listening and established connections.
* -p: Displays the process ID (PID) and program name associated with each socket. Note that on some systems, you may need root privileges (sudo) to see the process information for non-system services.
The output of ss is structured into columns: State, Recv-Q, Send-Q, Local Address:Port, Peer Address:Port, Process. The “State” column is particularly informative. Common states include ESTAB (established), LISTEN (waiting for connections), TIME_WAIT (connection closing), and CLOSE_WAIT (the local application has not closed the socket after receiving a disconnect request).
Using ‘netstat’ on Legacy Systems
Although deprecated, you may still encounter netstat on older distributions or in environments that have not yet migrated to ss. If your system lacks ss, netstat is often available via installation from package repositories. However, be aware that modern distributions like Ubuntu 20.04+ require the net-tools package to be installed explicitly, as it is removed by default to reduce attack surface and dependency bloat.
The syntax for netstat is largely identical in terms of flags:
netstat -tunap
The output format differs slightly from ss. While ss uses a more compact, column-based layout that resembles a spreadsheet, netstat traditionally presents data in a table with headers. A key difference you will notice is the “State” column; in older versions of netstat, active connections might not always explicitly show the state unless specific flags are used, whereas ss makes the connection state the primary focus of its output design.
When using netstat, pay attention to the “Foreign Address” and “Local Address” columns. If you see an entry with an asterisk (*) in the Foreign Address column, it indicates a wildcard bind, meaning the service is listening on all available network interfaces (0.0.0.0). This is standard for web servers but can be a security risk if not intended.
Interpreting Connection States and Common Issues
Both ss and netstat provide critical insights into connection states, which are vital for diagnosing specific networking problems.
ESTABLISHED: The connection is open and data is being exchanged. If you see too many of these without corresponding traffic, it may indicate a “connection leak” in your application code where sockets are not being properly closed after use.
TIME_WAIT: This state occurs when the local host has sent a FIN packet to close the connection but must wait for an acknowledgment before fully closing the socket. While normal during high-traffic periods, an accumulation of TIME_WAIT sockets can exhaust ephemeral ports, preventing new connections from being established. You can view these specifically with ss -tan | grep TIME-WAIT.
CLOSE_WAIT: This is often a sign of application error. It means the remote peer has closed the connection, but your application has not yet called close() on its socket file descriptor. This usually points to a bug in the application logic or a failure to handle shutdown signals gracefully.
Practical Example: Auditing Open Ports and Processes
To demonstrate how to use these tools effectively, let’s perform a real-world audit of a web server. Suppose you need to verify that only the necessary services are listening on port 80 (HTTP) and 443 (HTTPS), and that no unauthorized processes are running.
First, ensure you have root privileges to see the process details:
sudo ss -tunap | grep -E ':80|:443'
This command filters the output to show only connections involving ports 80 or 443. You should look for the “Process” column. If you see nginx, apache2, or httpd, that is expected. If you see an unknown PID or a process named something suspicious, investigate immediately.
Next, check for any listening sockets on non-standard ports that might indicate a misconfiguration or a backdoor:
sudo ss -tunap | grep LISTEN | awk '{print $5}' | cut -d: -f1 | sort -u | xargs -I {} netstat -tlnp --tcp=0:{} 2>/dev/null || echo "No extra listeners found"
While the above command is complex, a simpler approach for daily checks is to simply list all listening ports and cross-reference them with your known services:
sudo ss -tunap | grep LISTEN
Review the “Local Address:Port” column. Ensure that 127.0.0.1 (localhost) bindings are used for internal administrative tools to prevent external access, while public services like web servers should ideally bind to specific IP addresses or use :: for IPv6 compatibility if configured correctly.
Security Considerations and Best Practices
When viewing network connections, security is a paramount concern. Always run these commands with caution in production environments. While reading socket information is generally safe, displaying the full process list (especially with the -p flag) can inadvertently expose sensitive information about running applications to anyone who can execute the command. In multi-user environments or shared hosting, ensure you are not inadvertently revealing other users’ processes.
Furthermore, be mindful of performance. Running ss without arguments is extremely fast because it reads directly from kernel memory. However, adding too many flags or piping the output through heavy text processing tools like awk or grep on massive datasets can introduce latency. For real-time monitoring dashboards, consider using watch to refresh the view every few seconds:
watch -n 2 'ss -tunap'
This provides a live stream of network activity, allowing you to see connections drop in real-time as they complete their lifecycle.
Conclusion and Migration Strategy
In summary, while netstat remains functional on many systems, ss is the superior choice for modern Linux administration due to its speed, efficiency, and detailed state reporting. The transition from netstat to ss is straightforward because the command-line flags (-t, -u, -n, -p) are nearly identical in function.
For new scripts and monitoring tools, always prefer ss. If you maintain legacy systems that rely on netstat, plan a migration path to ss or consider installing iproute2 packages which provide the modern toolset. When troubleshooting connection issues, always start by checking the state of the sockets (ESTAB, TIME_WAIT, etc.) and verifying the associated processes. By mastering these commands, you gain immediate visibility into your system’s network health, allowing for faster diagnosis and more secure configuration management. Remember that availability, eligibility, and specific kernel versions may vary slightly across distributions, so always verify command availability on your specific OS release before relying on them in critical production scripts.

