The ss Command: The netstat Replacement You Already Have Installed

netstat is gone from most fresh installs, and its replacement has been sitting in iproute2 the whole time. Here's how the ss command answers "what's listening?" and "who's connected?" in one line each.

Tux Harlow

4 min read

By Tux Harlow — Solnyxus editorial voice. Drafted with AI assistance; reviewed by the team.

Fresh server. Something's supposed to be listening on a port and clearly isn't. Your fingers type netstat -tulpn before your brain has finished the thought, you hit Enter, and the shell answers command not found with the calm of a librarian. Good news: the ss command has been sitting on that box the whole time. It ships in iproute2, the same package that gives you ip, and it's better at this job than netstat ever was.

Why the ss command replaced netstat

netstat lives in net-tools, and plenty of distributions just don't install that by default any more. Nobody held a funeral. It simply stopped turning up.

ss does the work differently. Instead of reading and parsing the text files under /proc/net, it asks the kernel directly over netlink, through the sock_diag interface. That's why it stays quick on a box juggling tens of thousands of connections, where scraping text starts to drag. And it knows more than its grandparent did: TCP timers, congestion details, memory per socket. (Side note: if you've ever watched a terminal sit there while netstat chewed through a busy load balancer, you already understand the appeal.)

The one-liner everyone needs

If you remember one thing from this post, make it this:

Bash
ss -tlnp

Read it left to right. TCP (-t). Listening sockets only (-l). Numeric addresses and ports, not names (-n). And the process that owns each socket (-p). Want UDP too? Add -u.

One catch that bites everyone once. Without root, the process column comes back empty for sockets you don't own, and you'll stare at it wondering whether the daemon is even running. It probably is. Run it with sudo and the names appear.

Who is connected right now

Bash
ss -tn

No -l, no -a, and ss shows established connections, the live ones. If you want every state at once, listening sockets included, that's what -a is for.

State filters

This is the bit that made me stop piping everything into grep. ss filters by TCP state on its own:

Bash
ss -tn state established
ss -tn state time-wait
ss -tn state syn-sent

The states you can ask for: established, syn-sent, syn-recv, fin-wait-1, fin-wait-2, time-wait, closed, close-wait, last-ack, listening and closing. There are groups as well, such as connected and synchronized, for when you want a family rather than one member.

My favourite is syn-sent. A pile of those usually means you've been knocking on a door upstream and nobody's answering. It's a diagnosis in one line, and I love it a little more than is healthy.

Address and port filters

Bash
ss -tn '( dport = :443 or sport = :443 )'
ss -tn dst 192.0.2.10

The first line catches connections to or from port 443. Quote those parentheses, or the shell will grab them before ss ever sees them, and the error you get won't tell you that's what happened. The second line shows connections to one remote address.

Summary and Unix sockets

Bash
ss -s
ss -x

ss -s prints totals per protocol and state. It's the fastest health check I know: one glance and you know whether you've got forty connections or forty thousand. ss -x lists Unix domain sockets, and that's where a lot of local daemons quietly live, databases and Docker among them. If a service "isn't listening" on any port, check here before you panic.

Timers and TCP internals

Bash
ss -tno
ss -tni

-o adds timer information: keepalive timers, retransmit timers, the little countdowns the kernel keeps on every connection. -i goes deeper into internal TCP information, with round-trip time, congestion window and retransmits. When a connection feels slow and nobody believes you, -i is how you bring receipts.

Killing a socket

Now the sharp knife. ss -K forcibly closes every socket that matches your filter. It needs root, and it needs a kernel built with CONFIG_INET_DIAG_DESTROY, so it won't work everywhere.

Bash
sudo ss -K dst 192.0.2.10 dport = :5432

Here's my hill, and I'll stand on it: always run the exact same filter without -K first. Read what comes back. Then add the K. A filter that's one character too broad will happily close connections you very much wanted open.

Translating netstat habits

Your fingers will keep typing the old commands for a while. Here's the phrasebook:

  • netstat -tulpn becomes ss -tulpn.
  • netstat -an becomes ss -an.
  • netstat -s statistics are closer to nstat from iproute2; ss -s is a socket summary.
  • netstat -r becomes ip route.

Notice how little changes in most of them? That's deliberate, and it's a kindness.

So here's tonight's homework. Open a terminal on anything you look after, run ss -tlnp with sudo, and check that every listening port on that list is one you meant to open. If one surprises you, well. Now you know which process to go and have a word with.