每天学习一个linux命令:netstat 查看网络连接情况

运行环境:Ubuntu 16.04.6 LTS

一、netstat 语法

1.1、基本语法

bingsanlang@ubuntu:~$ netstat -h
usage: netstat [-vWeenNcCF] [] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [ ...]
       netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }
-r, --route              display routing table //显示Routing Table。
-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 //直接使用IP地址,而不通过域名服务器。
--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//显示正在使用Socket的程序识别码和程序名称。
-c, --continuous         continuous listing  //持续列出网络状态。
-l, --listening          display listening server sockets//仅列出有在 Listen (监听) 的服務状态
-a, --all, --listening   display all sockets (default: connected) //显示所有连线中的Socket。
-o, --timers             display timers//显示计时器.
-F, --fib                display Forwarding Information Base (default)//显示FIB.
-C, --cache              display routing cache instead of FIB//显示路由器配置的快取信息。
  ={-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
  =Use '-6|-4' or '-A ' or '--'; 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.2、排查命令常用组合

  1. netstat -i // 显示网卡列表
  2. netstat -g // 显示组播组的关系
  3. netstat -s | more 10 //显示网络统计
  4. netstat -lntup | grep port // l:listening n:num t:tcp u:udp p:process
  5. netstat –e //用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量.
  6. netstat –r// 显示路由信息
  7. netstat -an | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a,S[a]} ’ //统计机器中网络连接各个状态个数
  8. netstat -ant|awk ‘{print $6}’|sort|uniq –c//状态全都取出来后使用uniq -c统计后再进行排序.
  9. netstat -ant|grep “192.168.25.*”|awk ‘{print $5}’|awk -F: ‘{print $1}’|sort -nr|uniq –c//查看连接某服务端口最多的的IP地址
  10. netstat -ap | grep ssh //找出程序运行的端口.
  11. netstat -pt//在 netstat 输出中显示 TCP连接信息
  12. netstat -p | grep port//找出运行在指定端口的进程

二、命令结果说明

bingsanlang@ubuntu:~$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 ubuntu:57824            server-54-192-151:https ESTABLISHED
.....省略很多

Active Internet connections:有源TCP连接,从当前的输出有6列,分别是:

Proto Recv-Q Send-Q Local Address Foreign Address State
列名称 连接协议 接收队列 发送队列 本地地址:端口 外部地址:端口 连接状态

连接状态有:

  • 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:没有任何连接状态

Active UNIX domain sockets:有源Unix域套接口,和网络套接字一样,只能用于本机通信,性能可以提高一倍。

.....省略很多
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22274    /run/user/1000/systemd/notify
.....省略很多
Proto RefCnt Flags Type State I-Node Path
列名称 连接协议 连接到本套接口上的进程号 套接口类型 套接口状态 打开套接字的进程ID(PID)和进程名 连接到套接字上的相应进程的路径名

Flags所显示的标志有:

  • SO_ACCEPTON (显示为ACC)
  • SO_WAITDATA(W)
  • SO_NOSPACE(N)。
  • SO_ACCECPTON,如果未连接套接字的相应进程正在等待连接请求,则在未连接套接字上使用这个。
    其他的标志不正常。

套接口类型Type的类型有:

  • DGRAM ,套接字以数据报(无连接)模式使用。
  • STREAM ,这是一个流(连接)套接字。
  • RAW ,套接字用作原始套接字。
  • RDM ,这个服务提供可靠传递的消息。
  • SEQPACKET ,这是一个顺序的数据包套接字。
  • PACKET ,原始接口访问套接字。
  • UNKNOWN ,不知道的类型

State的状态有:

  • FREE ,套接字还没有分配
  • LISTENING ,套接字正在监听请求。
  • CONNECTING ,套接字正在尝试连接。
  • CONNECTED ,套接字已经连接。
  • DISCONNECTING ,套接字断开连接。
  • 空,套接字没有连接到其他地方。
  • UNKNOWN ,未知的状态

三、单个命令示例

3.1 netstat -r

bingsanlang@ubuntu:~$ netstat -r   
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         bogon           0.0.0.0         UG        0 0          0 enp0s5
10.211.55.0     *               255.255.255.0   U         0 0          0 enp0s5
link-local      *               255.255.0.0     U         0 0          0 enp0s5

各个列的解释如下:

  • Destination 此列指示目标网络。default是任意网络:0.0.0.0。
  • Gateway 此列指示网络的已定义网关。 如果在此列中看到*或者0.0.0.0,则表示指定的目的网络不需要转发网关。bogon是本地默认网关的意思。
  • Genmask 此列指示网络的网络掩码。
  • Flags 此列中的U输出表示路线已启动。 G输出表示应该为此路由使用指定的网关。 D代表动态安装,M代表修改,R代表恢复。
  • MSS 此列指示此路由的TCP连接的默认最大段大小(MSS)。
  • Window 此列指示此路由上TCP连接的默认窗口大小。
  • Irtt 此列指示此路线的初始往返时间。
  • Iface Iface列显示网络接口。 如果您有多个接口,您会看到lo(用于环回),eth0(第一个以太网设备)和eth1(用于第二个以太网设备),依此类推您已安装的接口数量。

其中Flags详细解释有:

  • U 该路由可以使用
  • G 该路由是到一个网关(路由器). 若没有此标志, 说明目的地址为直接连接的.
  • H 该路由是到一个主机, 既目标地址是完整的主机地址而不是网络地址. 如果没有该标志, 说明该路由是到一个网络,所以目的地址是一个网络地址(网络号 + 子网号 + 0)
  • D 该路由是有重定向报文创建的.
  • M 该路由已被重定向报文修改

其中G和H比较重要:G区分了直接路由和间接路由, 对于直接路由, 发往直接路由的分组不仅有目的端的IP, 还有目的端的链路层地址. 但对于间接路由, 当分组被发送到间接路由时, IP地址指明的是最终目的地, 但链路层地址指明的是网关(下一站路由).
H说明了目的地址是一个完整的主机地址, 没有设置H则说明目的地址是一个网络地址(主机号为0), 当搜索路由表时, 主机地址必须与目的地址完全匹配, 而网络地址只需要匹配网络号和子网号.

其中MSS的详细解释:
MSS:Maximum Segment Size ,TCP允许提交给IP层的数据部分大小(TCP限制应用层最大的发送字节数),MSS值一般是MTU-ip头-tcp头,对于ipv4来说,ip头占20,ipv6占40。tcp头始终占20。默认MTU(Maximum Transmit Unit,最大传输单元)为1500的情况下,一般MSS都会是1460,假设应用层有2000 byte发送,需要两个segment才可以完成发送,第一个TCP segment = 1460,第二个TCP segment = 540。

3.2 netstat -i

//显示网络接口表单信息。

bingsanlang@ubuntu:~$ netstat -i
Kernel Interface table
Iface MTU Met RX-OK   RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
enp0s5 1500 0   3938993   0      0     0   3189429   0    0    0     BMRU
lo    65536 0   1658137   0      0     0   1658137   0    0    0     LRU

各个列的解释如下:

  • Iface:网络接口名称
  • MTU:设置的最大传输单元
  • RX-OK:接收-正确的数据包数
  • RX-ERR:接收-产生错误的数据包数
  • RX-DRP:接收-丢弃的数据包数
  • RX-OVR:接收-由于过速而丢失的数据包数
  • TX-OK:发送-正确的数据包数
  • TX-ERR:发送-产生错误的数据包数
  • TX-DRP:发送-丢弃的数据包数
  • TX-OVR:发送-由于过速而丢失的数据包数
  • Flg:有以下标记,
    B 已经设置了一个广播地址
    L 该接口是一个回送设备
    M 接收所有的数据包(混乱模式)
    N 避免跟踪
    O 在该接口上,禁用ARP
    P 这是一个点到点连接
    R 接口正在运行
    U 接口处于"活动"状态

3.3 netstat -g

display multicast group memberships//显示多重广播功能群组组员名单

bingsanlang@ubuntu:~$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
enp0s5          1      224.0.0.251
enp0s5          1      all-systems.mcast.net
lo              1      ip6-allnodes
lo              1      ff01::1
enp0s5          1      ff02::fb
enp0s5          1      ff02::1:ff3e:e5e1
enp0s5          1      ff02::1:ff23:82af
enp0s5          1      ff02::1:ff34:2193
enp0s5          1      ip6-allnodes
enp0s5          1      ff01::1

各个列的解释如下:

  • Interface:网络接口名称
  • RefCnt:组播地址关联的成员个数
  • Group:多重广播组

3.4 netstat -s

-s, --statistics display networking statistics (like SNMP)//显示网络工作信息统计表。

bingsanlang@ubuntu:~$ netstat -s
Ip:
    5530713 total packets received
    ...省略...
Icmp:
    3482 ICMP messages received
        ...省略...
IcmpMsg:
        InType0: 9
        ...省略...
Tcp:
    24831 active connections openings
    ...省略...
Udp:
    447862 packets received
     ...省略...
UdpLite:
TcpExt:
    1 ICMP packets dropped because they were out-of-window
     ...省略...
IpExt:
    InMcastPkts: 66300
    ...省略...

3.5 netstat -M

-M, --masquerade display masqueraded connections//显示伪装的网络连线。

bingsanlang@ubuntu:~$ netstat -M
netstat: no support for `ip_masquerade' on this system.

3.6 netstat -v

-v, --verbose be verbose //显示指令执行过程。
告诉用户存在的冗长信息。特别是打印一些关于未配置的地址家庭有用的信息。

bingsanlang@ubuntu:~$ netstat -v
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 ubuntu:50230            a2-18-212-136.depl:http ESTABLISHED
...省略...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22274    /run/user/1000/systemd/notify
...省略...
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.

比如上面最后四行输出的结果是未配置,但是这个是什么意思我还不知道,需要后期搞清楚。

3.7 netstat -W

-W, --wide don’t truncate IP addresses// 不用截取ip地址
Foreign Address显示的是全限定地址

bingsanlang@ubuntu:~$ netstat -W
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address         State
tcp        0      0 ubuntu:47622  a104-124-11-34.deploy.static.akamaitechnologies.com:http 
ESTABLISHED
...省略...
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22274    /run/user/1000/systemd/notify
...省略...

3.8 netstat -a

-a, --all, --listening display all sockets (default: connected) //显示所有连线中的Socket。

bingsanlang@ubuntu:~$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:9870                  *:*                     LISTEN
tcp        0      0 localhost:8563          *:*                     LISTEN
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN
...省略...
udp        0      0 *:47468                 *:*
udp        0      0 *:ipp                   *:*
udp        0      0 ubuntu:domain           *:*
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7
...省略...
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    22274    /run/user/1000/systemd/notify
...省略...

3.9 netstat -n

-n, --numeric don’t resolve names //直接使用IP地址,而不通过域名服务器。
比如 netstat -r 加上-n后将不再使用域名,直接使用ip
,以下是命令对比:

wanghanbing@ubuntu:~$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         bogon           0.0.0.0         UG        0 0          0 enp0s5
10.211.55.0     *               255.255.255.0   U         0 0          0 enp0s5
link-local      *               255.255.0.0     U         0 0          0 enp0s5
wanghanbing@ubuntu:~$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.211.55.1     0.0.0.0         UG        0 0          0 enp0s5
10.211.55.0     0.0.0.0         255.255.255.0   U         0 0          0 enp0s5
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 enp0s5
wanghanbing@ubuntu:~$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
udp        0      0 0.0.0.0:5353            0.0.0.0:*
raw6       0      0 :::58                   :::*
...省略...

如上所示,所有的域名、端口都数字化。

3.10 netstat --numeric-hosts

–numeric-hosts don’t resolve host names//无需解析主机名称
-n是将主机,端口和用户名进行数字化显示,如果只想将一部分显示信息进行数字话,可以选取部分参数数字化,比如将主机名称数字化:–numeric-hosts。

bingsanlang@ubuntu:~$ netstat --numeric-hosts
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 10.211.55.4:44734       54.149.210.11:https     ESTABLISHED
tcp        0      0 10.211.55.4:ssh         10.211.55.2:55512       ESTABLISHED
...省略...

3.11 netstat --numeric-ports

–numeric-ports don’t resolve port names//无需解析端口名称
实际的执行结果是将主机名和端口号数字化了,这个不知道为什么,留个//TODO

bingsanlang@ubuntu:~$ netstat --numeric-hosts
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:9870            0.0.0.0:*               LISTEN
...省略...

3.12 netstat --numeric-users

–numeric-users don’t resolve user names//无需解析用户名称

bingsanlang@ubuntu:~$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode
tcp        0      0 ubuntu:44734            ec2-54-149-210-11:https ESTABLISHED wanghanbing 18078578
tcp        0    188 ubuntu:ssh              bogon:55512             ESTABLISHED root       16884441
A
bingsanlang@ubuntu:~$ netstat -e --numeric-users
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode
tcp        0      0 10.211.55.4:44734       54.149.210.11:https     ESTABLISHED 1000       18078578
tcp        0      0 10.211.55.4:ssh         10.211.55.2:55512       ESTABLISHED 0          16884441
...省略...

3.13 netstat -N

-N, --symbolic resolve hardware names//显示网络硬件外围设备的符号连接名称。
这个用例暂时没找到

3.14 netstat -e

-e, --extend display other/more information//显示网络其他相关信息。
多显示了User和Inode信息。

bingsanlang@ubuntu:~$ netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode
tcp        0      0 ubuntu:44734            ec2-54-149-210-11:https ESTABLISHED wanghanbing 18078578
tcp        0    188 ubuntu:ssh              bogon:55512             ESTABLISHED root       16884441
A
...省略...

3.15 netstat -p

-p, --programs display PID/Program name for sockets//显示正在使用Socket的程序识别码和程序名称。
最后一列多显示了PID/Program name。

bingsanlang@ubuntu:~$ netstat -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 ubuntu:44734            ec2-54-149-210-11:https ESTABLISHED 19132/firefox
tcp        0      0 ubuntu:ssh              bogon:55512             ESTABLISHED -
...省略...

3.16 netstat -c

-c, --continuous continuous listing //持续列出网络状态。
使用这个参数后会持续输出命令结果。

wanghanbing@ubuntu:~$ netstat -rc
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         bogon           0.0.0.0         UG        0 0          0 enp0s5
10.211.55.0     *               255.255.255.0   U         0 0          0 enp0s5
link-local      *               255.255.0.0     U         0 0          0 enp0s5
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         bogon           0.0.0.0         UG        0 0          0 enp0s5
10.211.55.0     *               255.255.255.0   U         0 0          0 enp0s5
link-local      *               255.255.0.0     U         0 0          0 enp0s5
...省略...
^C

3.17 netstat -l

-l, --listening display listening server sockets//仅列出有在 Listen (监听) 的服務状态
Active UNIX domain sockets 的State是LISTENING。

bingsanlang@ubuntu:~$ netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:9870                  *:*                     LISTEN
...省略...
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     22275    /run/user/1000/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     22289    /run/user/1000/keyring/control
...省略...

3.18 netstat -o

-o, --timers display timers//显示计时器.
最后一列多显示了Timer。

wanghanbing@ubuntu:~$ netstat -o
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 ubuntu:44734            ec2-54-149-210-11:https ESTABLISHED keepalive (365.24/0/0)
tcp        0     84 ubuntu:ssh              bogon:55512             ESTABLISHED keepalive (6208.08/0/0)
tcp        0      0 ubuntu:46242            203.208.50.38:https     TIME_WAIT   timewait (41.40/0/0)
...省略...

3.19 netstat -F

-F, --fib display Forwarding Information Base (default)//显示FIB.
这个用例暂时没找到

3.20 netstat -C

-C, --cache display routing cache instead of FIB//显示路由器配置的缓存信息。
这个用例暂时没找到

3.21 类型参数

={-t|–tcp} {-u|–udp} {-w|–raw} {-x|–unix} --ax25 --ipx --netrom
=Use ‘-6|-4’ or '-A ’ or ‘–’; 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)

bingsanlang@ubuntu:~$ netstat -a -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:9870                  *:*                     LISTEN
tcp        0      0 ubuntu:ssh              bogon:55512             ESTABLISHED
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN
...省略...
bingsanlang@ubuntu:~$ netstat -a -u
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:mdns                  *:*
udp6       0      0 [::]:50810              [::]:*
...省略...

参考:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
https://man.linuxde.net/netstat
https://www.cnblogs.com/wyk930511/p/7221413.html
https://www.jianshu.com/p/3be7582ddf78
https://blog.csdn.net/freeking101/article/details/53520974
https://www.jb51.net/article/152228.htm
https://blog.csdn.net/ma2595162349/article/details/88540456
https://blog.csdn.net/woshizhangliang999/article/details/50733038
https://www.cnblogs.com/ftl1012/p/netstat.html

你可能感兴趣的:(Linux,ubuntu)