Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。本文提供 10 个非常实用的 Netstat 命令实例。
1. 列出所有端口 (包括监听和未监听的)
列出所有端口 netstat -a
netstat -a | more
[root@diancai1 ~]# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:5666 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 diancai1.com:47537 115.239.224.60:60000 ESTABLISHED
tcp 0 0 *:6080 *:* LISTEN
tcp 0 0 localhost.localdomain:8035 *:* LISTEN
tcp 0 0 *:8039 *:* LISTEN
tcp 0 0 *:7080 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 localhost.localdomain:8015 *:* LISTEN
tcp 0 0 *:webcache *:* LISTEN
tcp 0 0 *:personal-agent *:* LISTEN
tcp 0 0 *:8019 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:glrpc *:* LISTEN
tcp 0 0 localhost.localdomain:8025 *:* LISTEN
tcp 0 0 *:8029 *:* LISTEN
tcp 0 0 *:esri_sde *:* LISTEN
tcp 0 48 diancai1.com:ssh localhost:57648 ESTABLISHED
tcp 0 0 diancai1.com:55624 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53528 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53529 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53484 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53483 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53482 localhost:mysql ESTABLISHED
列出所有 tcp 端口 netstat -at
[root@diancai1 ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:5666 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 diancai1.com:47537 115.239.224.60:60000 ESTABLISHED
tcp 0 0 *:6080 *:* LISTEN
tcp 0 0 localhost.localdomain:8035 *:* LISTEN
tcp 0 0 *:8039 *:* LISTEN
tcp 0 0 *:7080 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 localhost.localdomain:8015 *:* LISTEN
tcp 0 0 *:webcache *:* LISTEN
tcp 0 0 *:personal-agent *:* LISTEN
tcp 0 0 *:8019 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:glrpc *:* LISTEN
tcp 0 0 localhost.localdomain:8025 *:* LISTEN
tcp 0 0 *:8029 *:* LISTEN
tcp 0 0 *:esri_sde *:* LISTEN
tcp 0 512 diancai1.com:ssh localhost:57648 ESTABLISHED
tcp 0 0 diancai1.com:6080 localhost:14048 TIME_WAIT
tcp 0 0 diancai1.com:55624 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53528 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53529 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:53484 localhost:mysql ESTABLISHED
列出所有 udp 端口 netstat -au
[root@diancai1 ~]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:bootpc *:*
2.列出所有处于监听状态的 Sockets
只显示监听端口 netstat -l
[root@diancai1 ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:5666 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 *:6080 *:* LISTEN
tcp 0 0 localhost.localdomain:8035 *:* LISTEN
tcp 0 0 *:8039 *:* LISTEN
tcp 0 0 *:7080 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 localhost.localdomain:8015 *:* LISTEN
tcp 0 0 *:webcache *:* LISTEN
tcp 0 0 *:personal-agent *:* LISTEN
tcp 0 0 *:8019 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:glrpc *:* LISTEN
tcp 0 0 localhost.localdomain:8025 *:* LISTEN
tcp 0 0 *:8029 *:* LISTEN
tcp 0 0 *:esri_sde *:* LISTEN
udp 0 0 *:bootpc *:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 8560 /tmp/.gdm_socket
unix 2 [ ACC ] STREAM LISTENING 8589 /tmp/.X11-unix/X0
只列出所有监听 tcp 端口 netstat -lt
[root@diancai1 ~]# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:5666 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 *:6080 *:* LISTEN
tcp 0 0 localhost.localdomain:8035 *:* LISTEN
tcp 0 0 *:8039 *:* LISTEN
tcp 0 0 *:7080 *:* LISTEN
tcp 0 0 *:rsync *:* LISTEN
tcp 0 0 localhost.localdomain:8015 *:* LISTEN
tcp 0 0 *:webcache *:* LISTEN
tcp 0 0 *:personal-agent *:* LISTEN
tcp 0 0 *:8019 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:glrpc *:* LISTEN
tcp 0 0 localhost.localdomain:8025 *:* LISTEN
tcp 0 0 *:8029 *:* LISTEN
tcp 0 0 *:esri_sde *:* LISTEN
只列出所有监听 udp 端口 netstat -lu
[root@diancai1 ~]# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 *:bootpc *:*
只列出所有监听 UNIX 端口 netstat -lx
[root@diancai1 ~]# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 8560 /tmp/.gdm_socket
unix 2 [ ACC ] STREAM LISTENING 8589 /tmp/.X11-unix/X0
3.显示每个协议的统计信息
显示所有端口的统计信息 netstat -s
[root@diancai1 ~]# netstat -s
Ip:
1037044938 total packets received
121 with invalid addresses
0 forwarded
0 incoming packets discarded
1036911071 incoming packets delivered
2034453624 requests sent out
35 fragments failed
Icmp:
12687916 ICMP messages received
8420682 input ICMP message failed.
ICMP input histogram:
destination unreachable: 3522
timeout in transit: 9777
echo requests: 12674586
echo replies: 31
4254071 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 52
echo request: 113
echo replies: 4253904
IcmpMsg:
InType0: 31
InType3: 3522
InType8: 12674586
InType11: 9777
OutType0: 4253904
OutType1: 2
OutType3: 52
OutType8: 113
Tcp:
41289248 active connections openings
40566438 passive connection openings
1428278 failed connection attempts
180025 connection resets received
46 connections established
1023982902 segments received
907824976 segments send out
503660 segments retransmited
1 bad segments received.
4290136 resets sent
Udp:
509280 packets received
24 packets to unknown port received.
0 packet receive errors
509934 packets sent
TcpExt:
365906 invalid SYN cookies received
271 resets received for embryonic SYN_RECV sockets
55 packets pruned from receive queue because of socket buffer overrun
6316 ICMP packets dropped because they were out-of-window
26 ICMP packets dropped because socket was locked
39728257 TCP sockets finished time wait in fast timer
105112 time wait sockets recycled by time stamp
1 TCP sockets finished time wait in slow timer
45200334 delayed acks sent
192823 delayed acks further delayed because of locked socket
Quick ack mode was activated 32969 times
1161215 times the listen queue of a socket overflowed
1161215 SYNs to LISTEN sockets ignored
454089419 packets directly queued to recvmsg prequeue.
2713155 packets directly received from backlog
310674262 packets directly received from prequeue
615928746 packets header predicted
235531 packets header predicted and directly queued to user
87820741 acknowledgments not containing data received
312798179 predicted acknowledgments
15896 times recovered from packet loss due to SACK data
5 bad SACKs received
Detected reordering 1 times using FACK
Detected reordering 1 times using SACK
Detected reordering 11 times using time stamp
15 congestion windows fully recovered
29 congestion windows partially recovered using Hoe heuristic
TCPDSACKUndo: 5227
7216 congestion windows recovered after partial ack
21724 TCP data loss events
1 timeouts after reno fast retransmit
2728 timeouts after SACK recovery
642 timeouts in loss state
40489 fast retransmits
313 forward retransmits
244256 retransmits in slow start
111404 other TCP timeouts
2596 sack retransmits failed
13159 packets collapsed in receive queue due to low socket buffer
55147 DSACKs sent for old packets
101 DSACKs sent for out of order packets
136949 DSACKs received
8 DSACKs for out of order packets received
2307330 connections reset due to unexpected data
5238 connections reset due to early user close
25640 connections aborted due to timeout
IpExt:
InMcastPkts: 8420682
InBcastPkts: 149203
显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
[root@diancai1 ~]# netstat -st
IcmpMsg:
InType0: 31
InType3: 3522
InType8: 12674598
InType11: 9777
OutType0: 4253906
OutType1: 2
OutType3: 52
OutType8: 113
Tcp:
41289265 active connections openings
40566490 passive connection openings
1428280 failed connection attempts
180025 connection resets received
46 connections established
1023984103 segments received
907825655 segments send out
503664 segments retransmited
1 bad segments received.
4290138 resets sent
TcpExt:
365906 invalid SYN cookies received
271 resets received for embryonic SYN_RECV sockets
55 packets pruned from receive queue because of socket buffer overrun
6316 ICMP packets dropped because they were out-of-window
26 ICMP packets dropped because socket was locked
39728270 TCP sockets finished time wait in fast timer
105112 time wait sockets recycled by time stamp
1 TCP sockets finished time wait in slow timer
45200362 delayed acks sent
192824 delayed acks further delayed because of locked socket
Quick ack mode was activated 32970 times
1161215 times the listen queue of a socket overflowed
1161215 SYNs to LISTEN sockets ignored
454089687 packets directly queued to recvmsg prequeue.
2713159 packets directly received from backlog
310674514 packets directly received from prequeue
615929541 packets header predicted
235561 packets header predicted and directly queued to user
87820876 acknowledgments not containing data received
312798309 predicted acknowledgments
15896 times recovered from packet loss due to SACK data
5 bad SACKs received
Detected reordering 1 times using FACK
Detected reordering 1 times using SACK
Detected reordering 11 times using time stamp
15 congestion windows fully recovered
29 congestion windows partially recovered using Hoe heuristic
TCPDSACKUndo: 5227
7216 congestion windows recovered after partial ack
21724 TCP data loss events
1 timeouts after reno fast retransmit
2728 timeouts after SACK recovery
642 timeouts in loss state
40489 fast retransmits
313 forward retransmits
244256 retransmits in slow start
111406 other TCP timeouts
2596 sack retransmits failed
13159 packets collapsed in receive queue due to low socket buffer
55148 DSACKs sent for old packets
101 DSACKs sent for out of order packets
136949 DSACKs received
8 DSACKs for out of order packets received
2307330 connections reset due to unexpected data
5238 connections reset due to early user close
25640 connections aborted due to timeout
IpExt:
InMcastPkts: 8420692
InBcastPkts: 149203
4.在 netstat 输出中显示 PID 和进程名称 netstat -p
netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
[root@diancai1 ~]# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 diancai1.com:glrpc localhost:47786 SYN_RECV -
tcp 0 0 diancai1.com:47537 115.239.224.60:60000 ESTABLISHED 27294/dns
tcp 0 288 diancai1.com:ssh localhost:57648 ESTABLISHED 21360/1
tcp 0 0 diancai1.com:webcache localhost:57867 ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55358 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55357 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55355 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55354 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55353 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55350 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55349 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55347 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55346 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55345 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55344 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55342 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55341 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55340 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55338 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55337 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55336 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55333 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55332 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55330 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55329 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55328 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55323 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55322 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55321 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55318 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55317 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55315 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55313 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55312 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55311 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55306 localhost:mysql TIME_WAIT -
tcp 0 0 diancai1.com:55361 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:55360 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:53528 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55289 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55290 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55254 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55255 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55256 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55258 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55259 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:62676 localhost:11211 ESTABLISHED 13212/java
tcp 0 0 diancai1.com:62688 localhost:11211 ESTABLISHED 13212/java
tcp 0 0 diancai1.com:42154 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:45857 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:40218 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:40217 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:40177 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:40176 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:40175 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:27391 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:apm-link localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:32482 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:esri_sde localhost:34142 ESTABLISHED 13574/java
tcp 0 0 diancai1.com:32055 localhost:11211 ESTABLISHED 24356/java
tcp 0 0 diancai1.com:jerand-lm localhost:mysql ESTABLISHED 13574/java
5. 在 netstat 输出中不显示主机,端口和用户名 (host, port or user)
当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。同样可以加速输出,因为不用进行比对查询。
[root@diancai1 ~]# 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:5666 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
tcp 0 0 172.16.2.2:9080 172.16.1.12:48674 SYN_RECV
tcp 0 0 172.16.2.2:47537 115.239.224.60:60000 ESTABLISHED
tcp 0 0 :::6080 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8035 :::* LISTEN
tcp 0 0 :::8039 :::* LISTEN
tcp 0 0 :::7080 :::* LISTEN
tcp 0 0 :::873 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8015 :::* LISTEN
tcp 0 0 :::8080 :::* LISTEN
tcp 0 0 :::5555 :::* LISTEN
tcp 0 0 :::8019 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::9080 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8025 :::* LISTEN
tcp 0 0 :::8029 :::* LISTEN
tcp 0 0 :::5151 :::* LISTEN
tcp 0 640 ::ffff:172.16.2.2:22 ::ffff:172.16.17.11:57648 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55358 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55355 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55354 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55351 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55349 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55347 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55346 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55345 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55344 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55333 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55366 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55365 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55364 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55361 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:55360 ::ffff:172.16.3.2:3306 TIME_WAIT
tcp 0 0 ::ffff:172.16.2.2:56634 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56632 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56633 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56638 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56636 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56637 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56626 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56627 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56625 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56631 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56619 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56620 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56621 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56648 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56642 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56640 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56641 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56647 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:56644 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:53528 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55289 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55290 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55254 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55255 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55256 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55258 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:55259 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:62676 ::ffff:172.16.3.2:11211 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:62688 ::ffff:172.16.3.2:11211 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:42154 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:45857 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:40218 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:40217 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:40177 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:40176 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:40175 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:27391 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:32483 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:32482 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:8080 ::ffff:172.16.1.12:52664 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:6080 ::ffff:172.16.1.12:51377 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:5151 ::ffff:172.16.2.253:34142 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:32055 ::ffff:172.16.3.2:11211 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:7080 ::ffff:172.16.1.12:53420 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:1810 ::ffff:172.16.3.2:3306 ESTABLISHED
tcp 0 0 ::ffff:172.16.2.2:5151 ::ffff:172.16.1.12:38644 ESTABLISHED
udp 0 0 0.0.0.0:68 0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 8560 /tmp/.gdm_socket
unix 2 [ ] DGRAM 3236 @/org/kernel/udev/udevd
unix 2 [ ACC ] STREAM LISTENING 8589 /tmp/.X11-unix/X0
unix 9 [ ] DGRAM 8177 /dev/log
unix 2 [ ] DGRAM 263514767
unix 2 [ ] STREAM CONNECTED 260618162
unix 2 [ ] STREAM CONNECTED 253364545
unix 2 [ ] DGRAM 10021
unix 3 [ ] STREAM CONNECTED 8700 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8699
unix 3 [ ] STREAM CONNECTED 8695 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8694
unix 3 [ ] STREAM CONNECTED 8674 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8593
unix 2 [ ] DGRAM 8583
unix 2 [ ] DGRAM 8296
unix 2 [ ] DGRAM 8273
unix 2 [ ] DGRAM 8255
unix 2 [ ] DGRAM 8185
6.持续输出 netstat 信息
netstat 将每隔一秒输出网络信息。
netstat -c
7.显示系统不支持的地址族 (Address Families)
[root@diancai1 ~]# netstat --verbose
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 diancai1.com:47537 115.239.224.60:60000 ESTABLISHED
tcp 0 256 diancai1.com:ssh localhost:57648 ESTABLISHED
tcp 0 0 diancai1.com:55333 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56698 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56696 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56697 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56702 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56703 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56700 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56701 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56690 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56691 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56688 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56689 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56692 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56682 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56683 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56680 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56686 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56687 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56684 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56673 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56678 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56679 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56676 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56677 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56666 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56667 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56670 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56671 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56668 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:56669 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56659 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56661 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56648 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56649 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56654 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56655 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56652 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:56647 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:53528 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55289 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55290 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55254 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55255 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55256 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55258 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:55259 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:62676 localhost:11211 ESTABLISHED
tcp 0 0 diancai1.com:62688 localhost:11211 ESTABLISHED
tcp 0 0 diancai1.com:42154 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:45857 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:40218 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:40217 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:40177 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:40176 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:40175 localhost:mysql TIME_WAIT
tcp 0 0 diancai1.com:27391 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:apm-link localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:32482 localhost:mysql ESTABLISHED
tcp 0 0 diancai1.com:esri_sde localhost:34142 ESTABLISHED
tcp 0 0 diancai1.com:32055 localhost:11211 ESTABLISHED
tcp 0 0 diancai1.com:jerand-lm localhost:mysql ESTABLISHED
netstat: no support for `AF INET (sctp)' on this system.
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 3236 @/org/kernel/udev/udevd
unix 9 [ ] DGRAM 8177 /dev/log
unix 2 [ ] DGRAM 263514767
unix 2 [ ] STREAM CONNECTED 260618162
unix 2 [ ] STREAM CONNECTED 253364545
unix 2 [ ] DGRAM 10021
unix 3 [ ] STREAM CONNECTED 8700 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8699
unix 3 [ ] STREAM CONNECTED 8695 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8694
unix 3 [ ] STREAM CONNECTED 8674 /tmp/.X11-unix/X0
unix 3 [ ] STREAM CONNECTED 8593
unix 2 [ ] DGRAM 8583
unix 2 [ ] DGRAM 8296
unix 2 [ ] DGRAM 8273
unix 2 [ ] DGRAM 8255
unix 2 [ ] DGRAM 8185
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.
8.显示核心路由信息 netstat -r
[root@diancai1 ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
172.16.2.0 * 255.255.255.128 U 0 0 0 eth0
169.254.95.0 * 255.255.255.0 U 0 0 0 usb0
169.254.0.0 * 255.255.0.0 U 0 0 0 usb0
default localhost 0.0.0.0 UG 0 0 0 eth0
注意:使用 netstat -rn 显示数字格式,不查询主机名称。
9.找出程序运行的端口
并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
[root@diancai1 ~]# netstat -ap | grep java
tcp 0 0 *:6080 *:* LISTEN 24356/java
tcp 0 0 localhost.localdomain:8035 *:* LISTEN 13212/java
tcp 0 0 *:8039 *:* LISTEN 13212/java
tcp 0 0 *:7080 *:* LISTEN 24044/java
tcp 0 0 localhost.localdomain:8015 *:* LISTEN 24356/java
tcp 0 0 *:webcache *:* LISTEN 13212/java
tcp 0 0 *:personal-agent *:* LISTEN 13574/java
tcp 0 0 *:8019 *:* LISTEN 24356/java
tcp 0 0 localhost.localdomain:8025 *:* LISTEN 24044/java
tcp 0 0 *:8029 *:* LISTEN 24044/java
tcp 0 0 *:esri_sde *:* LISTEN 13574/java
tcp 0 0 diancai1.com:55333 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56682 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56683 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56684 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56760 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56754 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56752 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56753 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56759 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56756 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56757 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56746 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56747 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56744 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56745 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56751 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56748 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56749 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56738 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56737 localhost:mysql ESTABLISHED 24044/java
tcp 0 0 diancai1.com:56743 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56740 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56741 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:56732 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56733 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:56725 localhost:mysql ESTABLISHED 24356/java
tcp 0 0 diancai1.com:55289 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55290 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55254 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55255 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55256 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55258 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:55259 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:62676 localhost:11211 ESTABLISHED 13212/java
tcp 0 0 diancai1.com:62688 localhost:11211 ESTABLISHED 13212/java
tcp 0 0 diancai1.com:42154 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:45857 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:40218 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:40217 localhost:mysql ESTABLISHED 13212/java
tcp 0 0 diancai1.com:apm-link localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:32482 localhost:mysql ESTABLISHED 13574/java
tcp 0 0 diancai1.com:esri_sde localhost:34142 ESTABLISHED 13574/java
tcp 0 0 diancai1.com:32055 localhost:11211 ESTABLISHED 24356/java
tcp 0 0 diancai1.com:jerand-lm localhost:mysql ESTABLISHED 13574/java
unix 2 [ ] STREAM CONNECTED 260618162 13574/java
unix 2 [ ] STREAM CONNECTED 253364545 24356/java
找出运行在指定端口的进程
[root@diancai1 ~]# netstat -an | grep 80
tcp 0 0 :::6080 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8035 :::* LISTEN
tcp 0 0 :::8039 :::* LISTEN
tcp 0 0 :::7080 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8015 :::* LISTEN
tcp 0 0 :::8080 :::* LISTEN
tcp 0 0 :::8019 :::* LISTEN
tcp 0 0 :::9080 :::* LISTEN
tcp 0 0 ::ffff:127.0.0.1:8025 :::* LISTEN
tcp 0 0 :::8029 :::* LISTEN
tcp 0 0 ::ffff:172.16.2.2:12180 ::ffff:172.16.3.2:3306 ESTABLISHED
10.显示网络接口列表
[root@diancai1 ~]# 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
eth0 1500 0 1025699644 0 0 0 2012041617 0 0 0 BMRU
lo 16436 0 5058346 0 0 0 5058346 0 0 0 LRU
usb0 1500 0 17137488 0 0 0 8583040 0 0 0 BMRU
显示详细信息,类似 ifconfig 使用 netstat -ie:
[root@diancai1 ~]# netstat -ie
Kernel Interface table
eth0 Link encap:Ethernet HWaddr 5C:F3:FC:B9:84:B0
inet addr:172.16.2.2 Bcast:172.16.2.127 Mask:255.255.255.128
inet6 addr: fe80::5ef3:fcff:feb9:84b0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1025700373 errors:0 dropped:0 overruns:0 frame:0
TX packets:2012042214 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:561572086964 (523.0 GiB) TX bytes:236444264469 (220.2 GiB)
Interrupt:169 Memory:96000000-96012800
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5058346 errors:0 dropped:0 overruns:0 frame:0
TX packets:5058346 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1615298995 (1.5 GiB) TX bytes:1615298995 (1.5 GiB)
usb0 Link encap:Ethernet HWaddr 5E:F3:FC:BF:86:43
inet addr:169.254.95.120 Bcast:169.254.95.255 Mask:255.255.255.0
inet6 addr: fe80::5cf3:fcff:febf:8643/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:17137508 errors:0 dropped:0 overruns:0 frame:0
TX packets:8583050 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1157465313 (1.0 GiB) TX bytes:408367136 (389.4 MiB)