netstat Cheatsheet - Linux Network & Ports

Three questions dominate operations: is this port open, who is connected to me, who is sitting on that socket. netstat answers them by laying out four kernel tables at once — established sockets, listeners, the routing table and per-protocol counters. The three flags combine in ways worth memorising: -l restricts output to listeners, -n suppresses name resolution (which costs runtime and turns 10.0.0.1 into a counter-intuitive hostname), and -p attaches the owning process. For routine checks, -tlnp answers everything in one line. The state-filtering group turns output into a diagnostic instrument: a pile of TIME_WAIT means short connections by the thousand, a SYN_RECV backlog suggests SYN flood or a full accept queue, and accumulating CLOSE_WAIT is an application bug that never closed the peer handle. The routing group answers which interface traffic leaves by, which beats guessing at IP addresses. The final group points at ss and ip as faster replacements, with netstat kept for older scripts.

SysOps·39 commands·Last updated 2026-07-21

Typical Use Case

For back-end and operations engineers diagnosing network connections and port conflicts. netstat has essentially one job: answering "what is holding this port" and "what state are the connections in". This sheet is organised into seven groups: connection viewing, listening ports, routes and interfaces, protocol statistics, state filtering and troubleshooting, advanced options, and replacement tools. Note that ss and ip equivalents are given throughout — netstat is being phased out, and their output is considerably cleaner.

View Connections 6

netstat
Show all active connections
netstat -a
Show all listening and non-listening ports
netstat -at
Show TCP connections only
netstat -au
Show UDP connections only
netstat -an
Don't resolve names/ports (faster)
netstat -anp
Show PID and process name (needs root)

Listening Ports 6

netstat -l
Show all listening ports
netstat -lt
Show TCP listening ports
netstat -lu
Show UDP listening ports
netstat -lx
Show Unix socket listeners
netstat -lntp
TCP listeners with processes (root); most common
netstat -lnup
UDP listeners with processes

Routing Table & Interface Stats 5

netstat -r
Show the routing table (like route)
netstat -rn
Routing table without hostname resolution (faster)
netstat -i
Show interface statistics (packets in/out)
netstat -ie
Interface details (like ifconfig)
netstat -rn | grep UG
Show only the default gateway route

Protocol Statistics 5

netstat -s
Show statistics for all protocols
netstat -st
Show TCP statistics
netstat -su
Show UDP statistics
netstat -s | grep -i error
Show network error statistics
netstat -s | grep -i retrans
Show TCP retransmission statistics

Filter by State & Troubleshoot 6

netstat -an | grep ESTABLISHED
Show established connections
netstat -an | grep TIME_WAIT
Show TIME_WAIT connections
netstat -an | grep :80
Show connections on port 80
netstat -lntp | grep :80
Find which process listens on port 80
netstat -an | awk '{print $6}' | sort | uniq -c
Count connections per state
netstat -an | grep ESTABLISHED | wc -l
Count current active connections

Advanced Options 6

-n
No name resolution; shown faster
-p
Show PID and name of the owning process (root)
-c 1
Continuously output every second (live monitoring)
-o
Show connection timer information
-e
Show extra info: user and inode columns
-v
Verbose: show unconfigured interfaces

Alternatives (ss / ip) 5

ss -tlnp
ss replaces netstat -tlnp; faster
ss -tn state established
Filter established connections with ss
ss -s
Connection statistics summary with ss
ip addr
ip replaces ifconfig/netstat -ie for interfaces
ip route
ip replaces netstat -r for the routing table

Parameter matrix

ParameterEffectExample
-l只显示监听中的连接netstat -lt
-n不解析域名与端口名,输出更快netstat -an
-p显示占用端口的 PID 与进程名(需 root)netstat -lntp
-a包含监听与非监听全部连接netstat -an
-t / -u只看 TCP / 只看 UDPnetstat -at
-e / -i附加用户与 inode 列 / 接口统计netstat -ie
-r查看路由表netstat -rn
-s各协议统计信息netstat -st
-c 1每秒刷新一次,实时观察变化netstat -c 1
grep ESTABLISHED筛出已建立连接netstat -an | grep ESTABLISHED
grep TIME_WAIT筛出等待关闭的连接netstat -an | grep TIME_WAIT
ss -tlnp现代替代:更快且输出更清晰ss -tlnp
ip addr查看网络接口(替代 ifconfig)ip addr
ip route查看路由表ip route

Common pitfalls

SymptomPID 列显示为 -,看不到是哪个进程占用端口。

Cause查看进程信息需要 root 权限,普通用户看不到其他用户的进程映射。

Fix加 sudo 重新执行;或用 ss -tlnp 配合权限确认。

Symptom端口明明被占用,但 netstat 里看不到监听记录。

Cause服务只绑定了 127.0.0.1 而从外部访问,或协议是 UDP 却只看了 TCP。

Fix用 netstat -anp 同时看 TCP 与 UDP;确认服务绑定的地址是否只是本地回环。

Symptom执行 netstat 很慢,连接数上万时几乎卡住。

Cause默认要解析域名与端口名,且 -p 需要查进程映射。

Fix加 -n 跳过解析(最有效);新系统改用 ss,走 netlink 直接读内核数据。

SymptomTIME_WAIT 状态连接数暴涨,短连接端口耗尽。

Cause大量短连接关闭后处于等待状态,内核需保留一段时间才能释放端口。

Fix改用长连接或连接池;临时可调 net.ipv4.tcp_tw_reuse,但根治要看连接设计。

Symptom看到 ESTABLISHED 连接但服务端似乎没在监听。

Cause连接可能来自已退出的进程留下的孤儿连接,或 TIME_WAIT 残留。

Fix结合 -p 与 ss -tn state established 交叉确认;必要时重启相关服务清理。

Symptom不同系统上 netstat 输出格式不一致,脚本解析失败。

CauseBSD 与 Linux 的 netstat 列含义与选项支持存在差异。

Fix统一改用 ss(格式跨发行版更一致);脚本里固定使用 -n 输出。

Troubleshooting

  1. 1查端口被哪个进程占用

    netstat -lntp | grep :80

    需 root 才能看到 PID;这是端口类问题最快的一步。

  2. 2统计当前连接状态分布

    netstat -an | awk '{print $6}' | sort | uniq -c

    各状态数量一目了然,ESTABLISHED 或 TIME_WAIT 异常增多时最直观。

  3. 3统计活跃连接数

    netstat -an | grep ESTABLISHED | wc -l

    适合放进监控脚本作为连接数指标。

  4. 4确认默认网关路由

    netstat -rn | grep UG

    排查「能 ping 通但连不上」时先看网关是否正确。

  5. 5新系统上的等价命令

    ss -tlnp

    ss 走 netlink 直接读内核,比 netstat 快一个数量级,输出也更易读。

Command Examples

Find what holds port 80

netstat -lntp | grep :80 # 或更快的现代写法 ss -tlnp | grep :80

-l 只看监听、-n 不解析、-t 只看 TCP、-p 显示进程,最后一个参数需 root。新系统优先用 ss,直接读内核 netlink 快很多。

Output

tcp  0  0  0.0.0.0:80   0.0.0.0:*  LISTEN  1420/nginx
LISTEN 0  511  0.0.0.0:80  0.0.0.0:*  users:(("nginx",pid=1420,fd=6))

Count connections by state

netstat -an | awk '{print $6}' | sort | uniq -c | sort -rn

TIME_WAIT 大量堆积通常说明连接未复用;CLOSE_WAIT 多则是对端已关闭而本地没及时释放,需要查应用侧的连接处理。

Output

   2841 ESTABLISHED
    312 TIME_WAIT
     118 LISTEN
      47 CLOSE_WAIT

Filter established connections with ss

ss -tn state established | head

ss 的状态过滤是一等公民,不需要 grep 管道,输出也更整齐。排查连接泄漏时先用它取基线。

Output

ESTAB  0  0   10.0.0.5:52344   93.184.216.34:443
ESTAB  0  0   10.0.0.5:41890   151.101.1.140:443

Common Pitfalls

  • Without -n, netstat performs reverse DNS lookups and becomes unusably slow on large outputs; always pass -n.
  • -p needs root to show the owning process; as a normal user that column is simply absent.
  • A flood of TIME_WAIT does not automatically call for kernel tuning — first check whether the app opens a new TCP connection per request and whether keep-alive can be enabled.
  • netstat is being superseded by ss on modern systems; prefer ss in new scripts and keep netstat for legacy hosts.
  • An unreachable port may be a firewall rather than a stopped service: netstat proves local listening only, not external reachability.

Tips

  • netstat is being replaced by ss; on newer systems prefer ss (faster, modern).
  • Viewing processes needs root, otherwise the PID column shows -.
  • Too many TIME_WAIT means frequent short connections; consider long-lived connections or a pool.
  • netstat -an is fastest; -p slows it down by mapping processes.
  • ss reads kernel data via netlink, an order of magnitude faster than netstat.
  • The most common port-occupancy check is netstat -tlnp | grep <port>.

FAQ

How do I find which process is using port 80?

Run netstat -lntp | grep :80 — -l for listening only, -n to skip name resolution, -t for TCP, -p to show the owning process, which requires root. On modern systems prefer ss -tlnp: it is faster and cleaner. Once you have the PID, ps -p <pid> -o pid,cmd confirms the program.

Do I need to worry about a buildup of TIME_WAIT connections?

A small number is normal: the side that closes first holds the state so the last segment can be delivered, and it clears in roughly 60 seconds. But tens of thousands in a short window means connection reuse is misconfigured — check whether your app opens a new TCP connection per request and whether keep-alive can be enabled. Start by measuring the trend with netstat -an | grep TIME_WAIT | wc -l before tuning anything like tcp_tw_reuse.

Should I use netstat, ss or lsof?

ss is the modern replacement — it reads kernel netlink directly and is much faster than netstat, so prefer it on current systems. netstat remains useful for portability across older machines and scripts. lsof -i :80 is better for the reverse lookup: once you know the port or process, it shows which files and network connections it holds. They are often used together.

Why is netstat so slow without -n?

Because it tries to reverse-resolve every IP to a hostname and look up port numbers as service names like http or ssh, which can trigger many DNS lookups per query. On large outputs that dominates the runtime. Adding -n prints raw IPs and port numbers and the speedup is dramatic, which is why -ntp is the combination everyone memorizes.

What can netstat -s tell me when diagnosing the network?

-s prints cumulative counters per protocol layer. Two lines matter: netstat -s | grep -i error for the error packet total, and netstat -s | grep -i retrans for TCP retransmissions. An elevated retransmission rate usually points to packet loss, congestion, or a slow peer rather than a problem on your host. Since these are monotonic counters, run it twice with a gap and diff for a better signal.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us