Linux的 netstat 命令

来源:互联网 发布:java cache 编辑:程序博客网 时间:2024/06/08 01:12

简介

Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。

输出信息含义

执行netstat后,其输出结果为

Active Internet connections (servers and established)Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1339/nginx          tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1253/sshd 
......
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  7      [ ]         DGRAM                    12370  /dev/log
unix  2      [ ]         DGRAM                    9891   @/org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    12813  @/org/freedesktop/hal/udev_event
unix  2      [ ]         DGRAM                    2672777 
......

从整体上看,netstat的输出结果可以分为两个部分:
一个是Active Internet connections,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列。这些数字一般都应该是0。如果不是,则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
另一个是Active UNIX domain sockets,其中Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。

常见参数

用法: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}      netstat [-vnNcaeol] [<Socket> ...]      netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]        -r, --route                显示路由(route)表;可以查看网关信息,很有用        -I, --interfaces=<Iface>   显示网卡接口(interface)表;可以查看在用的网卡信息        -i, --interfaces           显示网卡接口(interface)表;没看出跟I的区别        -g, --groups               显示多播组(group)的成员关系;        -s, --statistics           显示网络统计(statistics)信息;        -M, --masquerade           显示伪装的(masqueraded)链接;        -v, --verbose              详细(verbose)        -n, --numeric              不解析名称(name)        --numeric-hosts            不解析主机名(host name)        --numeric-ports            不解析端口名(port name)        --numeric-users            不解析用户名(user name)        -N, --symbolic             解析硬件名称(name)        -e, --extend               显示其他更多的扩展(extend)信息        -p, --programs             显示程序名称(PID/Program)        -c, --continuous           连续(continuous)显示;每隔一段时间一次        -l, --listening            显示正在监听的(listening)服务器套接字        -a, --all, --listening     显示所有(All)套接字,默认只显示connected的,不显示listen的        -o, --timers               显示定时器(timer)        -F, --fib                  显示FIB(Forwarding Information Base);默认        -C, --cache                显示路由缓存,来替代FIB        -T, --notrim               不截断(notrim)长地址        -Z, --context              显示SELinux安全上下文(context)  <Iface>: Name of interface to monitor/list.  <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom  <AF>=Use '-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) 

实用命令实例

1. 列出所有端口 

列出所有端口 netstat -a
列出所有 tcp 端口 netstat -at
列出所有 udp 端口 netstat -au

2. 列出所有监听状态

只显示监听端口 netstat -l
只列出所有监听 TCP 端口 netstat -lt
只列出所有监听 UDP 端口 netstat -lu
只列出所有监听 UNIX 端口 netstat -lx

3. 显示每个协议的统计信息

显示所有端口的统计信息 netstat -s
显示 TCP 或 UDP 端口的统计信息 netstat -st -su

4. 显示 PID 和进程名称 

netstat -p
可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。

5. 不显示主机,端口和用户名

当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。
netstat -an,同样可以加速输出,因为不用进行比对查询。
如果只是不想让这三个名称中的一个被显示,使用以下命令:
netsat -a --numeric-ports
netsat -a --numeric-hosts
netsat -a --numeric-users

6. 持续输出 netstat 信息。

netstat -c
将每隔一秒输出网络信息。 

7. 显示系统不支持的地址族

netstat -v
在输出的末尾,会有如下的信息:
netstat:no support for `AF IPX' on this system.
netstat: no supportfor `AF AX25' on this system.
netstat: no supportfor `AF X25' on this system.
netstat: no supportfor `AF NETROM' on this system.

8. 显示核心路由信息 

netstat -r
使用 netstat -rn 显示数字格式,不查询主机名称。

9. 找出程序运行的端口

并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
netstat -ap | grep ssh
找出运行在指定端口的进程:
netstat -an | grep':80'

10. 显示网络接口列表

netstat -i
netstat -ie

显示详细信息,像是 ifconfig 使用 netstat -ie:

参考:http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316661.html

0 0
原创粉丝点击