Linux netstat can still play like this

A chance to fish, I saw a friend in the group asked what the netstat -tnpl command does. This command has been used a lot, but I haven't really studied it, but this is a problem, I can't let it go I have passed it, and I use the netstat command to query the listening status of the port every day, so take this opportunity to understand the netstat command together.

The netstat command is compatible under Linux, Windows and MacOS operating systems. The difference is that the command for netstat to display detailed information under UNIX is man netstat , while under Linux and Windows it is netstat --help .

netstat command under Linux

When I executed the man netstat command under Linux, the prompt displayed by the system really surprised me. It seems that this command has a lot of content! Don't be intimidated by such a long description, let's go step by step.

image-20220114192905308

First, let's understand what the netstat command does.

The official explanation given to us is that

netstat -- show network status

list network status

But what about the state of the network? With doubts, I executed it under Linux.

image-20220114191156926

The print is a six-tuple, and the content of each column of the six-tuple is

image-20220114210858771

A closer look at the six-tuple seems to indicate that the netstat command is a command-line tool for monitoring incoming and outgoing network connections and status .

On the whole, the output of netstst can be divided into two parts, one is Active Internet connections , called active TCP connections , where Recv-Q and Send-Q refer to the client sending queue and the client receiving queue. The values ​​of these two queues are generally 0. If it is not 0, it means that there are messages piled up and have not been sent/taken out. This situation is rarely seen.

The other part is Active UNIX domain sockets , called active Unix domain sockets. The sockets in this part are the same as network socket sockets. The difference is that this piece can only be used for local communication, and its performance is higher than that of network sockets. . Active UNIX domain sockets is also a six-tuple, representing

image-20220114213625117

netstat parameter definition

Let's explain some of the parameters listed in netstat --help. Let's start with the most common parameters, so that everyone can form a staged memory and not lose focus.

netstat -a

-a This parameter will monitor all socket connections by default.

image-20220114215136010

Including those that have been monitored, those that have established connections, those sent by the client that are waiting for the server, and those that have not been monitored will be listed.

netstat -at/-t

The suffixes netstat -at and netstat -t are both used to monitor ports related to the TCP protocol. The difference is that netstat -at listens to ports in all states, while netstat -t only listens to ports in ESTABLISHED state. .

netstat -at

image-20220114222513317

netstat -t

image-20220114222526237

netstat -au/-u

Similarly, netstat -au and netstat -u both monitor ports related to UDP, the difference is that netstat -au listens to all ports in the state, while netstat -u only listens to ports in the ESTABLISHED state.

netstat -au

image-20220114222955527

netstat -u

image-20220114223046771

My test here is not to monitor the UDP protocol in the established connection state.

netstat -ap

This command is used to list the ports on which the program is running. The commonly used commands are

netstat -ap|grep '程序名'

For example, if we are looking for an http program, it is Netstat -ap|grep http

image-20220117205646573

You can also list the port number directly

netstat -ap|grep 8080

image-20220117210019303

However, it should be noted that not all programs can be found, and those without permissions will not be displayed. You can query all information with root permissions.

netstat -l

netstat -l is used to monitor the port that is in the listening state (it is really hard to read, in order to read more smoothly, use the 列出replacement monitor directly later).

image-20220114223946803

netstat -lt is only used to list all listening TCP ports.

image-20220117183747925

netstat -lu is only used to list all listening UDP ports.

image-20220117185030035

netstat -lx is only used to list all listening UNIX ports.

image-20220117185216268

netstat -s

netstat -s is used to list statistics for all ports.

image-20220117185307357

netstat -st is used to list statistics of TCP ports.

image-20220117185401696

netstat -su is used to list statistics for UDP ports.

image-20220117185645981

netstat -p

netstat -p can be used with other parameters, eg netstat -pt can list the service name and PID number.

image-20220117185913422

netstat -c

Using netstat -c will list network information every second.

image-20220117190501847

netstat -r

netstat -r is used to list routing core information.

image-20220117190549020

netstat --verbose

This command will list the Address Family supported by the system .

image-20220117191102190

Address Family simply means which communication protocol is used by the bottom layer to submit data. For example, AF_INET uses TCP/IPv4; AF_INET6 uses TCP/IPv6; and AF_LOCAL or AF_UNIX refers to local communication (that is, this time Communication is the communication between processes on the current host), which is generally specified in the form of an absolute path.

netstat -i

netstat -i is used to list network interface packets, including transmitting and receiving packets with MTU (maximum transmission unit).

image-20220117192723454

In addition, netstat -ie is also used to list the kernel interface table, very similar to the ifconfig command

image-20220117193432779

about this issue

So, going back to the question at the beginning of the article, what is netstat -tnpl used for, in fact, this is a combination of several parameters

  • -t : only list information related to tcp
  • -n: list numerically
  • -p: list the socket PID and program name in use
  • -l: List listening server sockets

Let's execute this command.

image-20220117214326520

In addition, in Linux, it has been recommended to use ss instead of netstat, use ip route instead of netstat -r, use ip -s link instead of netstat -i, and use ip addr instead of netstat -g.

Original link: Can netstat still play like this?

If this article is helpful to you, I hope you like it and follow it!

Guess you like

Origin blog.csdn.net/qq_36894974/article/details/122935788