[Comprehensive explanation of Linux commands] 138. In-depth understanding of the netstat command: Linux network status information viewing tool

netstat

View network system status information in Linux

Additional information

netstatThe command is used to print the status information of the network system in Linux, allowing you to know the network status of the entire Linux system.

grammar

netstat(选项)

Options

  • -aOr --all: Display all connected Sockets;
  • -A<网络类型>Or --<网络类型>: list the relevant addresses in the connection of this network type;
  • -cOr --continuous: Continuously list network status;
  • -COr --cache: Display cache information of router configuration;
  • -eOr --extend: display other network-related information;
  • -FOr --fib: display FIB;
  • -gOr --groups: Display the list of members of the multicast function group;
  • -hOr --help: online help;
  • -iOr --interfaces: display the web interface information form;
  • -lOr --listening: Display the Socket of the server being monitored;
  • -MOr --masquerade: Display a disguised network connection;
  • -nOr --numeric: use the IP address directly without going through the domain name server;
  • -Nor --netlinkor --symbolic: Displays the symbolic link name of the network hardware peripheral;
  • -oor --timers: display timer;
  • -pOr --programs: Display the program identification code and program name that is using the Socket;
  • -ror --route: display routing table;
  • -sOr --statistics: Display network work information statistics table;
  • -tOr --tcp: Display the connection status of TCP transmission protocol;
  • -uOr --udp: Display the connection status of UDP transmission protocol;
  • -vOr --verbose: display the instruction execution process;
  • -VOr --version: display version information;
  • -wOr --raw: Display the connection status of RAW transmission protocol;
  • -xOr --unix: The effect of this parameter -A unixis the same as the specified parameter;
  • --ipOr --inet: The effect of this parameter -A inetis the same as the specified parameter.

Example

List all ports (listening and unlistening)

netstat -a     # 列出所有端口
netstat -at    # 列出所有TCP端口
netstat -au    # 列出所有UDP端口  

List all sockets in listening state

netstat -l        # 只显示监听端口
netstat -lt       # 只列出所有监听TCP端口
netstat -lu       # 只列出所有监听UDP端口
netstat -lx       # 只列出所有监听UNIX端口

Show statistics for each protocol

netstat -s   # 显示所有端口的统计信息
netstat -st  # 显示TCP端口的统计信息
netstat -su  # 显示UDP端口的统计信息

Show PID and process name in netstat output

netstat -pt
netstat -p`可以与其它开关一起使用,就可以添加“PID/进程名称”到netstat输出中,这样debugging的时候可以很方便的发现特定端口运行的程序。

在netstat输出中不显示主机、端口和用户名(host, port or user)

当你不想让主机、端口和用户名显示,使用`netstat -n`,将会使用数字代替那些名称。同样可以加速输出,因为不用进行比对查询。

 
netstat -an

If you don't want only one of the three names to be displayed, use the following command:

netsat -a --numeric-ports
netsat -a --numeric-hosts
netsat -a --numeric-users

Continuously output netstat information

netstat -c   # 每隔一秒输出网络信息

Display address families not supported by the system (Address Families)

netstat --verbose

At the end of the output, there will be the following information:

netstat: no support for `AF IPX' on this system.
netstat: no support for `AF AX25' on this system.
netstat: no support for `AF X25' on this system.
netstat: no support for `AF NETROM' on this system.

Display core routing information

netstat -r

Use netstat -rndisplay number format and do not query host names.

Find out the port the program is running on

Not all processes can be found, and those without permission will not be displayed. Use root permissions to view all information.

netstat -ap | grep ssh

Find the processes running on a specified port:

netstat -an | grep ':80'

Find process ID by port

netstat -anp | grep 8081 | grep LISTEN | awk '{printf $7}' | cut -d/ -f1

Show list of network interfaces

netstat -i

Display detailed information, such as ifconfig usage netstat -ie.

IP and TCP analysis

Check the IP addresses with the most connections to a certain service port:

netstat -ntu | grep :80 | awk '{print $5}' | cut -d: -f1 | awk '{++ip[$1]} END {for(i in ip) print ip[i],"\t",i}' | sort -nr

List of various TCP states:

netstat -nt | grep -e 127.0.0.1 -e 0.0.0.0 -e ::: -v | awk '/^tcp/ {++state[$NF]} END {for(i in state) print i,"\t",state[i]}'

Check the number of phpcgi processes. If it is close to the default value, it means it is not enough and needs to be increased:

netstat -anpo | grep "php-cgi" | wc -l

Expand knowledge

Detailed explanation of network connection status

There are a total of 12 possible states. The first 11 are described according to the three-way handshake when the TCP connection is established and the four-way wave process when the TCP connection is disconnected:

  • LISTEN: First, the server needs to open a socket to listen, with the status LISTEN, to listen for connection requests from the remote TCP port;
  • SYN_SENT: The client performs active open through the application call connect, so the client TCP sends a SYNrequest to establish a connection, then the status is set to SYN_SENT, and after sending the connection request, it waits for a matching connection request;
  • SYN_RECV: The server should send an ACK to confirm the client SYN, and at the same time send one to the client SYN, and then set the status to wait for the confirmation of the connection request after receiving and sending a connection request;
  • ESTABLISHED: Represents an open connection, the two parties can conduct or are already interacting with data, represents an open connection, and data can be transmitted to the user;
  • FIN_WAIT1: Active close (active close) end application call close, so its TCP sends FINa request to actively close the connection, and then enters the FIN_WAIT1 state, waiting for the remote TCP connection interruption request, or the confirmation of the previous connection interruption request;
  • CLOSE_WAIT: After receiving the passive close TCP FIN, it sends ACKa response FINrequest (its reception is also passed to the upper-layer application as an end-of-file character), and enters CLOSE_WAIT, waiting for a connection interruption request from the local user;
  • FIN_WAIT2: After actively closing the terminal ACK, it enters FIN-WAIT-2 and waits for the connection interruption request from the remote TCP;
  • LAST_ACK: After passively closing the end for a period of time, the application that receives the end-of-file character will call to CLOSEclose the connection, which causes its TCP to also send an FINACK and wait for the other party's ACK. It enters LAST-ACK and waits for the confirmation of the connection interruption request originally sent to the remote TCP;
  • TIME_WAIT: After being received by the active closing end FIN, TCP sends ACKthe packet and enters the TIME-WAIT state, waiting for enough time to ensure that the remote TCP receives acknowledgment of the connection interruption request;
  • CLOSING: Relatively rare, waiting for the remote TCP to confirm the connection interruption;
  • CLOSED: After receiving the ACK packet, the passive closed end enters the closed state, the connection ends, and there is no connection status;
  • UNKNOWN: Unknown Socket status;

Common flags

  • SYN(Synchronize Sequence Numbers) This flag is only valid when the three-way handshake establishes a TCP connection. Represents a new TCP connection request.
  • ACK(Acknowledgement Number) is a confirmation mark for the TCP request, and also prompts that the peer system has successfully received all data.
  • FIN(End flag, FINish) is used to end a TCP session. However, the corresponding port is still open and ready to receive subsequent data.

Learn from scratchpython

[Learn python from scratch] 92. Use Python’s requests library to send HTTP requests and process responses
[Learn python from scratch] 91. Use decorators and dictionaries to manage request paths in a simple web application
[Learn python from scratch] 93. Use dictionary management Request path
[Learn python from scratch] 89. Use WSGI to build a simple and efficient Web server
[Learn python from scratch] 88. Detailed explanation of WSGI interface: realize simple and efficient web development
[Learn python from scratch] 87. Manually build an HTTP server in Python Implementation and multi-threaded concurrent processing
[Learn python from scratch] 86. In-depth understanding of the HTTP protocol and its role in browser and server communication
[Learn python from scratch] 85. Application of parallel computing technology in Python process pool
[Learn python from scratch] ] 84. In-depth understanding of threads and processes
[Learn python from scratch] 83. Python multi-process programming and the use of process pools
[Learn python from scratch] 82. Chat program implementation based on multi-threading
[Learn python from scratch] 81. Python more Application of thread communication and queue
[Learn python from scratch] 80. Thread access to global variables and thread safety issues
[Learn python from scratch] 79. Thread access to global variables and thread safety issues
[Learn python from scratch] 78. File download case
[ Learn python from scratch] 77. TCP server programming and precautions
[learn python from scratch] 76. Server and client: key components of network communication
[learn python from scratch] 75. TCP protocol: reliable connection-oriented transmission layer communication protocol
[Learn python from scratch] 74. UDP network program: Detailed explanation of port issues and binding information
[Learn python from scratch] 73. UDP network program - sending data
[Learn python from scratch] 72. In-depth understanding of Socket communication and creation of sockets Method
[Learn python from scratch] 71. Network ports and their functions
[Learn python from scratch] 70. Network communication methods and their applications: from direct communication to routers to connect multiple networks
[Learn python from scratch] 69. Network communication and IP address classification analysis
[Learn python from scratch] 68. Greedy and non-greedy modes in Python regular expressions
[Learn python from scratch] 67. The re module in Python: regular replacement and advanced matching technology
[Learn python from scratch] 66 .In-depth understanding of regular expressions: a powerful tool for pattern matching and text processing
[Learn python from scratch] 65. Detailed explanation of Python regular expression modifiers and their applications
[Learn python from scratch] 64. The re.compile method in Python regular expressions Detailed explanation of usage
[Learn python from scratch] 63. Introduction to the re.Match class and its attributes and methods in regular expressions
[Learn python from scratch] 62. Python regular expressions: a powerful string matching tool
[Learn python from scratch] 61. Detailed explanation and application examples of property attributes in Python
[Learn python from scratch] 60. Exploration generator: a flexible tool for iteration
[Learn python from scratch] 59. Iterator: An efficient tool for optimizing data traversal
[Learn python from scratch] 58. Custom exceptions in Python and methods of raising exceptions
[Learn python from scratch] 57. Use the with keyword in Python to correctly close resources
[Learn python from scratch] 56. The importance and application of exception handling in programming
[Learn python from scratch] 55. Serialization and sum in Python Deserialization, application of JSON and pickle modules
[Learn python from scratch] 54. Writing data in memory
[Learn python from scratch] 53. CSV files and Python’s CSV module
[Learn python from scratch] 52. Reading and writing files - Python file operation guide
[Learn python from scratch] 51. Opening and closing files and their applications in Python
[Learn python from scratch] 49. Object-related built-in functions in Python and their usage
[Learn python from scratch] 48 .Detailed explanation of inheritance and multiple inheritance in Python
[Learn python from scratch] 47. The concept and basic use of inheritance in object-oriented programming
[Learn python from scratch] 46. Analysis of __new__ and __init__ methods and singletons in Python Design Patterns
[Learn python from scratch] 45. Class methods and static methods in Python
[Learn python from scratch] 44. Private properties and methods in object-oriented programming
[Learn python from scratch] 43. Examples in Python object-oriented programming Properties and class attributes
[Learn python from scratch] 42. Built-in properties and methods in Python
[Learn python from scratch] 41. python magic method (2)
[Learn python from scratch] 40. python magic method (1)
[Learn python from scratch] 39. Basic object-oriented syntax and application examples
[Learn python from scratch] 38. How to use and import Python packages
[Learn python from scratch] 37. The use and precautions of Python custom modules
[Learn python from scratch] Learn python] 36. Methods and techniques of using pip for third-party package management in Python
[Learn python from scratch] 35. Python common system modules and their usage
[Learn python from scratch] 34. Detailed explanation of the import and use of Python modules
[ Learn python from scratch] 33. The role of decorators (2)
[Learn python from scratch] 32. The role of decorators (1)
[Learn python from scratch] 31. In-depth understanding of higher-order functions and closures in Python
[From Learn python from scratch】30. In-depth understanding of recursive functions and anonymous functions
【learn python from scratch】29. "Detailed explanation of function parameters" - understand the different uses of Python function parameters
【learn python from scratch】28. Local variables and global variables in Python Variables
[Learn python from scratch] 27. The use of Python functions and nested calls
[Learn python from scratch] 25. Functions: a tool to improve the efficiency of code writing
[Learn python from scratch] 24. String operations and traversal methods in Python
[Learn python from scratch] 23. How to use sets (set) and common operations in Python
[Learn python from scratch] 22. Add, delete, modify, and query dictionary variables in Python
[Learn python from scratch] 21. Python tuples and dictionaries
[Learn python from scratch] 20. Python list operation skills and examples
[Learn python from scratch] 19. Applications of looping through lists and list nesting
[Learn python from scratch] 18. Detailed explanation of the basic operations of Python lists (1)
[From Learning python from scratch】 17. The format method of Python strings (2)
【Learning python from scratch】 16. The format method of Python strings (1)
【Learning python from scratch】 15. In-depth understanding of strings and character set encoding
【From Learning python from scratch】14. Common operations on Python strings (2)
【Learning python from scratch】13. Common operations on Python strings (1)
【Learning python from scratch】12. Python string operations and applications
【Learning python from scratch】 11. Python loop statements and control flow
[Learn python from scratch] 10. Detailed explanation of Python conditional statements and if nesting
[Learn python from scratch] 09. Conditional judgment statements in Python
[Learn python from scratch] 08. Python understands bit operations operator, operator priority
[Learn python from scratch] 07. Detailed explanation of Python operators: assignment, comparison and logical operators
[Learn python from scratch] 06. Use arithmetic operators in Python for calculations and string concatenation
[Learn from scratch] python ] 05. Output and input in Python
[Learn python from scratch] 04. Basics of Python programming: variables, data types and identifiers
[Learn python from scratch] 03. Python interactive programming and detailed explanation of comments
[Learn python from scratch] 02. Introduction to development tools
[Learn python from scratch] 01. Install and configure python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132896625