netstat命令
netstat命令用於顯示與IP、TCP、UDP和ICMP協議相關的統計數據,一般用於檢驗本機各端口的網絡連接情況。netstat
是在內核中訪問網絡及相關信息的程序,它能提供TCP連接,TCP和UDP監聽,進程內存管理的相關報告。
如果計算機有時候接收到的數據報導致出錯數據或故障,不必感到奇怪,TCP/IP可以容許這些類型的錯誤,並能夠自動重發數據報。但如果累計的出錯情況數目佔到所接收的IP數據報相當大的百分比,或者它的數目正迅速增加,那麼就應該使用netstat
查一查爲什麼會出現這些情況了。
1.命令格式
netstat [-acCeFghilMnNoprstuvVwx][-A<網絡類型>][--ip]
2.命令功能
netstat
用於顯示與IP、TCP、UDP和ICMP協議相關的統計數據,一般用於檢驗本機各端口的網絡連接情況。
3.命令參數
-
-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」參數相同。
4.使用實例
實例1:無參數使用
命令:
netstat
輸出:
[yiibai@localhost ~]$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 13100 /run/systemd/shutdownd
unix 2 [ ] DGRAM 8385 /run/systemd/notify
unix 2 [ ] DGRAM 8387 /run/systemd/cgroups-agent
unix 5 [ ] DGRAM 8398 /run/systemd/journal/socket
unix 13 [ ] DGRAM 8400 /dev/log
unix 3 [ ] STREAM CONNECTED 19870
unix 3 [ ] STREAM CONNECTED 19850
unix 3 [ ] STREAM CONNECTED 19871
unix 3 [ ] STREAM CONNECTED 19830
unix 3 [ ] STREAM CONNECTED 16422
unix 3 [ ] STREAM CONNECTED 19852
unix 3 [ ] STREAM CONNECTED 16423 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19873
unix 3 [ ] STREAM CONNECTED 19874
unix 3 [ ] STREAM CONNECTED 15501
unix 3 [ ] STREAM CONNECTED 19847
unix 3 [ ] STREAM CONNECTED 19821
unix 3 [ ] STREAM CONNECTED 19849
unix 3 [ ] STREAM CONNECTED 19844
unix 3 [ ] STREAM CONNECTED 19820
unix 3 [ ] STREAM CONNECTED 19876
unix 3 [ ] STREAM CONNECTED 19833
unix 3 [ ] STREAM CONNECTED 19877
unix 2 [ ] DGRAM 16501
unix 3 [ ] STREAM CONNECTED 16576
unix 3 [ ] STREAM CONNECTED 19879
unix 3 [ ] STREAM CONNECTED 19829
unix 2 [ ] DGRAM 80277
unix 3 [ ] STREAM CONNECTED 19880
unix 3 [ ] STREAM CONNECTED 15803 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19882
unix 3 [ ] STREAM CONNECTED 19263
unix 3 [ ] STREAM CONNECTED 19883
unix 3 [ ] STREAM CONNECTED 19853
unix 2 [ ] DGRAM 16412
unix 3 [ ] STREAM CONNECTED 19855
unix 3 [ ] STREAM CONNECTED 15802
unix 3 [ ] STREAM CONNECTED 19856
unix 3 [ ] STREAM CONNECTED 15502 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 80280
unix 3 [ ] STREAM CONNECTED 19858
unix 3 [ ] STREAM CONNECTED 19859
unix 3 [ ] STREAM CONNECTED 80281
unix 3 [ ] STREAM CONNECTED 19861
unix 3 [ ] STREAM CONNECTED 19843
unix 3 [ ] STREAM CONNECTED 19862
unix 3 [ ] STREAM CONNECTED 15457
unix 3 [ ] STREAM CONNECTED 19864
unix 3 [ ] STREAM CONNECTED 15613 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19865
unix 3 [ ] STREAM CONNECTED 19846
unix 2 [ ] DGRAM 80112
unix 3 [ ] STREAM CONNECTED 19867
unix 3 [ ] STREAM CONNECTED 16577 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19868
unix 2 [ ] DGRAM 15509
unix 3 [ ] STREAM CONNECTED 19264 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 16129 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 13393
unix 3 [ ] STREAM CONNECTED 19837
unix 2 [ ] DGRAM 13285
unix 3 [ ] STREAM CONNECTED 13530 /run/systemd/journal/stdout
unix 2 [ ] DGRAM 19784
unix 3 [ ] STREAM CONNECTED 16379 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 16160
unix 3 [ ] STREAM CONNECTED 13796 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15732 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15611
unix 3 [ ] STREAM CONNECTED 13777
unix 2 [ ] DGRAM 20011
unix 3 [ ] STREAM CONNECTED 16106 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19840
......
說明:
從整體上看,netstat
的輸出結果可以分爲兩個部分:
一個是Active Internet connections,稱爲有源TCP連接,其中」Recv-Q」和」Send-Q」指的是接收隊列和發送隊列。這些數字一般都應該是0。如果不是則表示軟件包正在隊列中堆積。這種情況只能在非常少的情況見到。
另一個是Active UNIX domain sockets,稱爲有源Unix域套接口(和網絡套接字一樣,但是隻能用於本機通信,性能可以提高一倍)。
Proto顯示連接使用的協議,RefCnt表示連接到本套接口上的進程號,Types顯示套接口的類型,State顯示套接口當前的狀態,Path表示連接到套接口的其它進程使用的路徑名。
套接口類型:
-
-t
:TCP -
-u
:UDP -
-raw
:RAW類型 -
--unix
:UNIX域類型 -
--ax25
:AX25類型 -
--ipx
:ipx類型 -
--netrom
:netrom類型
狀態說明:
-
LISTEN
:偵聽來自遠方的TCP端口的連接請求 -
SYN-SENT
:再發送連接請求後等待匹配的連接請求(如果有大量這樣的狀態包,檢查是否中招了) -
SYN-RECEIVED
:再收到和發送一個連接請求後等待對方對連接請求的確認(如有大量此狀態,估計被flood攻擊了) -
ESTABLISHED
:代表一個打開的連接 -
FIN-WAIT-1
:等待遠程TCP連接中斷請求,或先前的連接中斷請求的確認 -
FIN-WAIT-2
:從遠程TCP等待連接中斷請求 -
CLOSE-WAIT
:等待從本地用戶發來的連接中斷請求 -
CLOSING
:等待遠程TCP對連接中斷的確認 -
LAST-ACK
:等待原來的發向遠程TCP的連接中斷請求的確認(不是什麼好東西,此項出現,檢查是否被攻擊) -
TIME-WAIT
:等待足夠的時間以確保遠程TCP接收到連接中斷請求的確認 -
CLOSED
:沒有任何連接狀態
實例2:列出所有端口
命令:
netstat -a
輸出:
[yiibai@localhost ~]$ 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 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
udp 0 0 0.0.0.0:trnsprntproxy 0.0.0.0:*
udp 0 0 localhost:323 0.0.0.0:*
udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
udp6 0 0 [::]:rnm [::]:*
udp6 0 0 localhost:323 [::]:*
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 12805 /run/lvm/lvmpolld.socket
unix 2 [ ACC ] SEQPACKET LISTENING 12811 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 12827 /run/lvm/lvmetad.socket
unix 2 [ ] DGRAM 13100 /run/systemd/shutdownd
unix 2 [ ACC ] STREAM LISTENING 19872 public/showq
unix 2 [ ACC ] STREAM LISTENING 19842 private/rewrite
unix 2 [ ACC ] STREAM LISTENING 15231 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 19845 private/bounce
unix 2 [ ACC ] STREAM LISTENING 19848 private/defer
unix 2 [ ACC ] STREAM LISTENING 19851 private/trace
unix 2 [ ACC ] STREAM LISTENING 19854 private/verify
unix 2 [ ACC ] STREAM LISTENING 19860 private/proxymap
unix 2 [ ACC ] STREAM LISTENING 19863 private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 19866 private/smtp
unix 2 [ ACC ] STREAM LISTENING 19869 private/relay
unix 2 [ ACC ] STREAM LISTENING 19875 private/error
unix 2 [ ACC ] STREAM LISTENING 19878 private/retry
unix 2 [ ACC ] STREAM LISTENING 19881 private/discard
unix 2 [ ACC ] STREAM LISTENING 19884 private/local
unix 2 [ ACC ] STREAM LISTENING 19887 private/virtual
unix 2 [ ACC ] STREAM LISTENING 19890 private/lmtp
unix 2 [ ACC ] STREAM LISTENING 19893 private/anvil
unix 2 [ ACC ] STREAM LISTENING 19896 private/scache
unix 2 [ ACC ] STREAM LISTENING 19822 public/pickup
unix 2 [ ACC ] STREAM LISTENING 19832 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 19835 public/qmgr
unix 2 [ ACC ] STREAM LISTENING 19857 public/flush
unix 2 [ ACC ] STREAM LISTENING 17836 /var/run/NetworkManager/private-dhcp
unix 2 [ ] DGRAM 8385 /run/systemd/notify
unix 2 [ ] DGRAM 8387 /run/systemd/cgroups-agent
unix 2 [ ACC ] STREAM LISTENING 19839 private/tlsmgr
......
說明:顯示一個所有的有效連接信息列表,包括已建立的連接(ESTABLISHED),也包括監聽連接請(LISTENING)的那些連接。
實例3:顯示當前UDP連接狀況
命令:
netstat -nu
輸出:
[yiibai@localhost ~]$ netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
[yiibai@localhost ~]$
實例4:顯示UDP端口號的使用情況
命令:
netstat -apu
輸出:
[yiibai@localhost ~]$ netstat -apu
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:trnsprntproxy 0.0.0.0:* -
udp 0 0 localhost:323 0.0.0.0:* -
udp 0 0 0.0.0.0:bootpc 0.0.0.0:* -
udp6 0 0 [::]:rnm [::]:* -
udp6 0 0 localhost:323 [::]:* -
[yiibai@localhost ~]$
實例5:顯示網卡列表
命令:
netstat -i
輸出:
[yiibai@localhost ~]$ netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33 1500 63831 0 0 0 6313 0 0 0 BMRU
lo 65536 19 0 0 0 19 0 0 0 LRU
[yiibai@localhost ~]$
實例6:顯示組播組的關係
命令:
netstat -g
輸出:
[yiibai@localhost ~]$ netstat -g
IPv6/IPv4 Group Memberships
Interface RefCnt Group
--------------- ------ ---------------------
lo 1 all-systems.mcast.net
ens33 1 all-systems.mcast.net
lo 1 ff02::1
lo 1 ff01::1
ens33 1 ff02::1:ff35:dd8c
ens33 1 ff02::1
ens33 1 ff01::1
[yiibai@localhost ~]$
實例7:顯示網絡統計信息
命令:
netstat -s
輸出:
[yiibai@localhost ~]$ netstat -s
Ip:
28607 total packets received
0 forwarded
0 incoming packets discarded
4123 incoming packets delivered
4143 requests sent out
50 dropped because of missing route
Icmp:
767 ICMP messages received
402 input ICMP message failed.
ICMP input histogram:
destination unreachable: 152
timeout in transit: 164
echo requests: 402
echo replies: 49
467 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 9
echo request: 458
IcmpMsg:
InType0: 49
InType3: 152
InType8: 402
InType11: 164
OutType3: 9
OutType8: 458
Tcp:
3 active connections openings
3 passive connection openings
0 failed connection attempts
0 connection resets received
1 connections established
2880 segments received
2548 segments send out
0 segments retransmited
0 bad segments received.
0 resets sent
Udp:
474 packets received
2 packets to unknown port received.
0 packet receive errors
1160 packets sent
0 receive buffer errors
0 send buffer errors
UdpLite:
TcpExt:
3 TCP sockets finished time wait in fast timer
11 delayed acks sent
1 delayed acks further delayed because of locked socket
1 packets directly queued to recvmsg prequeue.
1139 packet headers predicted
441 acknowledgments not containing data payload received
1356 predicted acknowledgments
IPReversePathFilter: 1
TCPRcvCoalesce: 69
TCPOrigDataSent: 2427
IpExt:
InNoRoutes: 5
InMcastPkts: 435
InBcastPkts: 24444
OutBcastPkts: 402
InOctets: 2452235
OutOctets: 539407
InMcastOctets: 13920
InBcastOctets: 2027384
OutBcastOctets: 33768
InNoECTPkts: 28642
[yiibai@localhost ~]$
說明:
按照各個協議分別顯示其統計數據。如果我們的應用程序(如Web瀏覽器)運行速度比較慢,或者不能顯示Web頁之類的數據,那麼我們就可以用本選項來查看一下所顯示的信息。我們需要仔細查看統計數據的各行,找到出錯的關鍵字,進而確定問題所在。
實例8:顯示監聽的套接口
命令:
netstat -l
輸出:
[yiibai@localhost ~]$ 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
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
udp 0 0 0.0.0.0:trnsprntproxy 0.0.0.0:*
udp 0 0 localhost:323 0.0.0.0:*
udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
udp6 0 0 [::]:rnm [::]:*
udp6 0 0 localhost:323 [::]:*
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 12805 /run/lvm/lvmpolld.socket
unix 2 [ ACC ] SEQPACKET LISTENING 12811 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 12827 /run/lvm/lvmetad.socket
unix 2 [ ACC ] STREAM LISTENING 19872 public/showq
unix 2 [ ACC ] STREAM LISTENING 19842 private/rewrite
unix 2 [ ACC ] STREAM LISTENING 15231 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 19845 private/bounce
unix 2 [ ACC ] STREAM LISTENING 19848 private/defer
unix 2 [ ACC ] STREAM LISTENING 19851 private/trace
unix 2 [ ACC ] STREAM LISTENING 19854 private/verify
unix 2 [ ACC ] STREAM LISTENING 19860 private/proxymap
unix 2 [ ACC ] STREAM LISTENING 19863 private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 19866 private/smtp
unix 2 [ ACC ] STREAM LISTENING 19869 private/relay
unix 2 [ ACC ] STREAM LISTENING 19875 private/error
unix 2 [ ACC ] STREAM LISTENING 19878 private/retry
unix 2 [ ACC ] STREAM LISTENING 19881 private/discard
unix 2 [ ACC ] STREAM LISTENING 19884 private/local
unix 2 [ ACC ] STREAM LISTENING 19887 private/virtual
unix 2 [ ACC ] STREAM LISTENING 19890 private/lmtp
unix 2 [ ACC ] STREAM LISTENING 19893 private/anvil
unix 2 [ ACC ] STREAM LISTENING 19896 private/scache
unix 2 [ ACC ] STREAM LISTENING 19822 public/pickup
unix 2 [ ACC ] STREAM LISTENING 19832 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 19835 public/qmgr
unix 2 [ ACC ] STREAM LISTENING 19857 public/flush
unix 2 [ ACC ] STREAM LISTENING 17836 /var/run/NetworkManager/private-dhcp
unix 2 [ ACC ] STREAM LISTENING 19839 private/tlsmgr
unix 2 [ ACC ] STREAM LISTENING 8395 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 12794 /run/systemd/private
[yiibai@localhost ~]$
實例9:顯示所有已建立的有效連接
命令:
netstat -n
輸出:
[yiibai@localhost ~]$ netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 52 192.168.0.197:22 192.168.0.5:54835 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 13100 /run/systemd/shutdownd
unix 2 [ ] DGRAM 8385 /run/systemd/notify
unix 2 [ ] DGRAM 8387 /run/systemd/cgroups-agent
unix 5 [ ] DGRAM 8398 /run/systemd/journal/socket
unix 13 [ ] DGRAM 8400 /dev/log
unix 3 [ ] STREAM CONNECTED 19870
unix 3 [ ] STREAM CONNECTED 19850
unix 3 [ ] STREAM CONNECTED 19871
unix 3 [ ] STREAM CONNECTED 19830
unix 3 [ ] STREAM CONNECTED 16422
unix 3 [ ] STREAM CONNECTED 19852
unix 3 [ ] STREAM CONNECTED 16423 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19873
unix 3 [ ] STREAM CONNECTED 19874
unix 3 [ ] STREAM CONNECTED 15501
unix 3 [ ] STREAM CONNECTED 19847
unix 3 [ ] STREAM CONNECTED 19821
unix 3 [ ] STREAM CONNECTED 19849
unix 3 [ ] STREAM CONNECTED 19844
unix 3 [ ] STREAM CONNECTED 19820
unix 3 [ ] STREAM CONNECTED 19876
unix 3 [ ] STREAM CONNECTED 19833
unix 3 [ ] STREAM CONNECTED 19877
unix 2 [ ] DGRAM 16501
unix 3 [ ] STREAM CONNECTED 16576
unix 3 [ ] STREAM CONNECTED 19879
unix 3 [ ] STREAM CONNECTED 19829
unix 2 [ ] DGRAM 80277
unix 3 [ ] STREAM CONNECTED 19880
unix 3 [ ] STREAM CONNECTED 15803 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19882
unix 3 [ ] STREAM CONNECTED 19263
unix 3 [ ] STREAM CONNECTED 19883
unix 3 [ ] STREAM CONNECTED 19853
unix 2 [ ] DGRAM 16412
unix 3 [ ] STREAM CONNECTED 19855
unix 3 [ ] STREAM CONNECTED 15802
unix 3 [ ] STREAM CONNECTED 19856
unix 3 [ ] STREAM CONNECTED 15502 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 80280
unix 3 [ ] STREAM CONNECTED 19858
unix 3 [ ] STREAM CONNECTED 19859
unix 3 [ ] STREAM CONNECTED 80281
unix 3 [ ] STREAM CONNECTED 19861
unix 3 [ ] STREAM CONNECTED 19843
unix 3 [ ] STREAM CONNECTED 19862
unix 3 [ ] STREAM CONNECTED 15457
unix 3 [ ] STREAM CONNECTED 19864
unix 3 [ ] STREAM CONNECTED 15613 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19865
unix 3 [ ] STREAM CONNECTED 19846
unix 2 [ ] DGRAM 80112
unix 3 [ ] STREAM CONNECTED 19867
unix 3 [ ] STREAM CONNECTED 16577 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19868
unix 2 [ ] DGRAM 15509
unix 3 [ ] STREAM CONNECTED 19264 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 16129 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 13393
unix 3 [ ] STREAM CONNECTED 19837
unix 2 [ ] DGRAM 13285
unix 3 [ ] STREAM CONNECTED 13530 /run/systemd/journal/stdout
unix 2 [ ] DGRAM 19784
unix 3 [ ] STREAM CONNECTED 16379 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 16160
unix 3 [ ] STREAM CONNECTED 13796 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15732 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15611
unix 3 [ ] STREAM CONNECTED 13777
unix 2 [ ] DGRAM 20011
unix 3 [ ] STREAM CONNECTED 16106 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19840
unix 2 [ ] DGRAM 16231
unix 3 [ ] STREAM CONNECTED 16105
unix 3 [ ] STREAM CONNECTED 16320
unix 3 [ ] STREAM CONNECTED 19885
unix 3 [ ] DGRAM 13816
unix 3 [ ] STREAM CONNECTED 18433 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 16321 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19886
unix 3 [ ] STREAM CONNECTED 16161 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 16378
unix 3 [ ] STREAM CONNECTED 19888
unix 3 [ ] STREAM CONNECTED 15612
unix 3 [ ] STREAM CONNECTED 18432
unix 3 [ ] STREAM CONNECTED 19889
unix 2 [ ] DGRAM 16204
unix 3 [ ] STREAM CONNECTED 15731
unix 3 [ ] STREAM CONNECTED 19834
unix 3 [ ] STREAM CONNECTED 19891
unix 3 [ ] STREAM CONNECTED 19841
unix 3 [ ] STREAM CONNECTED 19836
unix 2 [ ] DGRAM 79889
unix 3 [ ] STREAM CONNECTED 19892
unix 2 [ ] DGRAM 16133
unix 3 [ ] STREAM CONNECTED 15152
unix 3 [ ] STREAM CONNECTED 16128
unix 3 [ ] STREAM CONNECTED 19894
unix 3 [ ] STREAM CONNECTED 19895
unix 3 [ ] STREAM CONNECTED 15153
unix 3 [ ] STREAM CONNECTED 19897
unix 3 [ ] STREAM CONNECTED 19898
unix 3 [ ] DGRAM 13817
unix 2 [ ] DGRAM 13797
unix 2 [ ] DGRAM 15143
[yiibai@localhost ~]$
實例10:顯示關於以太網的統計數據
命令:
netstat -e
輸出:
[yiibai@localhost ~]$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED root 80161
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 13100 /run/systemd/shutdownd
unix 2 [ ] DGRAM 8385 /run/systemd/notify
unix 2 [ ] DGRAM 8387 /run/systemd/cgroups-agent
unix 5 [ ] DGRAM 8398 /run/systemd/journal/socket
unix 13 [ ] DGRAM 8400 /dev/log
unix 3 [ ] STREAM CONNECTED 19870
unix 3 [ ] STREAM CONNECTED 19850
unix 3 [ ] STREAM CONNECTED 19871
unix 3 [ ] STREAM CONNECTED 19830
unix 3 [ ] STREAM CONNECTED 16422
unix 3 [ ] STREAM CONNECTED 19852
unix 3 [ ] STREAM CONNECTED 16423 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19873
unix 3 [ ] STREAM CONNECTED 19874
unix 3 [ ] STREAM CONNECTED 15501
unix 3 [ ] STREAM CONNECTED 19847
unix 3 [ ] STREAM CONNECTED 19821
unix 3 [ ] STREAM CONNECTED 19849
unix 3 [ ] STREAM CONNECTED 19844
unix 3 [ ] STREAM CONNECTED 19820
unix 3 [ ] STREAM CONNECTED 19876
unix 3 [ ] STREAM CONNECTED 19833
unix 3 [ ] STREAM CONNECTED 19877
unix 2 [ ] DGRAM 16501
unix 3 [ ] STREAM CONNECTED 16576
unix 3 [ ] STREAM CONNECTED 19879
unix 3 [ ] STREAM CONNECTED 19829
unix 2 [ ] DGRAM 80277
unix 3 [ ] STREAM CONNECTED 19880
unix 3 [ ] STREAM CONNECTED 15803 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19882
unix 3 [ ] STREAM CONNECTED 19263
unix 3 [ ] STREAM CONNECTED 19883
unix 3 [ ] STREAM CONNECTED 19853
unix 2 [ ] DGRAM 16412
unix 3 [ ] STREAM CONNECTED 19855
unix 3 [ ] STREAM CONNECTED 15802
unix 3 [ ] STREAM CONNECTED 19856
unix 3 [ ] STREAM CONNECTED 15502 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 80280
unix 3 [ ] STREAM CONNECTED 19858
unix 3 [ ] STREAM CONNECTED 19859
unix 3 [ ] STREAM CONNECTED 80281
unix 3 [ ] STREAM CONNECTED 19861
unix 3 [ ] STREAM CONNECTED 19843
unix 3 [ ] STREAM CONNECTED 19862
unix 3 [ ] STREAM CONNECTED 15457
unix 3 [ ] STREAM CONNECTED 19864
unix 3 [ ] STREAM CONNECTED 15613 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19865
unix 3 [ ] STREAM CONNECTED 19846
unix 2 [ ] DGRAM 80112
unix 3 [ ] STREAM CONNECTED 19867
unix 3 [ ] STREAM CONNECTED 16577 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19868
unix 2 [ ] DGRAM 15509
unix 3 [ ] STREAM CONNECTED 19264 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 16129 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 13393
unix 3 [ ] STREAM CONNECTED 19837
unix 2 [ ] DGRAM 13285
unix 3 [ ] STREAM CONNECTED 13530 /run/systemd/journal/stdout
unix 2 [ ] DGRAM 19784
unix 3 [ ] STREAM CONNECTED 16379 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 16160
unix 3 [ ] STREAM CONNECTED 13796 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15732 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 15611
unix 3 [ ] STREAM CONNECTED 13777
unix 2 [ ] DGRAM 20011
unix 3 [ ] STREAM CONNECTED 16106 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 19840
unix 2 [ ] DGRAM 16231
unix 3 [ ] STREAM CONNECTED 16105
unix 3 [ ] STREAM CONNECTED 16320
unix 3 [ ] STREAM CONNECTED 19885
unix 3 [ ] DGRAM 13816
unix 3 [ ] STREAM CONNECTED 18433 /run/systemd/journal/stdout
unix 3 [ ] STREAM CONNECTED 16321 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 19886
unix 3 [ ] STREAM CONNECTED 16161 /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 16378
unix 3 [ ] STREAM CONNECTED 19888
unix 3 [ ] STREAM CONNECTED 15612
unix 3 [ ] STREAM CONNECTED 18432
unix 3 [ ] STREAM CONNECTED 19889
unix 2 [ ] DGRAM 16204
unix 3 [ ] STREAM CONNECTED 15731
unix 3 [ ] STREAM CONNECTED 19834
unix 3 [ ] STREAM CONNECTED 19891
unix 3 [ ] STREAM CONNECTED 19841
unix 3 [ ] STREAM CONNECTED 19836
unix 2 [ ] DGRAM 79889
unix 3 [ ] STREAM CONNECTED 19892
unix 2 [ ] DGRAM 16133
unix 3 [ ] STREAM CONNECTED 15152
unix 3 [ ] STREAM CONNECTED 16128
unix 3 [ ] STREAM CONNECTED 19894
unix 3 [ ] STREAM CONNECTED 19895
unix 3 [ ] STREAM CONNECTED 15153
unix 3 [ ] STREAM CONNECTED 19897
unix 3 [ ] STREAM CONNECTED 19898
unix 3 [ ] DGRAM 13817
unix 2 [ ] DGRAM 13797
unix 2 [ ] DGRAM 15143
[yiibai@localhost ~]$
說明:
用於顯示關於以太網的統計數據。它列出的項目包括傳送的數據報的總字節數、錯誤數、刪除數、數據報的數量和廣播的數量。這些統計數據既有發送的數據報數量,也有接收的數據報數量。這個選項可以用來統計一些基本的網絡流量)
實例11:顯示關於路由表的信息
命令:
netstat -r
輸出:
[yiibai@localhost ~]$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 ens33
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
[yiibai@localhost ~]$
實例12:列出所有 tcp 端口
命令:
netstat -at
輸出:
[yiibai@localhost ~]$ netstat -at
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 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
[yiibai@localhost ~]$
實例13:統計機器中網絡連接各個狀態個數
命令:
netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
輸出:
[yiibai@localhost ~]$ netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
LISTEN 4
ESTABLISHED 1
[yiibai@localhost ~]$
實例14:把狀態全都取出來後使用uniq -c統計後再進行排序
命令:
netstat -nat |awk '{print $6}'|sort|uniq -c
輸出:
實例15:查看連接某服務端口最多的的IP地址
命令:
netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20
輸出:
[yiibai@localhost ~]$ netstat -nat |awk '{print $6}'|sort|uniq -c
1 established)
1 ESTABLISHED
1 Foreign
4 LISTEN
[yiibai@localhost ~]$
實例16:找出程序運行的端口
命令:
netstat -ap | grep ssh
輸出:
[yiibai@localhost ~]$ netstat -ap | grep ssh
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN -
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED -
tcp6 0 0 [::]:ssh [::]:* LISTEN -
[yiibai@localhost ~]$
實例17:在 netstat 輸出中顯示 PID 和進程名稱
命令:
netstat -pt
輸出:
[yiibai@localhost ~]$ netstat -pt
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 52 localhost.localdoma:ssh 192.168.0.5:54835 ESTABLISHED -
[yiibai@localhost ~]$
說明:netstat -p
可以與其它開關一起使用,就可以添加 「PID/進程名稱」 到 netstat
輸出中,這樣 debugging
的時候可以很方便的發現特定端口運行的程序。
實例18:找出運行在指定端口的進程
命令:
netstat -anpt | grep ':16160'
輸出:
[root@localhost yiibai]# netstat -anpt | grep ':16160'
[root@localhost yiibai]# netstat -anpt | grep ':22'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1032/sshd
tcp 0 52 192.168.0.197:22 192.168.0.5:54835 ESTABLISHED 4039/sshd: yiibai [
tcp6 0 0 :::22 :::* LISTEN 1032/sshd
[root@localhost yiibai]#
說明:
運行在端口22
的進程id
爲1032
,再通過ps
命令就可以找到具體的應用程序了。