1. netstat命令,一文搞懂

        共 18066字,需瀏覽 37分鐘

         ·

        2021-03-26 07:48



        前言


        后臺(tái)開發(fā),netstat命令總是繞不過,不僅工作中經(jīng)常用過,面試也是考的多。netstat命令,對(duì)應(yīng)的選項(xiàng)比較多,功能比較強(qiáng)大。netstat 常用來查看,后臺(tái)服務(wù)進(jìn)程的相關(guān)狀態(tài)。

        netstat 可以用來打印網(wǎng)絡(luò)連接、路由表、連接的數(shù)據(jù)統(tǒng)計(jì)。下面我們來學(xué)習(xí)一下。


        打印所有連接

        使用 -a,列出所有連接。

        [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      [::]:* 


        打印 TCP 或 UDP 連接

        使用 -t 選項(xiàng)列出 TCP 協(xié)議的連接。

        [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


        使用 -u 選項(xiàng)列出 UDP 協(xié)議的連接。

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

        禁用反向域名解析

        使用 -n 選項(xiàng)禁用域名解析功能,這樣可以加快查找速度。

        [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  

        顯示對(duì)應(yīng)的程序識(shí)別碼和程序名稱

        使用 -p 選項(xiàng)列出正在監(jiān)聽的套接字。

        [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 


        打印監(jiān)聽中的連接

        使用 -l 選項(xiàng)列出正在監(jiān)聽的套接字。

        [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:*                          


        停下來,花30秒時(shí)間,欣賞一幅美麗的歐式建筑。建筑就像小時(shí)候童話里的,看著感覺心情舒暢。接下來接著學(xué)。

        打印連接進(jìn)程信息

        使用 -p 選項(xiàng)查看進(jìn)程信息。

        [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                         


        打印網(wǎng)絡(luò)工作信息統(tǒng)計(jì)表

        使用 -s 選項(xiàng)查看進(jìn)程信息(只展示部分結(jié)果)。

        [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   

        打印網(wǎng)絡(luò)接口

        使用 -i 選項(xiàng)查看進(jìn)程信息。

        [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                     


        持續(xù)輸出信息

        使用 -c 選項(xiàng)查看進(jìn)程信息。

        [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                     


        根據(jù)進(jìn)程pid查端口

        利用 grep 命令,通過進(jìn)程 pid 查出監(jiān)聽端口。

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

        根據(jù)端口查進(jìn)程

        利用 grep 命令,通過監(jiān)聽端口查出進(jìn)程 pid 。

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

        統(tǒng)計(jì)網(wǎng)絡(luò)socket狀態(tài)

        借助 awk 命令,統(tǒng)計(jì)socket狀態(tài)對(duì)應(yīng)的個(gè)數(shù)。

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

        絮叨

        netstat命令不難,但是選項(xiàng)比較多,工作中也常用,務(wù)必要掌握。這篇文章可以用作為大家 netstat 參考手冊(cè)。

        希望這篇文章能幫助到大家,下期再會(huì)!

        瀏覽 70
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 午夜鲁丝| 国产.com | 国产插到喷水 | 中文字幕一区二区三区免费2023 | 韩国黄色一级大片 |