Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Netstat is used when building production servers to collect statistics on network interfaces, such as TCP and UDP ports.

1. Introduction to commands

The main function of the netstat command is to collect statistics on network information. In fact, the spelling of the netstat command shows many things: netstat=network+statistics. Network stands for network, statistics stands for statistics, so the combination of the two represents the functions it can provide. To put it more simply, this command lets the user know what your computer is doing on the network.

Two, usage introduction

Netstat can display a lot of information, but you can use parameters to control what kind and style of information is displayed. Netstat -i The usual optional parameter is -i. The output will display a statistical list of all the network interfaces on your computer.

$netstat -i
Copy the code

It can be clearly seen that four pieces of information are listed, docker0 and veth171093D are all docker-related network interface information. Eth0 indicates the Ethernet interface information, and LO indicates Local Loopback.

In the following columns, RX stands for receive and TX for transmit, the most common form of communication.

  • Rx-ok: Indicates the correct number of packets received on this interface. OK = no problem, OK
  • Rx-err: indicates the number of error packets received on the interface. ERR, I’m sorry.
  • Rx-drp: indicates the number of discarded packets received by the interface. DRP is short for drop, meaning to drop.
  • Rx-ovr: Indicates the number of packets that were not received on this interface. OVR is short for over, meaning “over”.

Similarly, tx-OK, tx-err, tx-dr, and tx-OVr indicate the number of packets transmitted on the interface.

MTU is short for Maximum Transmission Unit. It refers to the Maximum size (in bytes) of packets that can pass through a layer of a communication protocol. Netstat -uta lists all open network connections.

$netstat -uta
Copy the code

The utA parameters represent:

  • -u: displays UDP connections (u is the first letter of UDP).
  • -t: displays TCP connections (t is the first letter of TCP).
  • -a: Displays all connections regardless of their status (A is the first letter of all).

If only TCP connection information is displayed:

$netstat -ta
Copy the code

Or display only UDP connection information (uncommon) :

$netstat -ua
Copy the code

The state column contains, but is not limited to, the following states:

  • ESTABLISHED: The ESTABLISHED connection to the remote computer is here.
  • TIME_WAIT: The connection is waiting for a packet to be processed on the network and begins to close the connection once it is processed. I have to wait for you.
  • CLOSE_WAIT: The remote server has terminated the connection (perhaps you have been inactive for too long). Close is an English word for “close”.
  • CLOSED: The connection is not used, it is CLOSED.
  • CLOSING: The connection is CLOSING, but some data is not sent.
  • LISTEN: Listens for incoming connections. The connection is not in use at this time. Listen and listen.

This is about the main content of Netstat, in addition, if you want to display the port information in numeric form, use the -n optional argument.