Mastering netstat: Essential Commands to Inspect Linux Network Connections
This guide explains the most useful netstat options for Linux, showing how to list all connections, filter TCP or UDP sockets, disable DNS lookups, display process IDs, monitor listening ports, continuously refresh output, and use grep or awk to map ports to processes and summarize socket states.
Introduction
The netstat command is a powerful tool for inspecting network connections, routing tables, and interface statistics on Linux systems. It is frequently used by backend developers, system administrators, and during technical interviews.
Print all connections
Use the -a flag to display every socket, including listening and established connections.
[root@VM-16-9-centos ~]# netstat -aPrint TCP or UDP connections
Use -t for TCP only and -u for UDP only.
[root@VM-16-9-centos ~]# netstat -t [root@VM-16-9-centos ~]# netstat -uDisable reverse DNS lookup
The -n option prevents hostname resolution, speeding up output.
[root@VM-16-9-centos ~]# netstat -nShow program PID and name
Adding -p lists the owning process ID and program name for each socket.
[root@VM-16-9-centos ~]# netstat -pPrint listening sockets
Use -l to show only sockets that are in a listening state.
[root@VM-16-9-centos ~]# netstat -lContinuous output
The -c flag refreshes the display continuously.
[root@VM-16-9-centos ~]# netstat -cFind port by PID
Combine netstat -nap with grep to locate the port used by a specific process ID.
[root@VM-16-9-centos test]# netstat -nap | grep 12178Find process by port
Similarly, filter by port number to retrieve the owning PID.
[root@VM-16-9-centos test]# netstat -nap | grep 8888Count socket states with awk
Use awk to tally the number of sockets in each state.
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'Network statistics
The -s option provides a summary of protocol statistics such as total packets, ICMP messages, and error counts.
[root@VM-16-9-centos ~]# netstat -sInterface table
Use -i to display the kernel interface table with packet counters. [root@VM-16-9-centos ~]# netstat -i These examples form a concise reference for using netstat to diagnose and monitor Linux network activity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
