Article count:1385 Read by:1972833

Featured Content
Account Entry

Netstat command, understand it in one article

Latest update time:2021-04-06
    Reads:

Preface

In backend development, the netstat command can never be bypassed. Not only is it often used at work, but it is also tested frequently in interviews. The netstat command has many corresponding options and is relatively powerful. Netstat is commonly used to view the status of background service processes.

netstat can be used to print statistics on network connections, routing tables, and connections. Let’s learn about it below.


Print all connections

Use -a to list all connections.

[root@VM-16-9-centos ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 VM-16-9-centos:49326    169.254.0.55:lsi-bobcat ESTABLISHED
tcp        0     52 VM-16-9-centos:ssh      119.137.1.7:20029       ESTABLISHED
tcp        0      0 VM-16-9-centos:ssh      113.110.224.255:63626   ESTABLISHED
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN     
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 VM-16-9-centos:ntp      0.0.0.0:*                          
udp        0      0 VM-16-9-centos:ntp      0.0.0.0:*                          
udp        0      0 0.0.0.0:38064           0.0.0.0:*                          
udp6       0      0 VM-16-9-centos:ntp      [::]:*                             
udp6       0      0 VM-16-9-centos:ntp      [::]:* 


Print TCP or UDP connection

Use the -t option to list connections for the TCP protocol.

[root@VM-16-9-centos ~]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 VM-16-9-centos:49326    169.254.0.55:lsi-bobcat ESTABLISHED
tcp        0     52 VM-16-9-centos:ssh      119.137.1.7:20029       ESTABLISHED
tcp        0      0 VM-16-9-centos:ssh      113.110.224.255:63626   ESTABLISHED
tcp        0      0 VM-16-9-centos:ssh      vps-d395feee.vps.:39128 ESTABLISHED


Use the -u option to list connections for the UDP protocol.

[root@VM-16-9-centos ~]# netstat -u
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State 


Disable reverse domain name resolution

Use the -n option to disable domain name resolution, which can speed up lookups.

[root@VM-16-9-centos ~]# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 172.17.16.9:49326       169.254.0.55:5574       ESTABLISHED
tcp        0     52 172.17.16.9:22          119.137.1.7:20029       ESTABLISHED
tcp        0      0 172.17.16.9:22          113.110.224.255:63626   ESTABLISHED
tcp        0      0 172.17.16.9:52236       169.254.0.4:80          TIME_WAIT  


Display the corresponding program identification code and program name

Use the -p option to list listening sockets.

[root@VM-16-9-centos ~]# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 VM-16-9-centos:49326    169.254.0.55:lsi-bobcat ESTABLISHED 1400/YDService      
tcp        0     52 VM-16-9-centos:ssh      119.137.1.7:20029       ESTABLISHED 22131/sshd: root@pt 
tcp        0      0 VM-16-9-centos:ssh      host-188-14-121-2:55529 TIME_WAIT   -                   
tcp        0      0 VM-16-9-centos:ssh      113.110.224.255:63626   ESTABLISHED 32201/sshd: root@pt 


Print listening connections

Use the -l option to list listening sockets.

[root@VM-16-9-centos ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN     
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          


Stop and spend 30 seconds admiring a beautiful European-style building. The architecture is like something out of a fairy tale when I was a kid, and I feel comfortable looking at it. Learn next.

Print connection process information

Use the -p option to view process information.

[root@VM-16-9-centos ~]# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 VM-16-9-centos:49326    169.254.0.55:lsi-bobcat ESTABLISHED 1400/YDService      
tcp        0      0 VM-16-9-centos:48680    169.254.0.55:webcache   TIME_WAIT   -                   
tcp        0     52 VM-16-9-centos:ssh      119.137.1.7:20029       ESTABLISHED 22131/sshd: root@pt 
tcp        0      0 VM-16-9-centos:ssh      113.110.224.255:63626   ESTABLISHED 32201/sshd: root@pt 
tcp        0      0 VM-16-9-centos:ssh      host-188-14-121-2:56025 ESTABLISHED 10384/sshd: unknown                         


Print network work information statistics table

Use the -s option to view process information (only partial results are displayed).

[root@VM-16-9-centos ~]# netstat -s
Ip:
    7502855 total packets received
    0 forwarded
    0 incoming packets discarded
    7502837 incoming packets delivered
    7624338 requests sent out
    40 dropped because of missing route
Icmp:
    938052 ICMP messages received
    147 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 411
        timeout in transit: 8
        redirects: 2
        echo requests: 937623
        echo replies: 7
        timestamp request: 1
    946258 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 8634
        echo replies: 937623
        timestamp replies: 1   


Print network interface

Use the -i option to view process information.

[root@VM-16-9-centos ~]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500  7825094      0      0 0       7712988      0      0      0 BMRU
lo              65536    10230      0      0 0         10230      0      0      0 LRU                     


Continuously output information

Use the -c option to view process information.

[root@VM-16-9-centos ~]# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 VM-16-9-centos:49326    169.254.0.55:lsi-bobcat ESTABLISHED
tcp        0     52 VM-16-9-centos:ssh      119.137.1.7:20029       ESTABLISHED
tcp        0      0 VM-16-9-centos:ssh      vps-d395feee.vps.:39442 ESTABLISHED
tcp        0      0 VM-16-9-centos:ssh      113.110.224.255:63626   ESTABLISHED
tcp        0      0 VM-16-9-centos:52660    169.254.0.4:http        TIME_WAIT  
tcp        0      0 VM-16-9-centos:ssh      b3d278e1.virtua.c:35113 TIME_WAIT  
tcp        0      0 VM-16-9-centos:ssh      58.230.147.230:56693    TIME_WAIT  
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         DGRAM                    7444     /run/systemd/notify                     


Check the port based on the process pid

Use the grep command to find out the listening port through the process pid.

[root@VM-16-9-centos test]# netstat -nap|grep 12178
tcp6       0      0 :::8888                 :::*                    LISTEN      12178/gin                     


Check process based on port

Use the grep command to find out the process pid through the listening port .

[root@VM-16-9-centos test]# netstat -nap|grep 8888
tcp6       0      0 :::8888                 :::*                    LISTEN      12178/gin                     


Statistics network socket status

Use the awk command to count the number corresponding to the socket status.

[test]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 
ESTABLISHED 5
TIME_WAIT 2                    


babble

The netstat command is not difficult, but it has many options and is commonly used in work, so be sure to master it. This article can be used as a reference manual for everyone's netstat.


end



Click to follow the official account and reply [1024] to receive free study materials

Recommended reading


 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号