Operations 8 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering netstat: Essential Commands to Inspect Linux Network Connections

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 -a

Print 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 -u

Disable reverse DNS lookup

The -n option prevents hostname resolution, speeding up output.

[root@VM-16-9-centos ~]# netstat -n

Show program PID and name

Adding -p lists the owning process ID and program name for each socket.

[root@VM-16-9-centos ~]# netstat -p

Print listening sockets

Use -l to show only sockets that are in a listening state.

[root@VM-16-9-centos ~]# netstat -l

Continuous output

The -c flag refreshes the display continuously.

[root@VM-16-9-centos ~]# netstat -c

Find 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 12178

Find process by port

Similarly, filter by port number to retrieve the owning PID.

[root@VM-16-9-centos test]# netstat -nap | grep 8888

Count 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 -s

Interface 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-lineSysadminNetwork Monitoringnetstatnetwork sockets
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.