3、实例
A、列出所有端口
列出所有端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
列出所有tcp端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 121.41.120.65:http 140.205.201.20:35425 TIME_WAIT
列出所有udp端口
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 121.41.120.65:ntp *:*
udp 0 0 iZ235ubl1arZ:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp6 0 0 [::]:ntp [::]:*
B、列出所有处于监听状态的Sockets
只显示监听端口netstat -l
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
udp 0 0 121.41.120.65:ntp *:*
udp 0 0 iZ235ubl1arZ:ntp *:*
udp 0 0 localhost:ntp *:*
只列出所有监听tcp端口netstat -lt
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
只列出所有监听udp端口 netstat -lu
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 121.41.120.65:ntp *:*
udp 0 0 iZ235ubl1arZ:ntp *:*
udp 0 0 localhost:ntp *:*
udp 0 0 *:ntp *:*
udp6 0 0 [::]:ntp [::]:*
只列出所有监听UNIX端口 netstat -lx
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 9506 /var/run/nscd/socket
unix 2 [ ACC ] STREAM LISTENING 8267 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] SEQPACKET LISTENING 7769 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 7592 @/com/ubuntu/upstart
C、在netstat 输出中显示PID和进程名称netstat -p
root@iZ235ubl1arZ:/home/yanmin/blog# netstat -p
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 316 121.41.120.65:ssh 58.246.62.146:54175 ESTABLISHED 17558/sshd: yanmin
tcp 0 0 121.41.120.65:ssh 58.246.62.146:52592 ESTABLISHED 14538/sshd: yanmin
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 10 [ ] DGRAM 8286 682/rsyslogd /dev/log
unix 3 [ ] STREAM CONNECTED 307127 17558/sshd: yanmin
D、显示核心路由信息 netstat -r
yanmin:blog yanmin$ netstat -r
Routing tables
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 10.96.150.1 UGSc 21 0 en0
10.16/14 10.254.254.81 UGSc 0 0 utun1
10.16.0.222/32 10.254.254.81 UGSc 1 0 utun1