Linux常用命令-netstat

命令

netstat

描述

Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
查看网络连接状态

用法

1
2
3
netstat [-vWeenNcCF] [<Af>] -r
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Options:
-r, --route 显示路由表
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces 显示网卡列表
-g, --groups display multicast group memberships
-s, --statistics 显示网络状态信息
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-W, --wide IP地址不换行显示
-n, --numeric 显示数字格式而非服务名称
--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 显示pid
-o, --timers display timers
-c, --continuous 不断的刷新显示,实时查看连接状态
-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} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
<AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$ netstat -V
net-tools 2.10-alpha
# -r 显示路由表
$ 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.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
# -i 显示网卡列表
$ netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 3072 0 0 0 1784 0 0 0 BMRU
lo 65536 0 0 0 0 0 0 0 0 LRU
# -s 显示网络连接状态
$ netstat -s
Ip:
3127 total packets received
0 forwarded
0 incoming packets discarded
3127 incoming packets delivered
1806 requests sent out
8 dropped because of missing route
Icmp:
0 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
0 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
Tcp:
0 active connections openings
1 passive connection openings
0 failed connection attempts
0 connection resets received
1 connections established
3055 segments received
1732 segments send out
0 segments retransmited
0 bad segments received.
0 resets sent
Udp:
59 packets received
0 packets to unknown port received.
0 packet receive errors
74 packets sent
0 receive buffer errors
0 send buffer errors
# 显示tcp和udp的连接
$ netstat -tu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 centos7:ssh 192.168.80.81:42886 ESTABLISHED
# 数字格式显示服务
$ netstat -tun
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.80.10:22 192.168.80.81:42886 ESTABLISHED
# 显示监听地址LISTEN
$ netstat -tunl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp6 0 0 ::1:323 :::*
# 显示所有连接
$ netstat -tuna
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
tcp 0 0 192.168.80.10:22 192.168.80.81:42886 ESTABLISHED
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp6 0 0 ::1:323 :::*
# 实时刷新显示,可以指定时间间隔,单位秒
$ netstat -tunac
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
tcp 0 0 192.168.80.10:22 192.168.80.81:42886 ESTABLISHED
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp6 0 0 ::1:323 :::*
# 每5秒刷新一次
$ netstat -tunac 5
...
# 显示IPv4连接
$ netstat -tuna4
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
tcp 0 0 192.168.80.10:22 192.168.80.81:42886 ESTABLISHED
udp 0 0 127.0.0.1:323 0.0.0.0:*
# 显示IPv6连接
$ netstat -tuna6
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp6 0 0 ::1:323 :::*