命令行工具--netstat

netstat命令使用

一、简介

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

二、安装

CentOS7:

yum install net-tools -y

三、常见参数

usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]

        -r, --route              display routing table
        -I, --interfaces=<Iface> display interface table for <Iface>
        -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
        --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
        -o, --timers             display timers
        -c, --continuous         continuous listing

        -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
  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 | more

2、列出所有 tcp 端口

# netstat -at

3、列出所有 udp 端口

# netstat -au

4、列出正在监听的端口

 netstat -l

5、列出所有监听的tcp端口

# netstat -lt

6、只列出所有监听 UNIX 端口

# netstat -lx

7、显示所有端口的统计信息

# netstat -s

8、显示 TCP 或 UDP 端口的统计信息

# netstat -st 
# netstat -su

9、在 netstat 输出中显示 PID 和进程名称

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

# netstat -pt

10、在 netstat 输出中不显示主机,端口和用户名 (host, port or user)

# 当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。
#同样可以加速输出,因为不用进行比对查询。
# netstat -an

# 如果只是不想让这三个名称中的一个被显示,使用以下命令
# netsat -a --numeric-ports
# netsat -a --numeric-hosts
# netsat -a --numeric-users

11、持续输出netstat信息

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

12、显示系统不支持的地址族 (Address Families)

netstat --verbose

#在输出的末尾,会有如下的信息
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.

13、显示核心路由信息

# netstat -r
# netstat -rn  不显示主机名称

14、找出程序运行的端口

#并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。
# netstat -ap | grep ssh

#找出运行在指定端口的进程
# netstat -an | grep ':80'

15、显示网络接口列表

# netstat -i
#  netstat -ie  //显示详细信息

16、 查看连接某服务端口最多的的IP地址

# netstat -nat | grep "192.168.1.15:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20

17、 TCP各种状态列表

# netstat -nat |awk '{print $6}' |sort|uniq -c

五、附录 netstat 使用手册

原文地址:https://www.cnblogs.com/yanling-coder/p/11648692.html