BLOG POSTS
    MangoHost Blog / Explore netstat and ss: View Linux Network Socket Stats
Explore netstat and ss: View Linux Network Socket Stats

Explore netstat and ss: View Linux Network Socket Stats

Why Should You Care About Linux Network Socket Stats?

If you’re running anything on a Linux server—whether it’s a cloud VM, a Docker container, a VPS, or a beefy dedicated box—network issues can be a nightmare. Slow sites, dropped connections, weird timeouts… Sometimes it’s not your code, not your database, not even your hosting provider. It’s the network stack itself. And that’s where netstat and ss come in.

These tools let you peek under the hood and see exactly what’s going on with your server’s network sockets. Who’s connecting? What ports are open? Are you being DDoSed? Is there a rogue process eating up all your connections? If you’ve ever wondered “Why is my site slow?” or “Is my server under attack?”—this is your first stop.

The Big Three Questions

  1. How do netstat and ss actually work? What do they show you, and how do they get that info?
  2. How do I set them up and use them quickly? What commands should I run, and what do the results mean?
  3. What are the real-world use cases? When do I use netstat vs ss? What are the pitfalls, and what else should I know?

How Do netstat and ss Work? (And Why Should You Care?)

Both netstat and ss are command-line tools that let you view active network connections, listening ports, routing tables, and more. They pull info from the Linux kernel’s networking stack—think of it as a live map of every socket (network endpoint) on your system.

  • netstat is the classic tool, around since the 90s. It’s part of the net-tools package.
  • ss (Socket Stat) is newer, faster, and part of the iproute2 suite. It’s the modern replacement for netstat.

Both tools let you answer questions like:

  • Which ports are open on my server?
  • Which processes are using which ports?
  • How many connections are in use? Are any stuck?
  • Is my server being scanned or attacked?

Algorithms & Structure (Geeky, but Simple)

Under the hood, both tools read from /proc/net/ (a virtual filesystem in Linux that exposes kernel info). ss is faster because it reads these files directly, while netstat uses older APIs and is a bit slower—especially on busy servers.

How to Set Up and Use netstat and ss (Quick & Dirty Guide)

Installing the Tools

  • On most modern Linux distros, ss is already installed.
  • netstat may not be—install it with:
    sudo apt install net-tools (Debian/Ubuntu)
    sudo yum install net-tools (CentOS/RHEL)

Basic Usage: The Commands You Actually Need

  • Show all listening ports (TCP & UDP):
    ss -tuln
    netstat -tuln
  • Show all connections (with process info):
    ss -tunap
    netstat -tunap
  • Show only established connections:
    ss -tuna state established
    netstat -tn | grep ESTABLISHED
  • See which process is using a port (e.g., 80):
    ss -ltnp 'sport = :80'
    netstat -ltnp | grep :80

What Do the Columns Mean?

  • Proto: Protocol (tcp, udp, etc.)
  • Recv-Q / Send-Q: Data waiting to be read/sent
  • Local Address:Port: Where the socket lives on your server
  • Foreign Address:Port: Where the connection is coming from/going to
  • State: Connection state (LISTEN, ESTABLISHED, TIME_WAIT, etc.)
  • PID/Program name: Which process owns the socket

Examples, Cases, and Practical Advice

Positive Case: Diagnosing a Web Server Bottleneck

Your site is slow. You run:

ss -tuna state established | grep :80

You see hundreds of connections from a single IP. That’s a DDoS or a misconfigured load balancer. Now you know where to look!

Negative Case: “Why is my port not open?”

You run:

ss -tuln | grep :443

No output? Your web server isn’t listening on port 443. Maybe it crashed, or the config is wrong. Fix the service, and you’re back in business.

Comparison Table: netstat vs ss

Feature netstat ss
Speed Slower on busy servers Much faster
Available by default No (on new distros) Yes
Shows process info Yes (with -p) Yes (with -p)
IPv6 support Yes Yes
Actively maintained No Yes
Learning curve Lower (more docs online) Medium (but worth it)

Beginner Mistakes and Common Myths

  • Myth: “netstat is better because it’s older.”
    Reality: ss is faster, more accurate, and the future.
  • Mistake: Forgetting sudo—you won’t see process info for sockets you don’t own without root.
  • Mistake: Grepping for the wrong port (e.g., grep 80 matches 8080, 8000, etc.). Use :80 or ss’s filter syntax.
  • Myth: “If netstat/ss shows nothing, the port is closed.”
    Reality: The service might not be running, or a firewall is blocking it. Check both.

Similar Solutions and Other Tools

  • nmap: Scan your server from the outside to see what’s open.
  • tcpdump: Capture raw packets for deep debugging (hardcore mode).
  • masscan: Super-fast port scanner (for big networks).
  • Wireshark: GUI packet analyzer (for desktops, not servers).

Interesting Facts & Non-Standard Usage

  • ss can show socket statistics over time, not just a snapshot. Try:
    watch -n 1 'ss -s'

    This gives you a live dashboard of TCP states (handy for debugging connection leaks).

  • You can use ss in scripts to alert you if too many connections are open:
    if [ $(ss -s | grep -oP 'estab \K\d+') -gt 1000 ]; then echo "Too many connections!"; fi
  • Want to see only Docker container sockets? Run inside the container, or use nsenter to enter its namespace.
  • Combine with lsof -i for even more detail (like file descriptors).

What New Opportunities Do These Tools Unlock?

  • Automation: Monitor your server’s connection count and auto-restart services if things get weird.
  • Security: Detect suspicious connections (e.g., backdoors, malware) by watching for unexpected listeners.
  • Debugging: Instantly see if your app is actually listening on the right port (no more “it works on my machine!” headaches).
  • Scaling: Track connection growth over time and know when to upgrade your VPS or move to a dedicated server (VPS / dedicated server).

Conclusion: Why, How, and Where to Use netstat and ss

If you run any kind of Linux server—cloud, Docker, VPS, or dedicated—knowing how to use ss (and, to a lesser extent, netstat) is a superpower. These tools give you instant visibility into your server’s network health, help you debug issues, and keep you safe from attacks.

  • Use ss for speed and modern features.
  • Keep netstat around for legacy scripts or if you’re following old tutorials.
  • Combine with other tools (nmap, tcpdump, lsof) for a full picture.
  • Automate checks to catch problems before your users do.

Don’t wait until your site is down or your server is hacked—get familiar with these commands now. Your future self (and your users) will thank you!

Official docs:
ss man page
netstat man page

Need more power? Consider upgrading your hosting:
Order a VPS or get a dedicated server for more control and better performance.

Happy debugging, and may your sockets always be in the ESTABLISHED state!



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked