DEV Community

Nic
Nic

Posted on • Originally published at coderscat.com on

Basic Networking Tool: netstat

CAPTURE-2020_03_09_basic-networking-tool-netstat.org_20200310_100143.png

netstat is a command-line networking tool, which can be used to show the statistics of networking connections. It’s a must tool for finding the performance problems in Unix/Linux networking.

In this post, I will list the typical usages and most used options of netstat.

List all the sockets

Use -a option to list all the sockets, including those in listen state and also not in listen state.

Use -l option to list the sockets in listen status.

netstat -a
Enter fullscreen mode Exit fullscreen mode

2020_03_09_basic-networking-tool-netstat.org_20200310_100508.png

List only TCP sockets

Use -t option to list only the TCP sockets.

netstat -tnetstat -lt # list all TCP sockets in listen status
Enter fullscreen mode Exit fullscreen mode

CAPTURE-2020_03_09_basic-networking-tool-netstat.org_20200310_095719.png

Find the related process

Use -p option to show the program name and pid with sockets.

netstat -ltp # list all TCP sockets in listen status, with the program name
Enter fullscreen mode Exit fullscreen mode

2020_03_09_basic-networking-tool-netstat.org_20200310_104252.png

Suppose we want to find which process is binding a specific socket.

netstat -lnp | grep 1234
Enter fullscreen mode Exit fullscreen mode

2020_03_09_basic-networking-tool-netstat.org_20200310_104442.png

Use pipeline for statistics of networking

With the pipeline, we could combine netstat with other command utilities, like awk, sort.

Let’s find all the counts for different networking status.

2020_03_09_basic-networking-tool-netstat.org_20200310_105712.png

Note:

awk '{print $6}' : use just the 6th column from the output.

tail -n + 2 : remove the first line from output (since it’s a header)

The post Basic Networking Tool: netstat appeared first on CodersCat.

Top comments (0)