引言
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"參數相同。
執行個體
- 列出所有 TCP 監聽伺服器端口
[root@hdp-01 ~]# netstat -nltp
- 列出所有端口情況
[root@xiesshavip002 ~]# netstat -a # 列出所有端口
[root@xiesshavip002 ~]# netstat -at # 列出所有TCP端口
[root@xiesshavip002 ~]# netstat -au # 列出所有UDP端口
- 列出所有處于監聽狀态的 Sockets
[root@xiesshavip002 ~]# netstat -l # 隻顯示監聽端口
[root@xiesshavip002 ~]# netstat -lt # 顯示監聽TCP端口
[root@xiesshavip002 ~]# netstat -lu # 顯示監聽UDP端口
[root@xiesshavip002 ~]# netstat -lx # 顯示監聽UNIX端口
- 顯示每個協定的統計資訊
[root@xiesshavip002 ~]# netstat -s # 顯示所有端口的統計資訊
[root@xiesshavip002 ~]# netstat -st # 顯示所有TCP的統計資訊
[root@xiesshavip002 ~]# netstat -su # 顯示所有UDP的統計資訊
- 顯示 PID 和程序名稱
[root@xiesshavip002 ~]# netstat -p
- 顯示核心路由資訊
[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 ~]#
- 檢視端口和服務
[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 指令幫助
[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)
檢視程序占用的端口号
套接字是和 IP 位址、軟體端口和協定結合起來使用的,而端口号對傳輸控制協定(TCP)和使用者資料報協定(UDP)協定都适用,TCP 和 UDP 都可以使用 0 到 65535 之間的端口号進行通信。
以下是端口配置設定類别:
- 0 - 1023: 常用端口和系統端口
- 1024 - 49151: 軟體的注冊端口
- 49152 - 65535: 動态端口或私有端口
在 Linux 上的
/etc/services
檔案可以檢視到更多關于保留端口的資訊。
可以使用以下六種方法檢視端口資訊。
-
:可以用于轉儲套接字統計資訊。ss
-
:可以顯示打開的套接字清單。netstat
-
:可以列出打開的檔案。lsof
-
:可以列出那些打開了檔案的程序的程序 ID。fuser
-
:是網絡檢測工具和端口掃描程式。nmap
-
:是 systemd 系統的控制管理器和服務管理器。systemctl
舉例
以下将找出
sshd
守護程序所使用的端口号
- 方法 1:使用 ss 指令
一般用于轉儲套接字統計資訊。它能夠輸出類似于
netstat
輸出的資訊,但它可以比其它工具顯示更多的 TCP 資訊和狀态資訊。
它還可以顯示所有類型的套接字統計資訊,包括 PACKET、TCP、UDP、DCCP、RAW、Unix 域等。
# ss -tnlp | grep ssh
LISTEN 0 128 *:22 *:* users:(("sshd",pid=997,fd=3))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=997,fd=4))
也可以使用端口号來檢查
# ss -tnlp | grep ":22"
LISTEN 0 128 *:22 *:* users:(("sshd",pid=997,fd=3))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=997,fd=4))
- 方法 2:使用 netstat 指令
netstat
能夠顯示網絡連接配接、路由表、接口統計資訊、僞裝連接配接以及多點傳播成員。
預設情況下,
netstat
會列出打開的套接字。如果不指定任何位址族,則會顯示所有已配置位址族的活動套接字。但
netstat
已經過時了,一般會使用
ss
來替代。
# netstat -tnlp | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 997/sshd
tcp6 0 0 :::22 :::* LISTEN 997/sshd
# netstat -tnlp | grep ":22"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1208/sshd
tcp6 0 0 :::22 :::* LISTEN 1208/sshd
- 方法 3:使用 lsof 指令
lsof
能夠列出打開的檔案,并列出系統上被程序打開的檔案的相關資訊。
# lsof -i -P | grep ssh
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 11584 root 3u IPv4 27625 0t0 TCP *:22 (LISTEN)
sshd 11584 root 4u IPv6 27627 0t0 TCP *:22 (LISTEN)
sshd 11592 root 3u IPv4 27744 0t0 TCP vps.2daygeek.com:ssh->103.5.134.167:49902 (ESTABLISHED)
# lsof -i tcp:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1208 root 3u IPv4 20919 0t0 TCP *:ssh (LISTEN)
sshd 1208 root 4u IPv6 20921 0t0 TCP *:ssh (LISTEN)
sshd 11592 root 3u IPv4 27744 0t0 TCP vps.2daygeek.com:ssh->103.5.134.167:49902 (ESTABLISHED)
- 方法 4:使用 fuser 指令
fuser
工具會将本地系統上打開了檔案的程序的程序 ID 顯示在标準輸出中。
# fuser -v 22/tcp
USER PID ACCESS COMMAND
22/tcp: root 1208 F.... sshd
root 12388 F.... sshd
root 49339 F.... sshd
- 方法 5:使用 nmap 指令
# nmap -sV -p 22 localhost
- 方法 6:使用 systemctl 指令