netstat Command in Linux: Network Connections, Routing, and Statistics
Learn netstat command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
10 min read
Learn netstat command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
- What netstat Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
The netstat command in linux has served as a foundational networking tool for decades, providing system administrators with visibility into open network sockets, active TCP/UDP connections, interface transmission statistics, and kernel routing tables. Originating in BSD Unix in 1983 and ported to Linux as part of the net-tools package, netstat became the universal tool for diagnosing connectivity issues, tracking network sockets, and inspecting daemon bindings.
The netstat command in Linux (network statistics) displays active network connections (TCP/UDP), listening ports, kernel routing tables, network interface packet statistics, and multicast memberships. While superseded by the modern ss and iproute2 suites on modern Linux distributions, netstat remains widely used for system administration across legacy Linux systems, BSD Unix, and macOS.
In modern Linux distributions, the net-tools suite—including netstat, ifconfig, route, and arp—is officially deprecated in favor of the high-performance iproute2 suite (ss and ip). Netstat operates by reading and parsing ASCII text entries in the /proc/net virtual filesystem. On servers with large numbers of active connections, this parsing model incurs kernel locking overhead and higher latency compared to the modern binary Netlink sock_diag interface used by ss.
Despite its deprecation on cutting-edge Linux systems, understanding netstat remains essential for engineers maintaining long-term support enterprise systems (such as RHEL 6/7 and legacy appliances), managing cross-platform BSD and macOS environments, or reading legacy administration scripts and documentation.
What netstat Does and When to Use It
Under the hood, netstat gathers its telemetry by opening, reading, and parsing virtual files created by the Linux kernel network stack under /proc/net. Active TCP sockets are parsed from /proc/net/tcp and /proc/net/tcp6, UDP sockets from /proc/net/udp, kernel routing tables from /proc/net/route, and interface packet statistics from /proc/net/dev. For each socket, /proc/net displays local/remote IP addresses and ports encoded in hexadecimal notation, along with internal socket state flags and inode identifiers.
To associate a socket inode with a specific Process ID (PID) and executable name, netstat scans the entire /proc directory, iterating through every numerical PID folder and inspecting the symbolic links located in /proc/[pid]/fd/*. When it matches a symbolic link target like socket:[12345] with the socket inode read from /proc/net/tcp, netstat extracts the process name from /proc/[pid]/cmdline. This multi-step file traversal explains why netstat is noticeably slower than Netlink-based tools on systems with high process counts.
At the operating system level, netstat interacts with the Linux kernel networking stack through netlink sockets (AF_NETLINK), raw packet sockets, or standard POSIX resolver APIs. When running netstat in cloud instances or Kubernetes worker nodes, combining flags such as -t, -u, -l, -a lets you bypass slow reverse DNS lookups and inspect numeric IPv4/IPv6 addresses and socket states directly.
Availability and Installation Across Linux Distributions
The netstat utility is packaged in net-tools across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:
Before running administrative tasks or automation scripts, verify whether netstat is installed and inspect which binary path and version your shell resolves:
type -a netstat
netstat --version 2>/dev/null || man -f netstat
# Illustrative output (reviewed against documentation)
netstat is /usr/bin/netstat
netstat (net-tools)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y net-tools
# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y net-tools
# Arch Linux
sudo pacman -S --noconfirm net-tools
# Alpine Linux
sudo apk add --no-cache net-tools
Command Syntax, Arguments, and Exit Status Codes
netstat [OPTIONS] [FILTER]
The netstat syntax uses single-letter option flags to control data selection and formatting: netstat [OPTIONS]. Common selection flags include -t (TCP), -u (UDP), -l (listening sockets only), -a (all sockets, both listening and active), -r (routing tables), -i (network interface tables), and -s (protocol summary statistics).
Formatting flags drastically alter output speed and readability: -n disables reverse DNS hostname lookups and port service translation, outputting raw numerical addresses; -p displays the owning PID and program name (requiring root elevation); and -c triggers continuous execution, refreshing output once per second until interrupted with Ctrl+C.
0— Successful query execution; connection tables, routing entries, or statistics were retrieved and formatted correctly.1— Operational failure, invalid command-line flag, or inability to read virtual/proc/netfiles.127— Command not found error, indicating that the net-tools package is not installed on the system.
Important Options and Flags Reference
| Option / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
-t | --tcp | Filters output to display only TCP (Transmission Control Protocol) connections and sockets. |
-u | --udp | Filters output to display only UDP (User Datagram Protocol) connections and sockets. |
-l | --listening | Displays only listening server sockets (omitted by default; netstat defaults to established connections). |
-a | --all | Displays all listening and established sockets across selected protocol families. |
-n | --numeric | Outputs numeric IP addresses and port numbers instead of resolving hostnames and service names. |
-p | --program | Displays the Process ID (PID) and program name associated with each socket (requires root/sudo privileges). |
-r | --route | Displays the kernel routing table (equivalent to the legacy route command). |
-i | --interfaces | Displays a table of network interfaces with MTU, transmission counters, and packet error rates. |
-s | --statistics | Displays summary counters for IP, ICMP, TCP, and UDP packet traffic and error conditions. |
-c | --continuous | Continuously refreshes the selected network display every second until interrupted. |
The classic sysadmin command combination is netstat -tulnp. Combining TCP (-t), UDP (-u), listening (-l), numeric (-n), and program (-p) flags produces an exhaustive view of all daemons waiting for inbound connections, identifying port conflicts and unauthorized services.
Always include the -n flag in production environments. Omitting -n causes netstat to issue reverse DNS queries for every single IP address in the connection table; if DNS resolvers are slow or unresponsive, netstat will hang for several minutes while attempting to resolve public IP addresses.
When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.
Practical Basic and Advanced Usage Examples
1. List All Listening Ports with PIDs and Program Names
Inspect all listening TCP and UDP sockets with numeric addresses and owning process identifiers.
sudo netstat -tulnp
# Illustrative output (reviewed against documentation)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 842/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1250/nginx: master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1250/nginx: master
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1100/mariadbd
udp 0 0 0.0.0.0:53 0.0.0.0:* 1024/named
The output lists all listening daemons, confirming that SSH (22), HTTP (80), and HTTPS (443) are listening on all interfaces, while MariaDB (3306) is isolated to 127.0.0.1.
2. Display Kernel IP Routing Table with Numeric Gateways
View the kernel routing table to check the default gateway and active interface routes without DNS translation.
netstat -rn
# Illustrative output (reviewed against documentation)
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.0.2.1 0.0.0.0 UG 0 0 0 eth0
192.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
The -r flag prints routing entries. Destination 0.0.0.0 with flag UG indicates the default network gateway reachable via interface eth0.
3. Display Network Interface Packet Transmission Statistics
Inspect all active network interfaces to view Maximum Transmission Unit (MTU), received packets, errors, and drops.
netstat -i
# Illustrative output (reviewed against documentation)
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0 1500 0 0 0 0 0 0 0 0 BMU
eth0 1500 1842010 0 0 0 1204912 0 0 0 BMRU
lo 65536 45210 0 0 0 45210 0 0 0 LRU
Interface eth0 shows healthy transmission counters with zero receive/transmit errors (RX-ERR/TX-ERR) and zero dropped packets (RX-DRP/TX-DRP).
4. Display Protocol-Level Network Summary Statistics
Print aggregate counters for IP, ICMP, TCP, and UDP protocols to detect retransmissions and failed connection attempts.
netstat -s | grep -A 8 "^Tcp:"
# Illustrative output (reviewed against documentation)
Tcp:
1450 active connection openings
3210 passive connection openings
12 failed connection attempts
45 connection resets received
2 connections established
142010 segments received
139800 segments sent
32 segments retransmitted
The protocol summary reveals TCP health metrics: retransmitted segments, connection resets, and successful active versus passive connection openings.
5. Monitor Active Network Connections in Real Time
Continuously refresh TCP connection status once per second to monitor incoming connections during load testing.
netstat -t -c
# Illustrative output (reviewed against documentation)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.0.2.10:22 198.51.100.45:52341 ESTABLISHED
tcp 0 64 192.0.2.10:443 203.0.113.88:41200 ESTABLISHED
The -c flag keeps netstat running in the foreground, clearing and updating the terminal every second with current connection states.
6. List Active Listening Unix Domain Sockets
Inspect local Unix domain stream and datagram sockets to troubleshoot inter-process communication.
netstat -lxp | head -n 6
# Illustrative output (reviewed against documentation)
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 12450 1/systemd /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 18920 912/dockerd /var/run/docker.sock
unix 2 [ ACC ] STREAM LISTENING 22100 1450/php-fpm: maste /run/php/php8.2-fpm.sock
The -lxp options display listening local IPC sockets, showing the socket file path, inode, and owning daemon PID.
Real-World Workflows, Pipelines, and Shell Automation
Interface Packet Loss and Transmission Error Diagnosis
Diagnose network performance degradation by monitoring interface errors, frame collisions, and dropped packets over time.
#!/usr/bin/env bash
set -euo pipefail
INTERFACE="eth0"
echo "=== Baseline Interface Statistics for: ${INTERFACE} ==="
netstat -i | grep -E "Iface|${INTERFACE}"
echo "Capturing packet activity for 10 seconds..."
sleep 10
echo "=== Updated Interface Statistics ==="
netstat -i | grep -E "Iface|${INTERFACE}"
echo "=== TCP Protocol Error Counters ==="
netstat -s | grep -i -E "retransmitted|failed connection|resets"
# Illustrative output (reviewed against documentation)
=== Baseline Interface Statistics for: eth0 ===
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 1842010 0 0 0 1204912 0 0 0 BMRU
Capturing packet activity for 10 seconds...
=== Updated Interface Statistics ===
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 1844500 0 0 0 1206100 0 0 0 BMRU
=== TCP Protocol Error Counters ===
12 failed connection attempts
45 connection resets received
32 segments retransmitted
This workflow samples interface packet counters before and after an observation window, checking for incrementing RX-ERR, RX-DRP, or TCP retransmission anomalies.
Legacy Server Port Audit and Service Inventory Pipeline
Audit listening daemons on legacy enterprise distributions where iproute2 is not available, formatting output for automated compliance reporting.
#!/usr/bin/env bash
set -euo pipefail
OUTPUT_REPORT="/tmp/listening_services_audit.csv"
echo "Protocol,Local_Address,Port,PID,Program" > "${OUTPUT_REPORT}"
echo "=== Auditing Listening Services with netstat ==="
sudo netstat -tulnp | awk 'NR>2 {
proto = $1;
split($4, a, ":");
port = a[length(a)];
sub(":"port"$", "", $4);
addr = $4;
split($7, p, "/");
pid = p[1];
prog = p[2];
if (pid == "-") { pid="N/A"; prog="N/A"; }
print proto","addr","port","pid","prog;
}' >> "${OUTPUT_REPORT}"
echo "Audit completed. Summary of discovered ports:"
column -s, -t "${OUTPUT_REPORT}"
# Illustrative output (reviewed against documentation)
=== Auditing Listening Services with netstat ===
Audit completed. Summary of discovered ports:
Protocol Local_Address Port PID Program
tcp 0.0.0.0 22 842 sshd
tcp 0.0.0.0 80 1250 nginx:
tcp 0.0.0.0 443 1250 nginx:
tcp 127.0.0.1 3306 1100 mariadbd
udp 0.0.0.0 53 1024 named
The script parses netstat output with awk, isolates IP addresses, ports, PIDs, and program names, and generates a structured CSV audit report.
Pipelines, Redirection, Quoting, and Shell Expansion
When embedding netstat inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.
Permissions, Security Implications, and Safe Execution
The netstat command is strictly a read-only diagnostic reporting utility. It cannot modify kernel routing tables, close sockets, or alter interface configurations directly.
When running netstat as an unprivileged user, the PID and Program name column is left blank (displaying a hyphen -), because Linux prevents unprivileged accounts from reading symbolic links inside other users' /proc/[pid]/fd directories. Always execute netstat with sudo to audit services accurately.
Omitting the -n flag can leak sensitive internal IP information to external DNS servers: when netstat attempts reverse DNS resolution on public connection addresses, internal hostnames and private IP relationships may be transmitted in cleartext DNS queries over port 53.
GNU/Linux, BSD, and BusyBox Implementation Differences
On modern Linux distributions (such as Ubuntu 20.04+, Debian 11+, RHEL 8+, and Fedora), the net-tools package is not installed by default and must be explicitly installed via apt install net-tools or dnf install net-tools. Modern documentation strongly recommends migrating to ss -tulpn and ip route show.
On BSD systems (FreeBSD, OpenBSD, NetBSD) and macOS, netstat is the primary, native networking tool and is not deprecated. However, BSD netstat uses different command-line flags (for example, netstat -an -f inet or netstat -r) and provides different output formatting compared to the Linux net-tools variant.
Troubleshooting Common Errors and Diagnostics
netstat: command not found
Root Cause: The legacy net-tools package is not installed on modern Linux distributions by default.
Resolution: Install net-tools using your distribution's package manager, or use the modern replacement command ss -tulpn.
sudo apt-get install -y net-tools || sudo dnf install -y net-tools
# Or use modern iproute2:
sudo ss -tulpn
netstat hangs or runs extremely slowly
Root Cause: Netstat is performing reverse DNS lookups for every active connection, causing delays when DNS servers are slow or unreachable.
Resolution: Always pass the -n flag to suppress DNS resolution and output raw numeric addresses.
netstat -tan
PID and Program name column displays only '-'
Root Cause: The command was executed without administrative privileges, preventing netstat from reading /proc/[pid]/fd directories of processes owned by other users.
Resolution: Execute netstat with sudo to inspect process ownership across all users.
sudo netstat -tulnp
Alternatives, Related Commands, and Related Tutorials
- nslookup Command in Linux: Query DNS Records and Name Servers — Practical syntax, flags, and examples for
nslookup. - How to Check Open Ports in Linux: nmap, netcat, and Bash — Practical syntax, flags, and examples for
nc. - ss Command in Linux: Display Socket Statistics and Listening Ports — Practical syntax, flags, and examples for
ss. - Netcat (nc) Command in Linux: Port Scanning, Transfers, and Debugging — Practical syntax, flags, and examples for
nc. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
/proc/net, which causes significant CPU locking and latency on servers with thousands of connections. ss uses kernel Netlink sock_diag sockets, delivering constant-time binary socket retrieval without locking.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides



Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!