Linux命令 - netstat命令

Linux命令 - netstat命令

  Linux中 netstat 命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。
  如果你的计算机有时候接收到的数据报导致出错数据或故障,也不必感到奇怪,TCP/IP可以容许这些类型的错误,并能够自动重发数据报。但如果累计的出错情况数目占到所接收的IP数据报相当大的百分比,或者它的数目正迅速增加,那么你就应该使用netstat查一查为什么会出现这些情况了。

1.语法:

netstat [参数]

2.功能:

  用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

3.参数:
-a或--all   显示所有连线中的Socket。
-A<网络类型>或--<网络类型>   列出该网络类型连线中的相关地址。
-c或--continuous   持续列出网络状态。
-C或--cache   显示路由器配置的快取信息。
-e或--extend   显示网络其他相关信息。
-F或--fib   显示FIB。
-g或--groups   显示多重广播功能群组组员名单。
-h或--help   在线帮助。
-i或--interfaces   显示网络界面信息表单。
-l或--listening   显示监控中的服务器的Socket。
-M或--masquerade   显示伪装的网络连线。
-n或--numeric   直接使用IP地址,而不通过域名服务器。
-N或--netlink或--symbolic   显示网络硬件外围设备的符号连接名称。
-o或--timers   显示计时器。
-p或--programs   显示正在使用Socket的程序识别码和程序名称。
-r或--route   显示Routing Table。
-s或--statistice   显示网络工作信息统计表。
-t或--tcp   显示TCP传输协议的连线状况。
-u或--udp   显示UDP传输协议的连线状况。
-v或--verbose   显示指令执行过程。
-V或--version   显示版本信息。
-w或--raw   显示RAW传输协议的连线状况。
-x或--unix   此参数的效果和指定"-A unix"参数相同。
--ip或--inet   此参数的效果和指定"-A inet"参数相同。
4.常用范例:

例一:显示网络状态

命令:netstat

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

状态说明:

  • LISTEN:侦听来自远方的TCP端口的连接请求

  • SYN-SENT:再发送连接请求后等待匹配的连接请求(如果有大量这样的状态包,检查是否中招了)

  • SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认(如有大量此状态,估计被flood攻击了)

  • ESTABLISHED:代表一个打开的连接

  • FIN-WAIT-1:等待远程TCP连接中断请求,或先前的连接中断请求的确认

  • FIN-WAIT-2:从远程TCP等待连接中断请求

  • CLOSE-WAIT:等待从本地用户发来的连接中断请求

  • CLOSING:等待远程TCP对连接中断的确认

  • LAST-ACK:等待原来的发向远程TCP的连接中断请求的确认(不是什么好东西,此项出现,检查是否被攻击)

  • TIME-WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认

  • CLOSED:没有任何连接状态

[root@localhost ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 localhost:bootpc        192.168.244.254:bootps  ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    43861    /run/user/0/systemd/notify
unix  2      [ ]         DGRAM                    34689    /run/user/42/systemd/notify
unix  3      [ ]         DGRAM                    13189    /run/systemd/notify
unix  2      [ ]         STREAM     CONNECTED     45263    
unix  3      [ ]         STREAM     CONNECTED     40372    
unix  3      [ ]         STREAM     CONNECTED     39696    
unix  3      [ ]         STREAM     CONNECTED     39631    
unix  3      [ ]         STREAM     CONNECTED     38860    
Active Bluetooth connections (w/o servers)
Proto  Destination       Source            State         PSM DCID   SCID      IMTU    OMTU Security
Proto  Destination       Source            State     Channel

例二:列出所有端口

命令:netstat -a

  显示一个所有的有效连接信息列表,包括已建立的连接(ESTABLISHED),也包括监听连接请(LISTENING)的那些连接。

[root@localhost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
udp        0      0 localhost:domain        0.0.0.0:*                          
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     47135    /tmp/.esd-0/socket
unix  2      [ ACC ]     STREAM     LISTENING     38161    @/tmp/dbus-qoyeuxO3M1
unix  2      [ ACC ]     STREAM     LISTENING     45272    @/tmp/dbus-jELgg1sf
unix  3      [ ]         STREAM     CONNECTED     38860    
Active Bluetooth connections (servers and established)
Proto  Destination       Source            State         PSM DCID   SCID      IMTU    OMTU Security
Proto  Destination       Source            State     Channel

例三:显示当前UDP连接状况

命令:netstat -nu

[root@localhost ~]# netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 192.168.244.128:68      192.168.244.254:67      ESTABLISHED

例四:显示UDP端口号的使用情况

命令:netstat -apu

[root@localhost ~]# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 localhost:domain        0.0.0.0:*                           1774/dnsmasq        
udp        0      0 0.0.0.0:bootps          0.0.0.0:*                           1774/dnsmasq        
udp        0      0 localhost:bootpc        192.168.244.254:bootps  ESTABLISHED 1056/NetworkManager 
udp        0      0 0.0.0.0:sunrpc          0.0.0.0:*                           1/systemd           
udp        0      0 0.0.0.0:38055           0.0.0.0:*                           888/avahi-daemon: r 
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                           888/avahi-daemon: r 
udp        0      0 localhost:323           0.0.0.0:*                           903/chronyd         
udp6       0      0 [::]:sunrpc             [::]:*                              1/systemd           
udp6       0      0 [::]:36032              [::]:*                              888/avahi-daemon: r 
udp6       0      0 [::]:mdns               [::]:*                              888/avahi-daemon: r 
udp6       0      0 localhost:323           [::]:*                              903/chronyd 

例五:显示网卡列表

命令:netstat -i

[root@localhost ~]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33            1500     8747      0      0 0          1937      0      0      0 BMRU
lo              65536        2      0      0 0             2      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU

例六:显示组播组的关系

命令:netstat -g

[root@localhost ~]# netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
ens33           1      224.0.0.251
ens33           1      all-systems.mcast.net
virbr0          1      224.0.0.251
virbr0          1      all-systems.mcast.net
lo              1      ff02::1
lo              1      ff01::1
ens33           1      ff02::1:fffa:1739
ens33           1      ff02::fb
ens33           1      ff02::1
ens33           1      ff01::1
virbr0          1      ff02::1
virbr0          1      ff01::1
virbr0-nic      1      ff02::1
virbr0-nic      1      ff01::1

例七:显示网络统计信息

命令:netstat -s

  按照各个协议分别显示其统计数据。如果我们的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么我们就可以用本选项来查看一下所显示的信息。我们需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。

[root@localhost ~]# netstat -s
Ip:
    Forwarding: 1
    2811 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    1945 incoming packets delivered
    1727 requests sent out
    139 dropped because of missing route
Icmp:
    2 ICMP messages received
    0 input ICMP message failed
    ICMP input histogram:
        destination unreachable: 2
    4 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 4
IcmpMsg:
        InType3: 2
        OutType3: 4
Tcp:
    17 active connection openings
    0 passive connection openings
    0 failed connection attempts
    0 connection resets received
    0 connections established
    1546 segments received
    1189 segments sent out
    0 segments retransmitted
    0 bad segments received
    3 resets sent
Udp:
    397 packets received
    0 packets to unknown port received
    0 packet receive errors
    530 packets sent
    0 receive buffer errors
    0 send buffer errors
UdpLite:
TcpExt:
    16 TCP sockets finished time wait in fast timer
    2 delayed acks sent
    1360 packet headers predicted
    17 acknowledgments not containing data payload received
    18 predicted acknowledgments
    TCPRcvCoalesce: 47
    TCPOrigDataSent: 35
    TCPDelivered: 52
IpExt:
    InMcastPkts: 588
    OutMcastPkts: 76
    InBcastPkts: 275
    OutBcastPkts: 6
    InOctets: 5728754
    OutOctets: 101124
    InMcastOctets: 45124
    OutMcastOctets: 8924
    InBcastOctets: 22196
    OutBcastOctets: 468
    InNoECTPkts: 5332
MPTcpExt:

例八:显示监听的套接口

命令:netstat -l

[root@localhost ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN     
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN     
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN     
udp        0      0 localhost:domain        0.0.0.0:*                                                 
udp        0      0 0.0.0.0:38055           0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp        0      0 localhost:323           0.0.0.0:*                                 
udp6       0      0 [::]:mdns               [::]:*                             
udp6       0      0 localhost:323           [::]:*                             
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7          
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     45272    @/tmp/dbus-jELgg1sf
unix  2      [ ACC ]     STREAM     LISTENING     22746    /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     22646    @/org/kernel/linux/storage/multipathd
unix  2      [ ACC ]     STREAM     LISTENING     37563    @/tmp/.ICE-unix/1809
unix  2      [ ACC ]     STREAM     LISTENING     31770    /run/gssproxy.sock
unix  2      [ ACC ]     STREAM     LISTENING     45085    /run/user/0/wayland-0
Active Bluetooth connections (only servers)
Proto  Destination       Source            State         PSM DCID   SCID      IMTU    OMTU Security
Proto  Destination       Source            State     Channel

例九:显示所有已建立的有效连接

命令:netstat -n

[root@localhost ~]# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 192.168.244.128:68      192.168.244.254:67      ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    43861    /run/user/0/systemd/notify
unix  2      [ ]         DGRAM                    34689    /run/user/42/systemd/notify
unix  3      [ ]         DGRAM                    13189    /run/systemd/notify
unix  2      [ ]         DGRAM                    13191    /run/systemd/cgroups-agent
unix  8      [ ]         DGRAM                    13205    /run/systemd/journal/socket
unix  24     [ ]         DGRAM                    13220    /run/systemd/journal/dev-log
unix  2      [ ]         DGRAM                    28360    /var/run/chrony/chronyd.sock
unix  3      [ ]         STREAM     CONNECTED     39631    
unix  3      [ ]         STREAM     CONNECTED     38860    
Active Bluetooth connections (w/o servers)
Proto  Destination       Source            State         PSM DCID   SCID      IMTU    OMTU Security
Proto  Destination       Source            State     Channel

例十:显示关于以太网的统计数据

命令:netstat -e

  用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)

[root@localhost ~]# netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode     
udp        0      0 localhost:bootpc        192.168.244.254:bootps  ESTABLISHED root       73574     
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    43861    /run/user/0/systemd/notify
unix  2      [ ]         DGRAM                    34689    /run/user/42/systemd/notify
unix  3      [ ]         DGRAM                    13189    /run/systemd/notify
unix  3      [ ]         STREAM     CONNECTED     39631    
unix  3      [ ]         STREAM     CONNECTED     38860    
Active Bluetooth connections (w/o servers)
Proto  Destination       Source            State         PSM DCID   SCID      IMTU    OMTU Security
Proto  Destination       Source            State     Channel

例十一:统计机器中网络连接各个状态个数

命令:netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

[root@localhost ~]# netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
LISTEN 7

例十二:查看连接某服务端口最多的的IP地址

命令:netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20

[root@localhost ~]# netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20
      8 10.2.1.68
      7 192.168.119.13
      6 192.168.119.201
      6 192.168.119.20
      6 192.168.119.10
      4 10.2.1.199
      3 10.2.1.207
      2 192.168.120.20
      2 192.168.120.15
      2 192.168.119.197
      2 192.168.119.11
      2 10.2.1.206
      2 10.2.1.203
      2 10.2.1.189
      2 10.2.1.173
      1 192.168.120.18
      1 192.168.119.19
      1 10.2.2.227
      1 10.2.2.138
      1 10.2.1.208

例十三:找出程序运行的端口

命令:netstat -ap | grep ssh

[root@localhost ~]# netstat -ap | grep ssh
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      1064/sshd           
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      1064/sshd           
unix  2      [ ACC ]     STREAM     LISTENING     44690    2142/gnome-keyring-  /run/user/0/keyring/ssh
unix  3      [ ]         STREAM     CONNECTED     30618    1064/sshd            
unix  2      [ ]         STREAM     CONNECTED     31204    1064/sshd  

例十四:找出运行在指定端口的进程

命令:netstat -anpt | grep ':16064'

  运行在端口16064的进程id为24596,再通过ps命令就可以找到具体的应用程序了。

[root@localhost ~]# netstat -anpt | grep ':16064'
tcp        0      0 :::16064                    :::*                        LISTEN      24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.201:6462 ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:26341 ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32208 ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32207 ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51303      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51302      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50020      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50019      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56155      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50681      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50680      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:52136      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56989      ESTABLISHED 24594/java          
tcp        0      0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56988      ESTABLISHED 24594/java

例十五:查看某端口号是否被占用

命令:netstat -nlp | grep 20670

[root@localhost ~]# netstat -nlp | grep 20670 
unix  2      [ ACC ]     STREAM     LISTENING     20670  3115/firefox        /tmp/orbit-root/linc-c2b-0-5734667cbe29
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值