Netstat 的全寫是 network statistics, 是分析網路流量的指令, 例如可以檢視開啟了的埠號或遠端連線的 IP 及 Routing table 等。
本文是關於在 Linux 下安裝 netstat, 及 netstat 的基本應用。
現在多數的 Linux 發行版預設沒有安裝 netstat, 執行 netstat 會出現以下錯誤:
netstat: command not found
netstat 包含在 net-tools 套件, 執行以下指令安裝 net-tools:
RHEL / CentOS
# yum install net-tools
Debian / Ubuntu
# apt install net-tools
Fedora
# dnf install net-tools
安裝後便可以使用 netstat 指令。以下是 netstat 一些使用例子:
列出開啟的網路服務:
# netstat -pnltu
輸出類似以下的結果:
|
1 2 3 4 5 6 7 8 |
# netstat -pnltu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1055/sshd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26827/nginx: master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 26827/nginx: master tcp6 0 0 :::22 :::* LISTEN 1055/sshd tcp6 0 0 :::80 :::* LISTEN 26827/nginx: master - |
列出網路連線:
列出所有 RAW, UDP, TCP, 及 UNIX socket 連線
# netstat -ant
檢視網路 Routing table:
# netstat -nr
列出網卡使用統計資訊:
# netstat -ai