When investigating dropped network connections, high latency, or throughput bottlenecks on Linux servers, administrators often run netstat -s or manually parse pseudo-files inside /proc/net/snmp and /proc/net/netstat. However, these traditional tools display cumulative counters accrued since the system booted, making it difficult to isolate transient network packet spikes. The nstat utility (part of the modern iproute2 package) solves this by computing and displaying differential metric rates, allowing engineers to track real-time TCP retransmissions and packet anomalies continuously.
How nstat Operates Inside the Linux Kernel
The Linux kernel maintains dozens of atomic SNMP and RFC-compliant network counters covering IP, ICMP, TCP, and UDP layers. Every time a TCP timeout occurs, a packet is retransmitted, an invalid checksum is received, or a socket listen queue overflows, the kernel increments the corresponding counter. nstat takes a snapshot of these values, waits for a specified interval, and outputs only the counters that changed during that specific measurement window.
Syntax and Key Parameters
nstat [options] [patterns]
-z(Zero Counters): Displays counters even if their value is zero during the measurement period.-a(Absolute Values): Displays cumulative totals since boot rather than differential updates.-r(Reset): Resets the internal history baseline snapshot.-s(Scan / Suppress History): Do not update history database file; ideal for recurring cron scripts.-d <seconds>: Runs continuously as a daemon, printing updates every specified interval.
Step-by-Step Practical Implementation
Step 1: Displaying Immediate Differential Network Activity
Run nstat with no arguments:
nstat
The first invocation creates a baseline snapshot in /tmp/.nstat.cache. Running nstat a second time prints only the metrics that changed between executions:
#kernel
IpInReceives 1420 0.0
IpInDelivers 1418 0.0
IpOutRequests 1250 0.0
TcpInSegs 1418 0.0
TcpOutSegs 1250 0.0
TcpRetransSegs 2 0.0
Step 2: Continuous Real-Time Metric Streaming
To monitor network performance during active load tests or streaming sessions, instruct nstat to print updates every 2 seconds:
nstat -d 2
Every two seconds, a concise delta block prints to your shell, highlighting any surge in retransmissions or reset packets.
Step 3: Filtering for TCP Retransmission and Error Metrics
To suppress general traffic counters and isolate only TCP reliability and error metrics:
nstat -z "TcpRetrans*" "TcpExtTCPTimeouts" "TcpExtListenOverflows"
This reveals whether packets are being retransmitted due to network congestion or server listen backlog exhaustion.
Step 4: Inspecting Absolute Cumulative Totals Since Boot
To inspect total lifetime network statistics since the server was powered on:
nstat -a "Tcp*"
Critical Kernel Metrics Monitored by nstat
| Metric Identifier | Protocol Layer | Diagnostic Meaning | Recommended Remediation When Spiking |
|---|---|---|---|
TcpRetransSegs |
TCP | Count of retransmitted TCP segments | Check for physical link packet loss, MTU mismatch, or network congestion |
TcpExtTCPTimeouts |
TCP Extended | TCP retransmission timer expirations | Investigate routing black holes or high bufferbloat |
TcpExtListenOverflows |
TCP Extended | Incoming connection dropped; listen queue full | Increase application backlog and kernel net.core.somaxconn |
TcpExtListenDrops |
TCP Extended | SYN requests dropped due to lack of memory | Increase net.ipv4.tcp_max_syn_backlog |
TcpExtTCPFastRetrans |
TCP Extended | Fast recovery retransmission triggered by triple dup-ACK | Normal TCP recovery behavior for out-of-order packets |
IpInDiscards |
IP | Incoming IP packets dropped due to buffer exhaustion | Increase NIC ring buffer size via ethtool -G |
Operational Notes and Best Practices
- No Root Privileges Required:
nstatreads publicly accessible metrics from/proc/net/snmpand/proc/net/netstat. Standard non-privileged users can execute it withoutsudo. - Automated Scripting: When using
nstatinside monitoring daemons or Prometheus node exporters, always pass-sto avoid race conditions on the local temporary cache file. - Correlating with Listen Drops: If web server throughput drops under load, check
TcpExtListenOverflows. Non-zero values confirm that the web server process (e.g. NGINX, Apache, or Node.js) cannot accept connections fast enough.