Linux上如何查看端口占用狀況

netstat -anp 顯示系統端口使用狀況node


直接使用 netstat   -anp   |   grep  portnonginx

即:netstat –apn | grep 80 便可查看80端口的使用狀況chrome


也可以使用netstat -tunlp |   grep  portno命令shell

-----------------------------------------------------------------------------------------------------------------------
apache

Netstat 簡介

Netstat 是一款命令行工具,可用於列出系統上全部的網絡套接字鏈接狀況,包括 tcp, udp 以及 unix 套接字,另外它還能列出處於監聽狀態(即等待接入請求)的套接字。若是你想確認系統上的 Web 服務有沒有起來,你能夠查看80端口有沒有打開。以上功能使 netstat 成爲網管和系統管理員的必備利器。在這篇教程中,我會列出幾個例子,教你們如何使用 netstat 去查找網絡鏈接信息和系統開啓的端口號。網絡

如下的簡單介紹來自 netstat 的 man 手冊:dom

netstat - 打印網絡鏈接、路由表、鏈接的數據統計、假裝鏈接以及廣播域成員。socket

1. 列出全部鏈接

第一個要介紹的,是最簡單的命令:列出全部當前的鏈接。使用 -a 選項便可。tcp

$ netstat -a
 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 enlightened:domain      *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp        0      0 enlightened.local:54750 li240-5.members.li:http ESTABLISHED
tcp        0      0 enlightened.local:49980 del01s07-in-f14.1:https ESTABLISHED
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
udp        0      0 enlightened:domain      *:*                                
udp        0      0 *:bootpc                *:*                                
udp        0      0 enlightened.local:ntp   *:*                                
udp        0      0 localhost:ntp           *:*                                
udp        0      0 *:ntp                   *:*                                
udp        0      0 *:58570                 *:*                                
udp        0      0 *:mdns                  *:*                                
udp        0      0 *:49459                 *:*                                
udp6       0      0 fe80::216:36ff:fef8:ntp [::]:*                             
udp6       0      0 ip6-localhost:ntp       [::]:*                             
udp6       0      0 [::]:ntp                [::]:*                             
udp6       0      0 [::]:mdns               [::]:*                             
udp6       0      0 [::]:63811              [::]:*                             
udp6       0      0 [::]:54952              [::]:*                             
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12403    @/tmp/dbus-IDgfj3UGXX
unix  2      [ ACC ]     STREAM     LISTENING     40202    @/dbus-vfs-daemon/socket-6nUC6CCx

上述命令列出 tcp, udp 和 unix 協議下全部套接字的全部鏈接。然而這些信息還不夠詳細,管理員每每須要查看某個協議或端口的具體鏈接狀況。ide

2. 只列出 TCP 或 UDP 協議的鏈接

使用 -t 選項列出 TCP 協議的鏈接:

$ netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 enlightened:domain      *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp        0      0 enlightened.local:36310 del01s07-in-f24.1:https ESTABLISHED
tcp        0      0 enlightened.local:45038 a96-17-181-10.depl:http ESTABLISHED
tcp        0      0 enlightened.local:37892 ABTS-North-Static-:http ESTABLISHED
.....

使用 -u 選項列出 UDP 協議的鏈接:

$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 *:34660                 *:*                                
udp        0      0 enlightened:domain      *:*                                
udp        0      0 *:bootpc                *:*                                
udp        0      0 enlightened.local:ntp   *:*                                
udp        0      0 localhost:ntp           *:*                                
udp        0      0 *:ntp                   *:*                                
udp6       0      0 fe80::216:36ff:fef8:ntp [::]:*                             
udp6       0      0 ip6-localhost:ntp       [::]:*                             
udp6       0      0 [::]:ntp                [::]:*

上面同時顯示了 IPv4 和 IPv6 的鏈接。

3. 禁用反向域名解析,加快查詢速度

默認狀況下 netstat 會經過反向域名解析技術查找每一個 IP 地址對應的主機名。這會下降查找速度。若是你以爲 IP 地址已經足夠,而沒有必要知道主機名,就使用 -n 選項禁用域名解析功能。

$ netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 192.168.1.2:49058       173.255.230.5:80        ESTABLISHED
tcp        0      0 192.168.1.2:33324       173.194.36.117:443      ESTABLISHED
tcp6       0      0 ::1:631                 :::*                    LISTEN

上述命令列出全部 TCP 協議的鏈接,沒有使用域名解析技術。So easy ? 很是好。

4. 只列出監聽中的鏈接

任何網絡服務的後臺進程都會打開一個端口,用於監聽接入的請求。這些正在監聽的套接字也和鏈接的套接字同樣,也能被 netstat 列出來。使用 -l 選項列出正在監聽的套接字。

$ netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN

如今咱們能夠看處處於監聽狀態的 TCP 端口和鏈接。若是你查看全部監聽端口,去掉 -t 選項。若是你只想查看 UDP 端口,使用-u 選項,代替 -t 選項。

注意:不要使用 -a 選項,不然 netstat 會列出全部鏈接,而不單單是監聽端口。

5. 獲取進程名、進程號以及用戶 ID

查看端口和鏈接的信息時,能查看到它們對應的進程名和進程號對系統管理員來講是很是有幫助的。舉個栗子,Apache 的 httpd 服務開啓80端口,若是你要查看 http 服務是否已經啓動,或者 http 服務是由 apache 仍是 nginx 啓動的,這時候你能夠看看進程名。

使用 -p 選項查看進程信息。

~$ sudo netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1144/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      661/cupsd       
tcp6       0      0 ::1:631                 :::*                    LISTEN      661/cupsd

使用 -p 選項時,netstat 必須運行在 root 權限之下,否則它就不能獲得運行在 root 權限下的進程名,而不少服務包括 http 和 ftp 都運行在 root 權限之下。

相比進程名和進程號而言,查看進程的擁有者會更有用。使用 -ep 選項能夠同時查看進程名和用戶名。

$ sudo netstat -ltpe
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp        0      0 enlightened:domain      *:*                     LISTEN      root       11090       1144/dnsmasq    
tcp        0      0 localhost:ipp           *:*                     LISTEN      root       9755        661/cupsd       
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN      root       9754        661/cupsd

上面列出 TCP 協議下的監聽套接字,同時顯示進程信息和一些額外信息。

這些額外的信息包括用戶名和進程的索引節點號。這個命令對網管來講頗有用。

注意 - 假如你將 -n 和 -e 選項一塊兒使用,User 列的屬性就是用戶的 ID 號,而不是用戶名。

6. 打印統計數據

netstat 能夠打印出網絡統計數據,包括某個協議下的收發包數量。

下面列出全部網絡包的統計狀況:

$ netstat -s
Ip:
    32797 total packets received
    0 forwarded
    0 incoming packets discarded
    32795 incoming packets delivered
    29115 requests sent out
    60 outgoing packets dropped
Icmp:
    125 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 125
    125 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 125
... OUTPUT TRUNCATED ...

若是想只打印出 TCP 或 UDP 協議的統計數據,只要加上對應的選項(-t 和 -u)便可,so easy。

7. 顯示內核路由信息

使用 -r 選項打印內核路由信息。打印出來的信息與 route 命令輸出的信息同樣。咱們也可使用 -n 選項禁止域名解析。

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

8. 打印網絡接口

netstat 也能打印網絡接口信息,-i 選項就是爲這個功能而生。

$ netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0     31611      0      0 0         27503      0      0      0 BMRU
lo        65536 0      2913      0      0 0          2913      0      0      0 LRU

上面輸出的信息比較原始。咱們將 -e 選項和 -i 選項搭配使用,能夠輸出用戶友好的信息。

$ netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:16:36:f8:b2:64  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29637117 (29.6 MB)  TX bytes:4590583 (4.5 MB)
          Interrupt:18 Memory:da000000-da020000 
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2921 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2921 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:305297 (305.2 KB)  TX bytes:305297 (305.2 KB)

上面的輸出信息與 ifconfig 輸出的信息同樣。

9. netstat 持續輸出

咱們可使用 netstat 的 -c 選項持續輸出信息。

$ netstat -ct

這個命令可持續輸出 TCP 協議信息。

10. 顯示多播組信息

選項 -g 會輸出 IPv4 和 IPv6 的多播組信息。

$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
eth0            1      224.0.0.251
eth0            1      all-systems.mcast.net
lo              1      ip6-allnodes
lo              1      ff01::1
eth0            1      ff02::fb
eth0            1      ff02::1:fff8:b264
eth0            1      ip6-allnodes
eth0            1      ff01::1
wlan0           1      ip6-allnodes
wlan0           1      ff01::1

更多用法

目前爲止咱們列出了 netstat 的基本用法,如今讓咱們一塊兒來 geek 吧~

打印 active 狀態的鏈接

active 狀態的套接字鏈接用 "ESTABLISHED" 字段表示,因此咱們可使用 grep 命令得到 active 狀態的鏈接:

$ netstat -atnp | grep ESTA
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 192.168.1.2:49156       173.255.230.5:80        ESTABLISHED 1691/chrome     
tcp        0      0 192.168.1.2:33324       173.194.36.117:443      ESTABLISHED 1691/chrome

配合 watch 命令監視 active 狀態的鏈接:

$ watch -d -n0 "netstat -atnp | grep ESTA"

查看服務是否在運行

若是你想看看 http,smtp 或 ntp 服務是否在運行,使用 grep。

$ sudo netstat -aple | grep ntp
udp        0      0 enlightened.local:ntp   *:*                                 root       17430       1789/ntpd       
udp        0      0 localhost:ntp           *:*                                 root       17429       1789/ntpd       
udp        0      0 *:ntp                   *:*                                 root       17422       1789/ntpd       
udp6       0      0 fe80::216:36ff:fef8:ntp [::]:*                              root       17432       1789/ntpd       
udp6       0      0 ip6-localhost:ntp       [::]:*                              root       17431       1789/ntpd       
udp6       0      0 [::]:ntp                [::]:*                              root       17423       1789/ntpd       
unix  2      [ ]         DGRAM                    17418    1789/ntpd

從這裏能夠看到 ntp 服務正在運行。使用 grep 命令你能夠查看 http 或 smtp 或其它任何你想查看的服務。

好了,netstat 的大部分功能都介紹過了,若是你想知道 netstat 更高級的功能,閱讀它的手冊吧(man netstat)。

歡迎在下面留下你的反饋和建議。

相關文章
相關標籤/搜索