netstat
netstat reads your computer's socket table: which ports are open, which connections are running, their TCP state and owner process. It also shows routes, interface counters and per-protocol statistics. It is built into Windows and macOS; on Linux it comes from the old net-tools package, and the options differ on every OS.
69.1 What it shows and where it runs
| Item | What it shows | Options |
|---|---|---|
| Socket table | Proto, Local, Foreign, State, PID | -a, -n, -o (Win) / -p (Linux) |
| Routing table | Destination, gateway, flags | -r, -rn |
| Interface counters | Bytes, packets, errors | -e (Win), -i (Linux/macOS) |
| Protocol statistics | Retransmits, failed opens | -s |
- Windows / macOS: built in (Windows -b needs Administrator).
- Linux: net-tools is obsolete; ss and ip are the modern replacements.
69.2 Where the State column comes from
- Step 1
1.Server 192.168.89.20 waits on 8080: LISTEN
- Step 2
2.Client 192.168.89.10 sends SYN: client SYN_SENT
- Step 3
3.SYN reaches the server: server SYN_RECV
- Step 4
4.Server replies SYN-ACK; client sends the last ACK
- Step 5
5.Both sides: ESTABLISHED
- Step 1
1.Client sends FIN; server ACKs
- Step 2
2.Client FIN_WAIT2, server CLOSE_WAIT
- Step 3
3.If the server app never closes its socket, CLOSE_WAIT stays stuck (app bug)
- Step 4
4.App closes: server FIN goes to the client
- Step 5
5.Client stays in TIME_WAIT (60 s on Linux) to handle late packets
69.3 Windows: connections and owner PID
- -a all connections and listening ports, -n numeric, -o owning PID.
- PID 1544 listens on 135; tasklist shows it is svchost.exe.
- -b shows the program name but needs Admin and is slow; -f shows the FQDN; a number at the end refreshes (netstat -ano 5).
- -p tcp / udp / tcpv6 / udpv6 filters one protocol; with -s also ip, icmp and their v6 versions.
netstat -ano -p tcp | findstr LISTENINGtasklist /FI "PID eq 1544"> netstat -ano -p tcp | findstr LISTENING TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1544 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4> tasklist /FI "PID eq 1544"Image Name PID Session Name Session# Mem Usage========================= ======== ================ =========== ============svchost.exe 1544 Services 0 27,880 K69.4 Windows: TCP states live (loopback)
- A PowerShell listener and client on 127.0.0.1:50690: both sides ESTABLISHED.
- The client closes first: client FIN_WAIT_2, server CLOSE_WAIT.
- The server closes too: the client side stays in TIME_WAIT with PID 0.
$l=[Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 50690)$l.Start()$c=[Net.Sockets.TcpClient]::new('127.0.0.1', 50690)$s=$l.AcceptTcpClient()netstat -ano | findstr :50690$c.Client.Shutdown('Send')netstat -ano | findstr :50690$s.Close()netstat -ano | findstr :50690> netstat -ano | findstr :50690 TCP 127.0.0.1:50690 0.0.0.0:0 LISTENING 13392 TCP 127.0.0.1:50690 127.0.0.1:63361 ESTABLISHED 13392 TCP 127.0.0.1:63361 127.0.0.1:50690 ESTABLISHED 13392(after the client's Shutdown('Send')) TCP 127.0.0.1:50690 0.0.0.0:0 LISTENING 13392 TCP 127.0.0.1:50690 127.0.0.1:63361 CLOSE_WAIT 13392 TCP 127.0.0.1:63361 127.0.0.1:50690 FIN_WAIT_2 13392(after the server's Close()) TCP 127.0.0.1:50690 0.0.0.0:0 LISTENING 13392 TCP 127.0.0.1:63361 127.0.0.1:50690 TIME_WAIT 069.5 Windows: statistics
- -e: Ethernet bytes, packets, discards and errors.
- -s -p icmp: destination unreachable and echo counters.
- -s -p tcp: Segments Retransmitted is a clue for a slow network.
- -r / -rn: routing table, same as route print.
netstat -enetstat -s -p icmpnetstat -s -p tcp> netstat -eInterface Statistics Received SentBytes 237815127 2548547415Unicast packets 18014295 6975659Non-unicast packets 20502 51494Discards 0 0Errors 0 0> netstat -s -p icmpICMPv4 Statistics Received Sent Messages 21820 9010 Destination Unreachable 20088 7970 Time Exceeded 95 0 Echo Replies 1304 333 Echos 333 707> netstat -s -p tcpTCP Statistics for IPv4 Active Opens = 76122 Passive Opens = 27410 Failed Connection Attempts = 9914 Reset Connections = 15538 Current Connections = 35 Segments Retransmitted = 39662069.6 Linux: listening sockets
- Lab: client 192.168.89.10 and server 192.168.89.20 on a Docker network, real net-tools netstat.
- -t TCP, -u UDP, -l listening only, -p PID/program (root), -n numeric; -a shows listening + others.
- socat listens on 8080, 8081, 9090, UDP 5000 and IPv6-only 8443.
netstat -tulpnnetstat -uln$ netstat -tulpnActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 29/socattcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 23/socattcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 16/socattcp6 0 0 :::8443 :::* LISTEN 41/socatudp 0 0 0.0.0.0:5000 0.0.0.0:* 35/socat$ netstat -ulnActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Stateudp 0 0 0.0.0.0:5000 0.0.0.0:*69.7 Linux: every TCP state in the lab
- Client: ESTABLISHED, TIME_WAIT, FIN_WAIT2, and two SYN_SENT (the server drops SYNs to 7070).
- Server: SYN_RECV and CLOSE_WAIT.
- awk + sort + uniq -c counts sockets per state.
- Linux spells FIN_WAIT1/FIN_WAIT2 without the second underscore; UNIX sockets have their own states.
netstat -tn # pcnetstat -tn # srvnetstat -tan | awk 'NR>2 {print $6}' | sort | uniq -c$ netstat -tn # pcProto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 1 192.168.89.10:53352 192.168.89.20:8081 SYN_SENTtcp 0 0 192.168.89.10:36248 192.168.89.20:8080 ESTABLISHEDtcp 0 0 192.168.89.10:36258 192.168.89.20:8080 TIME_WAITtcp 0 0 192.168.89.10:36262 192.168.89.20:8080 TIME_WAITtcp 0 1 192.168.89.10:37900 192.168.89.20:7070 SYN_SENTtcp 0 0 192.168.89.10:38480 192.168.89.20:9090 FIN_WAIT2$ netstat -tn # srvProto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 192.168.89.20:8081 192.168.89.10:53352 SYN_RECVtcp 0 0 192.168.89.20:8080 192.168.89.10:36248 ESTABLISHEDtcp 0 0 192.168.89.20:9090 192.168.89.10:38480 CLOSE_WAIT$ netstat -tan | awk 'NR>2 {print $6}' | sort | uniq -c 1 ESTABLISHED 1 FIN_WAIT2 1 LISTEN 2 SYN_SENT 2 TIME_WAIT69.8 Linux: output control
- -o adds a Timer column: seconds left in TIME_WAIT, retransmit timer for SYN_SENT.
- -p gives PID/Program; -ee adds User and Inode.
- Without -n you get container names instead of IPs (DNS lookups, can be slow).
- -W wide output, -c repeat every second.
netstat -tno # pcnetstat -tnp # srvnetstat -tnee # srvnetstat -t # pc (no -n: names, can be slow)$ netstat -tno # pcProto Recv-Q Send-Q Local Address Foreign Address State Timertcp 0 1 192.168.89.10:53352 192.168.89.20:8081 SYN_SENT on (2.31/7/0)tcp 0 0 192.168.89.10:36248 192.168.89.20:8080 ESTABLISHED off (0.00/0/0)tcp 0 0 192.168.89.10:36258 192.168.89.20:8080 TIME_WAIT timewait (36.98/0/0)tcp 0 0 192.168.89.10:38480 192.168.89.20:9090 FIN_WAIT2 timewait (39.72/0/0)$ netstat -tnp # srvProto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 192.168.89.20:8081 192.168.89.10:53352 SYN_RECV -tcp 0 0 192.168.89.20:8080 192.168.89.10:36248 ESTABLISHED 71/socattcp 0 0 192.168.89.20:9090 192.168.89.10:38480 CLOSE_WAIT 77/socat$ netstat -tnee # srvProto Recv-Q Send-Q Local Address Foreign Address State User Inodetcp 0 0 192.168.89.20:8080 192.168.89.10:36248 ESTABLISHED 0 1429173tcp 0 0 192.168.89.20:9090 192.168.89.10:38480 CLOSE_WAIT 0 1435800$ netstat -t # pcProto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 1 pc:53352 n069-srv.n069-lan:8081 SYN_SENTtcp 0 0 pc:36248 n069-srv.n069-lan:8080 ESTABLISHED69.9 Linux: display modes and address families
- -rn routing table: default route Flags UG = up + via gateway.
- -i interface counters, -g multicast groups, -st TCP statistics (18 segments retransmitted).
- -M (masquerade) is not supported on this kernel; old families like --ax25 are not compiled in.
- -4 / -6 only IPv4 / IPv6 sockets; -x UNIX domain sockets.
- Modern replacements: ss, ip route, ip -s link, ip maddr.
netstat -rnnetstat -inetstat -gnetstat -st | head -16netstat -Mnetstat -6 -tlnnetstat -xa | head -6$ netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface0.0.0.0 192.168.89.1 0.0.0.0 UG 0 0 0 eth0192.168.89.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0$ netstat -iKernel Interface tableIface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flgeth0 1500 4755 0 0 0 1973 0 0 0 BMRUlo 65536 16 0 0 0 16 0 0 0 LRU$ netstat -gIPv6/IPv4 Group MembershipsInterface RefCnt Grouplo 1 224.0.0.1eth0 1 224.0.0.1eth0 1 ff01::1$ netstat -st | head -16Tcp: 8 active connection openings 0 failed connection attempts 1 connections established 4721 segments received 1947 segments sent out 18 segments retransmitted$ netstat -Mnetstat: no support for `ip_masquerade' on this system.$ netstat -6 -tlnProto Recv-Q Send-Q Local Address Foreign Address Statetcp6 0 0 :::8443 :::* LISTEN$ netstat -xa | head -6Active UNIX domain sockets (servers and established)Proto RefCnt Flags Type State I-Node Pathunix 3 [ ] DGRAM CONNECTED 142883369.10 macOS (BSD-style netstat)
- On macOS -p means protocol, not program; for the PID use -v or lsof -i.
- -I en0 -w 1 shows live traffic every second (check Wi-Fi speed).
- Route flags: U up, G gateway, H host route, S static.
- State names differ slightly: SYN_RECEIVED (Windows), SYN_RECV (Linux), SYN_RCVD (macOS).
netstat -an -p tcp daily combo (-p = protocol on BSD)netstat -an -p tcp | grep LISTENnetstat -v process name/PID columns (or lsof -i)netstat -i / -I en0 interface counters / one interfacenetstat -I en0 -w 1 live traffic every 1 snetstat -s -p tcp TCP statisticsnetstat -g multicast membershipsnetstat -m mbuf memorysudo netstat -B BPF devices (tcpdump)netstat -rn -f inet IPv4 routes + default gatewaynetstat -rnl routes with MTU (more l = more metrics)| Task | Windows | Linux | macOS |
|---|---|---|---|
| Listening + PID | netstat -ano | netstat -tulpn | netstat -an -p tcp + lsof -i |
| Routes | netstat -rn | netstat -rn | netstat -rn -f inet |
| Interface counters | netstat -e | netstat -i | netstat -i / -I en0 |
| Protocol stats | netstat -s -p tcp | netstat -st | netstat -s -p tcp |
69.11 Common mistakes
- Thinking many TIME_WAIT sockets is a problem or attack: it is normal on busy servers.
- Do worry about many CLOSE_WAIT (application not closing sockets) or many SYN_RECV (possible SYN flood).
- Using -p on macOS expecting the program: it is the protocol filter there.
- Forgetting -n and waiting on slow name lookups.
69.12 Interview tips
- Who owns port 8080? Windows: netstat -ano | findstr :8080 then tasklist; Linux: sudo netstat -tulpn or ss -tulpn; macOS: lsof -i :8080.
- 100+ CLOSE_WAIT = application bug: the remote side closed and your app did not.
- TIME_WAIT sits on the side that closed first.
- On Linux prefer ss and ip; net-tools netstat is obsolete.