Sockets and listening ports with ss
Find what is listening on which address with ss -tlnp, read TCP socket states, and know when TIME_WAIT and CLOSE_WAIT are a problem.
Interfaces, addresses and the ip commandProcesses through /proc
Sockets and listening ports with ss
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A socket is a process's endpoint for network traffic, and to the process it is a file descriptor. A listening socket waits for connections. A connected socket carries one.
- `ss -tlnp` lists listening TCP sockets with numeric ports and the owning process. Run it with `sudo` to see processes you do not own.
- The local address column tells you who can reach a service. `127.0.0.1:5432` is reachable only from this machine. `0.0.0.0:22` or `[::]:22` is reachable on every interface.
- Outgoing connections get a source port from the ephemeral range, `net.ipv4.ip_local_port_range`, which defaults to 32768–60999 on Linux.
- `TIME_WAIT` is normal. The side that closes first holds the socket for 60 seconds so stray packets cannot land in a new connection. Lots of `CLOSE_WAIT` is a bug in the local program, which never closed its end.
“Is it even listening?”
Half of all “the service is down” tickets end with the same discovery: the program is running, but it is not listening where everyone thinks it is. It bound to localhost instead of the public interface, or to port 8081 instead of 8080, or it crashed during start-up and systemd restarted it into a loop. Before you touch a firewall or a load balancer, find out what the kernel thinks is listening.
The thing to look at is the socket. When a program wants to talk over the network, it asks the kernel for a socket and gets back a file descriptor, the same kind of handle it gets for a file. You can see them under /proc/<pid>/fd, as in processes through /proc. A server socket is bound to an address and port and listening. Each accepted connection gets a new, connected socket of its own. Think of a restaurant host stand: the stand stays put, and every party that arrives is shown to its own table.
ss -tlnp
ss (socket statistics) reads socket tables straight from the kernel and replaces the old netstat. The ss man page defines the flags in the classic combination:
sudo ss -tlnp # TCP, listening, numeric, with processes
sudo ss -ulnp # same for UDP
ss -tn # established TCP connections
ss -s # summary counts
-t is TCP, -u UDP, -l listening only, -n turns off name resolution so you see :22 instead of :ssh, and -p shows the owning process. Without sudo you see only your own processes in that column.
The column that answers most questions is Local Address:Port:
| Local address | Who can connect |
|---|---|
127.0.0.1:5432 |
only this machine, over loopback |
192.0.2.10:8080 |
only traffic addressed to that one IP |
0.0.0.0:22 |
any IPv4 address on any interface |
[::]:80 or *:80 |
any address; often IPv4 too, depending on the program |
Bind address is the first filter, before any firewall. A service bound to loopback cannot be reached from outside however open the firewall is. For the firewall half of the story see firewalls, ports and network troubleshooting.
Socket states
A TCP connection moves through states, and ss names them. The man page lists established, syn-sent, syn-recv, fin-wait-1, fin-wait-2, time-wait, close-wait, last-ack, closing, closed and listening. You can filter by them:
ss -tn state established '( dport = :443 )'
ss -tno state time-wait # -o adds the timer, so you see time left
ss -tn state close-wait
These filter examples follow the ss man page. I did not run them, since there is no Linux machine here.
Ephemeral ports
The client side of a connection also needs a port. The kernel picks one from the ephemeral range. The kernel’s ip-sysctl document gives ip_local_port_range a default of 32768 to 60999, about 28,000 ports for each destination address and port. Other systems differ. On the Mac I wrote this on:
$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535
That is the IANA range, which Linux does not use by default. It is a good reminder to check values on the actual system rather than trust a number from another OS.
TIME_WAIT and CLOSE_WAIT
When a TCP connection closes, the side that closes first enters TIME_WAIT and keeps the socket for a while. The reason is safety: a delayed packet from the old connection could otherwise arrive and be accepted by a new connection that reused the same address and port pair. In the kernel source, TCP_TIMEWAIT_LEN is defined as 60 seconds in include/net/tcp.h, and it is not a sysctl.
Many TIME_WAIT sockets on a busy server are normal and cost little. The problem case is a client that opens a fresh connection for every request to the same backend: each one ties up an ephemeral port for a minute, and at a few hundred requests a second the range runs out. The fix is connection reuse, through HTTP keep-alive or a pool. Sysctl tricks are not the fix. The ip-sysctl document says tcp_tw_reuse now defaults to 2, reuse for loopback traffic only. The old tcp_tw_recycle knob, which broke clients behind NAT, no longer exists in current kernels.
CLOSE_WAIT is the opposite and more worrying. It means the other side closed and the kernel told your program, but your program never called close(). A count that keeps climbing is a descriptor leak in the application. It will eventually hit the file limit from resource limits with ulimit.
For a feel of the proportions, this is the state count on my Mac while writing, with macOS’s netstat since it has no ss:
$ netstat -an -p tcp | awk 'NR>2 {print $6}' | sort | uniq -c | sort -rn
31 LISTEN
20 ESTABLISHED
About the video
The embed is a five-minute clip, 2:05 to the end, of Learn Linux TV’s “How to Use the ss Command (Linux Crash Course Series)”, October 2024. It covers basic ss usage, UDP sockets and the statistics view, and the clip skips the sponsor segment at 0:49. It does not go into socket states or TIME_WAIT. The LearnITGuide video in deeper is about those states, though I judged it from its title only. English was judged from both channels’ English caption tracks. Facts were checked against the ss man page, the kernel’s ip-sysctl document and include/net/tcp.h in the mainline tree (7.4-rc4), on 2026-11-28. The sysctl and netstat output above was run on macOS. Nothing here was run on Linux.
Where this fits
You can now see what is listening. Next, decide who is allowed to reach it: packet filtering with nftables.
Check yourself
`ss -tlnp` shows PostgreSQL on `127.0.0.1:5432`. A colleague cannot connect from another server even though the firewall allows 5432. Why?
The socket is bound to loopback only, so the kernel will not accept connections arriving on other interfaces. PostgreSQL's listen address must change. The firewall was never the problem.
A busy client service shows 25,000 sockets in `TIME_WAIT` to the same backend and starts failing with 'Cannot assign requested address'. What is happening?
It opens and closes a new connection per request, and each closed connection holds a local port for 60 seconds, so the ephemeral range runs out. Reusing connections through keep-alive or pooling is the real fix.
What does a growing number of `CLOSE_WAIT` sockets on your server mean?
The peer has closed the connection but your application has not called close on its end. It usually points to a leak in the program's connection handling, not the network.
Why might `ss -tlnp` show a listening socket with no process name?
You ran it without enough privilege to read other users' processes, or the socket belongs to the kernel itself, as with some NFS services. Retry with `sudo`.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.