天天看點

netstat 指令詳解 netstat指令是一個監控TCP/IP網絡的非常有用的工具,它可以顯示路由表、實際的網絡連接配接以及每一個網絡接口裝置的狀态資訊。

文法選項

netstat [選項]

-a或--all:顯示所有連線中的Socket; 
-A<網絡類型>或--<網絡類型>:列出該網絡類型連線中的相關位址; 
-c或--continuous:持續列出網絡狀态; 
-C或--cache:顯示路由器配置的快取資訊; 
-e或--extend:顯示網絡其他相關資訊; 
-F或--fib:顯示FIB; 
-g或--groups:顯示多重廣播功能群組組員名單; 
-h或--help:線上幫助; 
-i或--interfaces:顯示網絡界面資訊表單; 
-l或--listening:顯示監控中的伺服器的Socket; 
-M或--masquerade:顯示僞裝的網絡連線; 
-n或--numeric:直接使用ip位址,而不通過域名伺服器; 
-N或--netlink或--symbolic:顯示網絡硬體外圍裝置的符号連接配接名稱; 
-o或--timers:顯示計時器; 
-p或--programs:顯示正在使用Socket的程式識别碼和程式名稱; 
-r或--route:顯示Routing Table; 
-s或--statistice:顯示網絡工作資訊統計表; 
-t或--tcp:顯示TCP傳輸協定的連線狀況; 
-u或--udp:顯示UDP傳輸協定的連線狀況; 
-v或--verbose:顯示指令執行過程; 
-V或--version:顯示版本資訊; 
-w或--raw:顯示RAW傳輸協定的連線狀況; 
-x或--unix:此參數的效果和指定"-A unix"參數相同; 
--ip或--inet:此參數的效果和指定"-A inet"參數相同。           

例子

  1. 列出所有端口情況
[root@xiesshavip002 ~]# netstat -a      # 列出所有端口
[root@xiesshavip002 ~]# netstat -at     # 列出所有TCP端口
[root@xiesshavip002 ~]# netstat -au     # 列出所有UDP端口           
  1. 列出所有處于監聽狀态的 Sockets
[root@xiesshavip002 ~]# netstat -l   # 隻顯示監聽端口
[root@xiesshavip002 ~]# netstat -lt  # 顯示監聽TCP端口
[root@xiesshavip002 ~]# netstat -lu  # 顯示監聽UDP端口
[root@xiesshavip002 ~]# netstat -lx  # 顯示監聽UNIX端口           
  1. 顯示每個協定的統計資訊
[root@xiesshavip002 ~]# netstat -s     # 顯示所有端口的統計資訊
[root@xiesshavip002 ~]# netstat -st    # 顯示所有TCP的統計資訊
[root@xiesshavip002 ~]# netstat -su    # 顯示所有UDP的統計資訊           
  1. 顯示 PID 和程序名稱
[root@xiesshavip002 ~]# netstat -p           
  1. 顯示核心路由資訊
[root@xiesshavip002 ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 eth0
192.168.130.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
[root@xiesshavip002 ~]# netstat -rn   # 顯示數字格式,不查詢主機名稱
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.130.1   0.0.0.0         UG        0 0          0 eth0
192.168.130.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
[root@xiesshavip002 ~]#           
  1. 檢視端口和服務
[root@xiesshavip002 ~]# netstat -antp | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      734/sshd            
tcp        0     52 192.168.130.20:22       119.129.118.189:58737   ESTABLISHED 1846/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      734/sshd            
[root@xiesshavip002 ~]# netstat -antp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      734/sshd            
tcp        0     52 192.168.130.20:22       119.129.118.189:58737   ESTABLISHED 1846/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      734/sshd            
[root@xiesshavip002 ~]#           

netstat --help

[root@xiesshavip002 ~]# netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]

        -r, --route              display routing table
        -I, --interfaces=<Iface> display interface table for <Iface>
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don\'t truncate IP addresses
        -n, --numeric            don\'t resolve names
        --numeric-hosts          don\'t resolve host names
        --numeric-ports          don\'t resolve port names
        --numeric-users          don\'t resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -o, --timers             display timers
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all                display all sockets (default: connected)
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB
        -Z, --context            display SELinux security context for sockets

  <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-w|--raw} {-x|--unix}
           --ax25 --ipx --netrom
  <AF>=Use \'-6|-4\' or \'-A <af>\' or \'--<af>\'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 
[root@xiesshavip002 ~]#