工学1号馆

home

netstat命令总结

Wu Yudong    October 25, 2016     Linux/Unix   570   

最近在编写unix网络编程相关的程序,所以有时候需要查看哪些端口被占用,linux下使用netstat命令用来查看,netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。

wu@ubuntu:~$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  21     [ ]         DGRAM                    10963    /dev/log
unix  3      [ ]         STREAM     CONNECTED     23670    @/tmp/dbus-g8ch26XQvq
unix  3      [ ]         STREAM     CONNECTED     16947    @/tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     14732    

…………

从整体上看,netstat的输出结果可以分为两个部分:

一个是Active Internet connections,称为有源TCP连接,其中”Recv-Q”和”Send-Q”指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。

另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。

本文地址:http://wuyudong.com/2016/10/25/2916.html,转载请注明出处。

常见参数

-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态

-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。

提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

下面来实践一下

1、列出所有端口 (包括监听和未监听的)  

列出所有端口 netstat -a

wu@ubuntu:~$ netstat -a
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  21     [ ]         DGRAM                    10963    /dev/log
unix  3      [ ]         STREAM     CONNECTED     23670    @/tmp/dbus-g8ch26XQvq
unix  3      [ ]         STREAM     CONNECTED     16947    @/tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     14732    

…………

列出所有 tcp 端口 netstat -at

wu@ubuntu:~$ netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:6379 *:* LISTEN
tcp 0 0 *:9877 *:* LISTEN
tcp 0 0 127.0.1.1:domain *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 localhost:ipp *:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN

列出所有 udp 端口 netstat -au

wu@ubuntu:~$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 127.0.1.1:domain        *:*                                
udp        0      0 *:41528                 *:*                                
udp        0      0 *:bootpc                *:*                                
udp        0      0 *:ipp                   *:*                                
udp        0      0 *:35974                 *:*                                
udp        0      0 *:mdns                  *:*                                
udp6       0      0 [::]:mdns               [::]:*                             
udp6       0      0 [::]:50046              [::]:*                             
udp6       0      0 [::]:46564              [::]:*

2. 列出所有处于监听状态的 Sockets

显示所有端口的统计信息 netstat -l

wu@ubuntu:~$ netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:6379          *:*                     LISTEN     
tcp        0      0 *:9877                  *:*                     LISTEN     
tcp        0      0 127.0.1.1:domain        *:*                     LISTEN     
tcp        0      0 *:ssh                   *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
udp        0      0 127.0.1.1:domain        *:*                                
udp        0      0 *:41528                 *:*                                
udp        0      0 *:bootpc                *:*                                
udp        0      0 *:ipp                   *:*                                
udp        0      0 *:35974                 *:*                                
udp        0      0 *:mdns                  *:*      

只列出所有监听 tcp 端口 netstat -lt

wu@ubuntu:~$ netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:6379          *:*                     LISTEN     
tcp        0      0 *:9877                  *:*                     LISTEN     
tcp        0      0 127.0.1.1:domain        *:*                     LISTEN     
tcp        0      0 *:ssh                   *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN 

只列出所有监听 udp 端口 netstat -lu

wu@ubuntu:~$ netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 127.0.1.1:domain *:*
udp 0 0 *:41528 *:*
udp 0 0 *:bootpc *:*
udp 0 0 *:ipp *:*
udp 0 0 *:35974 *:*
udp 0 0 *:mdns *:*
udp6 0 0 [::]:mdns [::]:*
udp6 0 0 [::]:50046 [::]:*
udp6 0 0 [::]:46564 [::]:*

只列出所有监听 UNIX 端口 netstat -lx

wu@ubuntu:~$ netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 15606 @/tmp/.ICE-unix/2104
unix 2 [ ACC ] STREAM LISTENING 16644 @/dbus-vfs-daemon/socket-BgVNPuOd
unix 2 [ ACC ] STREAM LISTENING 13109 @/tmp/.X11-unix/X0

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

显示所有端口的统计信息 netstat -s

wu@ubuntu:~$ netstat -s
Ip:
    1038 total packets received
    71 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    967 incoming packets delivered
    883 requests sent out
    12 outgoing packets dropped
Icmp:
    24 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:

4. 在 netstat 输出中显示 PID 和进程名称 netstat -p

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

5. 持续输出 netstat 信息

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

6. 显示核心路由信息 netstat -r

wu@ubuntu:~$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.111.2   0.0.0.0         UG        0 0          0 eth0
192.168.111.0   *               255.255.255.0   U         0 0          0 eth0

7. 找出程序运行的端口

并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。

# netstat -ap | grep ssh
tcp 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT –
tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT –

找出运行在指定端口的进程

# netstat -an | grep ‘:80′

8. 显示网络接口列表

wu@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
eth0       1500 0       850      0      0 0           810      0      0      0 BMRU
lo        65536 0       211      0      0 0           211      0      0      0 L

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

wu@ubuntu:~$ netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:0c:29:de:34:5d  
          inet addr:192.168.111.142  Bcast:192.168.111.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fede:345d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:852 errors:0 dropped:0 overruns:0 frame:0
          TX packets:812 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:647373 (647.3 KB)  TX bytes:85245 (85.2 KB)
          Interrupt:19 Base address:0x2000

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:211 errors:0 dropped:0 overruns:0 frame:0
          TX packets:211 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:17004 (17.0 KB)  TX bytes:17004 (17.0 KB)

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)