Linux之netstat命令 簡介Netstat 命令用於顯示各種網路相關資訊,如網路連接,路由表,介面狀態 (Interface Statistics),masquerade 串連,多播成員 (Multicast Memberships) 等等。輸出資訊含義執行netstat後,其輸出結果為[plain] [root@localhost ~]# netstat Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State getnameinfo failed getnameinfo failed tcp 0 132 [UNKNOWN]:ssh [UNKNOWN]:3101 ESTABLISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 9 [ ] DGRAM 7700 /dev/log unix 2 [ ] DGRAM 8754 @/var/run/hal/hotplug_socket unix 2 [ ] DGRAM 5079 @udevd unix 2 [ ] DGRAM 43227 unix 3 [ ] STREAM CONNECTED 12811 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 12810 unix 3 [ ] STREAM CONNECTED 8749 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 8748 unix 3 [ ] STREAM CONNECTED 8742 /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 8741 unix 3 [ ] STREAM CONNECTED 8618 unix 3 [ ] STREAM CONNECTED 8617 unix 2 [ ] DGRAM 8494 unix 2 [ ] DGRAM 8429 unix 2 [ ] DGRAM 8414 unix 2 [ ] DGRAM 8387 unix 2 [ ] DGRAM 8268 unix 3 [ ] STREAM CONNECTED 7890 unix 3 [ ] STREAM CONNECTED 7889 unix 2 [ ] DGRAM 7780 unix 2 [ ] DGRAM 7708 從整體上看,netstat的輸出結果可以分為兩個部分:一個是Active Internet connections,稱為有源TCP串連,其中"Recv-Q"和"Send-Q"指%0A的是接收隊列和發送隊列。這些數字一般都應該是0。如果不是則表示軟體包正在隊列中堆積。這種情況只能在非常少的情況見到。另一個是Active UNIX domain sockets,稱為有源Unix域套介面(和網路通訊端一樣,但是只能用於本機通訊,效能可以提高一倍)。Proto顯示串連使用的協議,RefCnt表示串連到本套介面上的進程號,Types顯示套介面的類型,State顯示套介面當前的狀態,Path表示串連到套介面的其它進程使用的路徑名。常見參數-a (all)顯示所有選項,預設不顯示LISTEN相關-t (tcp)僅顯示tcp相關選項-u (udp)僅顯示udp相關選項-n 拒絕顯示別名,能顯示數位全部轉化成數字。-l 僅列出有在 Listen (監聽) 的服務狀態-p 顯示建立相關連結的程式名-r 顯示路由資訊,路由表-e 顯示擴充資訊,例如uid等-s 按各個協議進行統計-c 每隔一個固定時間,執行該netstat命令。提示:LISTEN和LISTENING的狀態只有用-a或者-l才能看到 實用命令執行個體1. 列出所有連接埠 (包括監聽和未監聽的) 列出所有連接埠 netstat -a[plain] # netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket 列出所有 tcp 連接埠 netstat -at[plain] # netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN 列出所有 udp 連接埠 netstat -au [plain] <span style="font-size:12px;"># netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* </span> 2. 列出所有處於監聽狀態的 Sockets 只顯示監聽連接埠 netstat -l[plain] # netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN udp 0 0 *:49119 *:* 只列出所有監聽 tcp 連接埠 netstat -lt[plain] # netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN 只列出所有監聽 udp 連接埠 netstat -lu[plain] # netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* 3. 查看連接埠被哪個進程佔用列出連接埠進程佔用情況 netstat -pan |grep 8019[plain] <span style="font-size:12px;">[root@localhost ~]# netstat -pan |grep 8019 unix 2 [ ACC ] STREAM LISTENING 8019 4765/acpid /var/run/acpid.socket</span>