运行环境:Ubuntu 16.04.6 LTS
bingsanlang@ubuntu:~$ netstat -h
usage: netstat [-vWeenNcCF] [] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [ ...]
netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }
-r, --route display routing table //显示Routing Table。
-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 //直接使用IP地址,而不通过域名服务器。
--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//显示正在使用Socket的程序识别码和程序名称。
-c, --continuous continuous listing //持续列出网络状态。
-l, --listening display listening server sockets//仅列出有在 Listen (监听) 的服務状态
-a, --all, --listening display all sockets (default: connected) //显示所有连线中的Socket。
-o, --timers display timers//显示计时器.
-F, --fib display Forwarding Information Base (default)//显示FIB.
-C, --cache display routing cache instead of FIB//显示路由器配置的快取信息。
={-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
=Use '-6|-4' or '-A ' or '--'; 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)
bingsanlang@ubuntu:~$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ubuntu:57824 server-54-192-151:https ESTABLISHED
.....省略很多
Active Internet connections:有源TCP连接,从当前的输出有6列,分别是:
列 | Proto | Recv-Q | Send-Q | Local Address | Foreign Address | State |
---|---|---|---|---|---|---|
列名称 | 连接协议 | 接收队列 | 发送队列 | 本地地址:端口 | 外部地址:端口 | 连接状态 |
连接状态有:
Active UNIX domain sockets:有源Unix域套接口,和网络套接字一样,只能用于本机通信,性能可以提高一倍。
.....省略很多
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 22274 /run/user/1000/systemd/notify
.....省略很多
列 | Proto | RefCnt | Flags | Type | State | I-Node | Path |
---|---|---|---|---|---|---|---|
列名称 | 连接协议 | 连接到本套接口上的进程号 | 套接口类型 | 套接口状态 | 打开套接字的进程ID(PID)和进程名 | 连接到套接字上的相应进程的路径名 |
Flags所显示的标志有:
套接口类型Type的类型有:
State的状态有:
bingsanlang@ubuntu:~$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default bogon 0.0.0.0 UG 0 0 0 enp0s5
10.211.55.0 * 255.255.255.0 U 0 0 0 enp0s5
link-local * 255.255.0.0 U 0 0 0 enp0s5
各个列的解释如下:
其中Flags详细解释有:
其中G和H比较重要:G区分了直接路由和间接路由, 对于直接路由, 发往直接路由的分组不仅有目的端的IP, 还有目的端的链路层地址. 但对于间接路由, 当分组被发送到间接路由时, IP地址指明的是最终目的地, 但链路层地址指明的是网关(下一站路由).
H说明了目的地址是一个完整的主机地址, 没有设置H则说明目的地址是一个网络地址(主机号为0), 当搜索路由表时, 主机地址必须与目的地址完全匹配, 而网络地址只需要匹配网络号和子网号.
其中MSS的详细解释:
MSS:Maximum Segment Size ,TCP允许提交给IP层的数据部分大小(TCP限制应用层最大的发送字节数),MSS值一般是MTU-ip头-tcp头,对于ipv4来说,ip头占20,ipv6占40。tcp头始终占20。默认MTU(Maximum Transmit Unit,最大传输单元)为1500的情况下,一般MSS都会是1460,假设应用层有2000 byte发送,需要两个segment才可以完成发送,第一个TCP segment = 1460,第二个TCP segment = 540。
//显示网络接口表单信息。
bingsanlang@ubuntu:~$ 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
enp0s5 1500 0 3938993 0 0 0 3189429 0 0 0 BMRU
lo 65536 0 1658137 0 0 0 1658137 0 0 0 LRU
各个列的解释如下:
display multicast group memberships//显示多重广播功能群组组员名单
bingsanlang@ubuntu:~$ netstat -g
IPv6/IPv4 Group Memberships
Interface RefCnt Group
--------------- ------ ---------------------
lo 1 all-systems.mcast.net
enp0s5 1 224.0.0.251
enp0s5 1 all-systems.mcast.net
lo 1 ip6-allnodes
lo 1 ff01::1
enp0s5 1 ff02::fb
enp0s5 1 ff02::1:ff3e:e5e1
enp0s5 1 ff02::1:ff23:82af
enp0s5 1 ff02::1:ff34:2193
enp0s5 1 ip6-allnodes
enp0s5 1 ff01::1
各个列的解释如下:
-s, --statistics display networking statistics (like SNMP)//显示网络工作信息统计表。
bingsanlang@ubuntu:~$ netstat -s
Ip:
5530713 total packets received
...省略...
Icmp:
3482 ICMP messages received
...省略...
IcmpMsg:
InType0: 9
...省略...
Tcp:
24831 active connections openings
...省略...
Udp:
447862 packets received
...省略...
UdpLite:
TcpExt:
1 ICMP packets dropped because they were out-of-window
...省略...
IpExt:
InMcastPkts: 66300
...省略...
-M, --masquerade display masqueraded connections//显示伪装的网络连线。
bingsanlang@ubuntu:~$ netstat -M
netstat: no support for `ip_masquerade' on this system.
-v, --verbose be verbose //显示指令执行过程。
告诉用户存在的冗长信息。特别是打印一些关于未配置的地址家庭有用的信息。
bingsanlang@ubuntu:~$ netstat -v
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ubuntu:50230 a2-18-212-136.depl:http ESTABLISHED
...省略...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 22274 /run/user/1000/systemd/notify
...省略...
netstat: no support for `AF IPX' on this system.
netstat: no support for `AF AX25' on this system.
netstat: no support for `AF X25' on this system.
netstat: no support for `AF NETROM' on this system.
比如上面最后四行输出的结果是未配置,但是这个是什么意思我还不知道,需要后期搞清楚。
-W, --wide don’t truncate IP addresses// 不用截取ip地址
Foreign Address显示的是全限定地址
bingsanlang@ubuntu:~$ netstat -W
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ubuntu:47622 a104-124-11-34.deploy.static.akamaitechnologies.com:http
ESTABLISHED
...省略...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 22274 /run/user/1000/systemd/notify
...省略...
-a, --all, --listening display all sockets (default: connected) //显示所有连线中的Socket。
bingsanlang@ubuntu:~$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9870 *:* LISTEN
tcp 0 0 localhost:8563 *:* LISTEN
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
...省略...
udp 0 0 *:47468 *:*
udp 0 0 *:ipp *:*
udp 0 0 ubuntu:domain *:*
raw6 0 0 [::]:ipv6-icmp [::]:* 7
...省略...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 22274 /run/user/1000/systemd/notify
...省略...
-n, --numeric don’t resolve names //直接使用IP地址,而不通过域名服务器。
比如 netstat -r 加上-n后将不再使用域名,直接使用ip
,以下是命令对比:
wanghanbing@ubuntu:~$ netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default bogon 0.0.0.0 UG 0 0 0 enp0s5
10.211.55.0 * 255.255.255.0 U 0 0 0 enp0s5
link-local * 255.255.0.0 U 0 0 0 enp0s5
wanghanbing@ubuntu:~$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.211.55.1 0.0.0.0 UG 0 0 0 enp0s5
10.211.55.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s5
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s5
wanghanbing@ubuntu:~$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
udp 0 0 0.0.0.0:5353 0.0.0.0:*
raw6 0 0 :::58 :::*
...省略...
如上所示,所有的域名、端口都数字化。
–numeric-hosts don’t resolve host names//无需解析主机名称
-n是将主机,端口和用户名进行数字化显示,如果只想将一部分显示信息进行数字话,可以选取部分参数数字化,比如将主机名称数字化:–numeric-hosts。
bingsanlang@ubuntu:~$ netstat --numeric-hosts
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 10.211.55.4:44734 54.149.210.11:https ESTABLISHED
tcp 0 0 10.211.55.4:ssh 10.211.55.2:55512 ESTABLISHED
...省略...
–numeric-ports don’t resolve port names//无需解析端口名称
实际的执行结果是将主机名和端口号数字化了,这个不知道为什么,留个//TODO
bingsanlang@ubuntu:~$ netstat --numeric-hosts
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:9870 0.0.0.0:* LISTEN
...省略...
–numeric-users don’t resolve user names//无需解析用户名称
bingsanlang@ubuntu:~$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 0 ubuntu:44734 ec2-54-149-210-11:https ESTABLISHED wanghanbing 18078578
tcp 0 188 ubuntu:ssh bogon:55512 ESTABLISHED root 16884441
A
bingsanlang@ubuntu:~$ netstat -e --numeric-users
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 0 10.211.55.4:44734 54.149.210.11:https ESTABLISHED 1000 18078578
tcp 0 0 10.211.55.4:ssh 10.211.55.2:55512 ESTABLISHED 0 16884441
...省略...
-N, --symbolic resolve hardware names//显示网络硬件外围设备的符号连接名称。
这个用例暂时没找到
-e, --extend display other/more information//显示网络其他相关信息。
多显示了User和Inode信息。
bingsanlang@ubuntu:~$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode
tcp 0 0 ubuntu:44734 ec2-54-149-210-11:https ESTABLISHED wanghanbing 18078578
tcp 0 188 ubuntu:ssh bogon:55512 ESTABLISHED root 16884441
A
...省略...
-p, --programs display PID/Program name for sockets//显示正在使用Socket的程序识别码和程序名称。
最后一列多显示了PID/Program name。
bingsanlang@ubuntu:~$ netstat -p
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 ubuntu:44734 ec2-54-149-210-11:https ESTABLISHED 19132/firefox
tcp 0 0 ubuntu:ssh bogon:55512 ESTABLISHED -
...省略...
-c, --continuous continuous listing //持续列出网络状态。
使用这个参数后会持续输出命令结果。
wanghanbing@ubuntu:~$ netstat -rc
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default bogon 0.0.0.0 UG 0 0 0 enp0s5
10.211.55.0 * 255.255.255.0 U 0 0 0 enp0s5
link-local * 255.255.0.0 U 0 0 0 enp0s5
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default bogon 0.0.0.0 UG 0 0 0 enp0s5
10.211.55.0 * 255.255.255.0 U 0 0 0 enp0s5
link-local * 255.255.0.0 U 0 0 0 enp0s5
...省略...
^C
-l, --listening display listening server sockets//仅列出有在 Listen (监听) 的服務状态
Active UNIX domain sockets 的State是LISTENING。
bingsanlang@ubuntu:~$ netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9870 *:* LISTEN
...省略...
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 22275 /run/user/1000/systemd/private
unix 2 [ ACC ] STREAM LISTENING 22289 /run/user/1000/keyring/control
...省略...
-o, --timers display timers//显示计时器.
最后一列多显示了Timer。
wanghanbing@ubuntu:~$ netstat -o
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State Timer
tcp 0 0 ubuntu:44734 ec2-54-149-210-11:https ESTABLISHED keepalive (365.24/0/0)
tcp 0 84 ubuntu:ssh bogon:55512 ESTABLISHED keepalive (6208.08/0/0)
tcp 0 0 ubuntu:46242 203.208.50.38:https TIME_WAIT timewait (41.40/0/0)
...省略...
-F, --fib display Forwarding Information Base (default)//显示FIB.
这个用例暂时没找到
-C, --cache display routing cache instead of FIB//显示路由器配置的缓存信息。
这个用例暂时没找到
={-t|–tcp} {-u|–udp} {-w|–raw} {-x|–unix} --ax25 --ipx --netrom
=Use ‘-6|-4’ or '-A ’ or ‘–’; 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)
bingsanlang@ubuntu:~$ netstat -a -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:9870 *:* LISTEN
tcp 0 0 ubuntu:ssh bogon:55512 ESTABLISHED
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN
...省略...
bingsanlang@ubuntu:~$ netstat -a -u
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:mdns *:*
udp6 0 0 [::]:50810 [::]:*
...省略...
参考:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
https://man.linuxde.net/netstat
https://www.cnblogs.com/wyk930511/p/7221413.html
https://www.jianshu.com/p/3be7582ddf78
https://blog.csdn.net/freeking101/article/details/53520974
https://www.jb51.net/article/152228.htm
https://blog.csdn.net/ma2595162349/article/details/88540456
https://blog.csdn.net/woshizhangliang999/article/details/50733038
https://www.cnblogs.com/ftl1012/p/netstat.html