Linux netstat详解

欢迎访问我的个人博客网站:http://www.yanmin99.com/

一、netstat简介

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

二、netstat使用

  • 1、命令的一般格式

    netstat [选项]
  • 2、参数介绍

    -a (all)显示所有选项,默认不显示LISTEN相关
    -t (tcp)仅显示tcp相关选项
    -u (udp)仅显示udp相关选项
    -n 拒绝显示别名,能显示数字的全部转化成数字。
    -l 仅列出有在 Listen (监听) 的服務状态
    
    -p 显示建立相关链接的程序名
    -r 显示路由信息,路由表
    -e 显示扩展信息,例如uid等
    -s 按各个协议进行统计
    -c 每隔一个固定时间,执行该netstat命令
  • 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

你可能感兴趣的:(linux)