Socket Statistics: ss & netstat

ss -tulnp vs netstat, TCP socket state machine, TIME_WAIT, and CLOSE_WAIT diagnosis.

Overview

Overview ss (socket statistics) is the modern replacement for netstat. It reads socket information directly from the kernel through a netlink socket rather than parsing /proc/net/tcp, which makes it significantly faster on systems with many connections.

The command you will type most is ss -tulnp. Each letter either picks which sockets to show or adds detail to them.

  • t: TCP sockets.
  • u: UDP sockets.
  • l: listening sockets only. Omit it to see established connections too.
  • n: numeric. Show IP addresses and port numbers instead of resolving them to hostnames and service names, which is much faster.
  • p: show the process name and PID.

Each line of ss output describes one socket. Netid is the socket type (tcp, udp or unix) and State is its TCP state. Recv-Q counts bytes received but not yet read by the application, so a high value means the app can't keep up. Send-Q counts bytes in the send buffer that the remote side has not yet acknowledged. The line ends with Local Address:Port, Peer Address:Port and Process.

The State column comes from the TCP state machine. The full handshake and teardown move a connection through distinct states. A server waits in LISTEN. A client that has sent its SYN and waits for the SYN-ACK is in SYN_SENT. A server that received the SYN and sent the SYN-ACK is in SYN_RECV. Both sides then reach ESTABLISHED, the connected state. On the way out, the side that starts the close passes through FIN_WAIT_1 and FIN_WAIT_2 and then TIME_WAIT, where it waits for delayed packets. The other side passes through CLOSE_WAIT, where it has acknowledged the FIN but its application hasn't called close(), then LAST_ACK, and both end in CLOSED. Two of these states deserve a closer look.

TIME_WAIT is where the side that initiated the close waits after the connection ends. It lasts 2×MSL (Maximum Segment Lifetime). MSL is typically 30 to 60 seconds, so TIME_WAIT lasts 60 to 120 seconds. Its purpose is to make sure delayed or duplicate packets from the old connection don't corrupt a new connection with the same 4-tuple. Thousands of TIME_WAIT connections are normal on a busy server, and the kernel manages them automatically.

CLOSE_WAIT is the opposite side of the same close. The remote sent a FIN because it wants to close, and the local side acknowledged it, but the local application hasn't called close() yet. CLOSE_WAIT connections accumulate when there's an application bug, such as not calling close() on HTTP keep-alive connections or leaving sockets unclosed in code. It is almost always an application bug, not a kernel issue.

Key concepts

  • ss vs netstat speed
  • -tulnp flags
  • TIME_WAIT 2MSL
  • CLOSE_WAIT as app bug

Interview questions

  • You see thousands of TIME_WAIT connections on a server. Is this a problem? How do you tune it?
  • What does CLOSE_WAIT mean in the TCP state machine? What usually causes it?

Sign up free to read the full topic

More Linux topics

Start free on Cariara · Coding problems · System design studio