LINUX下完全可以替代netstat命令的ss命令详解

科技   2024-10-17 16:33   河北  

1.ss命令简介

ss 命令是 Socket Statistics 的缩写,也称为 IPC(Inter-Process Communication)套接字统计。它是一个功能强大的网络管理工具,主要用于获取系统中 socket 的统计信息。ss 命令帮助系统管理员诊断和排查网络问题,如检查当前网络连接及端口状态、搜索网络问题、统计网络情况和过滤网络数据等,从而有效解决网络问题,减少管理负担,并确保网络安全。

相对于 netstat ,它有如下优势:

  • 详细信息:ss 能够显示更多详细的 TCP 和连接状态信息。

  • 性能优越:在处理大量 socket 连接时,ss 命令比 netstat 更快更高效。这得益于 ss 利用了 TCP 协议栈中的 tcp_diag 模块,直接从 Linux 内核获取第一手信息,从而拥有更好的性能。

另外,ss 命令的使用非常灵活,可以通过添加不同的参数来查看不同类型的信息:

  • -a:列出所有连接状态。
  • -t:仅列出 TCP 连接状态。
  • -u:仅列出 UDP 连接状态。
  • -l:仅列出监听状态。
  • -s:列出已建立的连接状态。

这些参数可以组合使用,以满足不同的查看需求。通过这些选项,用户可以根据具体情况选择所需的信息,从而更加高效地管理和分析网络状况。

2.ss命令使用格式

2.1命令格式
ss [选项]...
2.2常用选项

当不使用任何选项时,ss 显示已建立连接的开放非监听套接字(例如TCP/UNIX/UDP)的列表。

下面列出 ss 常用选项:

选项描述
-t显示TCP套接字
-u显示UDP套接字
-w显示RAW套接字
-x显示UNIX域套接字
-a显示所有套接字(包括监听和非监听)
-l显示监听套接字
-p显示进程信息(需要root权限)
-n不解析主机名、端口号和用户名
-r解析主机名(但可能增加延迟)
-i显示内部TCP信息
-4仅显示IPv4套接字
-6仅显示IPv6套接字
-o显示计时器信息
-e显示扩展信息(如UID等)
-f显示套接字类型(例如:tcp、udp等)
-s显示套接字统计信息(如总接收、总发送等)
-m显示套接字的内存使用情况
-d显示套接字调试信息
-v显示详细模式(可能包含更多内部信息)

3. ss命令的常见(组合)用法

用法描述命令示例
显示所有 TCP 套接字信息ss -tass -t -a
显示所有 UDP 套接字信息ss -uass -u -a
显示所有监听状态的套接字及其对应的进程信息ss -lpss -l -p(需要root权限)
以数字格式显示所有套接字信息及其统计摘要ss -nasss -n -a -s

4. ss 应用场景举例

4.1监控网络连接状态

ss 命令可以用来实时查看系统的网络 tcp 连接状态,如下命令查看所有已建立的 TCP 连接:

[root@myiscsi ~]# ss -ta
State       Recv-Q Send-Q                      Local Address:Port                            Peer Address:Port     
LISTEN      0      128                                     *:sunrpc                                    *:*       
LISTEN      0      5                           192.168.122.1:domain                                    *:*         
LISTEN      0      128                                     *:ssh                                       *:*         
LISTEN      0      128                             127.0.0.1:ipp                                       *:*         LISTEN      0      100                             127.0.0.1:smtp                                      *:*         
LISTEN      0      128                             127.0.0.1:x11-ssh-offset                            *:*         LISTEN      0      256                                     *:iscsi-target                              *:*         
ESTAB       0      52                        192.168.250.153:ssh                          123.113.158.11:avenue     
LISTEN      0      128                                     [::]:sunrpc                                 [::]:*       
LISTEN      0      128                                     [::]:ssh                                    [::]:*       
LISTEN      0      128                                     [::1]:ipp                                   [::]:*       
LISTEN      0      100                                     [::1]:smtp                                  [::]:*       
LISTEN      0      128                                     [::1]:x11-ssh-offset                        [::]:*       
[root@myiscsi ~]

其中包括了本地地址(Local Address)、对端(远程)地址(Peer Address)、状态(State)等信息。LISTEN 表示该端口正在监听;ESTAB 表示连接已经建立。

4.2查找指定端口的使用情况
[root@openvpn ~]# ss -lp | grep :1194
udp UNCONN 0 0 *:1194 *:* users:(("openvpn",pid=5000,fd=6))
[root@openvpn ~]#
4.3检查系统的监听套接字

监听套接字是系统等待外部连接的地方。使用 ss 命令可以轻松检查这些套接字:

[root@openvpn ~]# ss -l
Netid State      Recv-Q Send-Q                                                       Local Address:Port                                                                        Peer Address:Port                
nl    UNCONN     0      0                                                                     rtnl:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                     rtnl:packagekitd/1985                                                                        *                     
nl    UNCONN     0      0                                                                     rtnl:NetworkManager/870                                                                      *                     
nl    UNCONN     0      0                                                                     rtnl:avahi-daemon/755                                                                        *                     
nl    UNCONN     0      0                                                                     rtnl:1775                                                                                    *                     
nl    UNCONN     0      0                                                                     rtnl:-637533010                                                                              *                     
nl    UNCONN     0      0                                                                     rtnl:796918958                                                                               *                     
nl    UNCONN     0      0                                                                     rtnl:packagekitd/1985                                                                        *                     
nl    UNCONN     0      0                                                                     rtnl:1775                                                                                    *                     
nl    UNCONN     0      0                                                                     rtnl:NetworkManager/870                                                                      *                     
nl    UNCONN     0      0                                                                     rtnl:avahi-daemon/755                                                                        *                     
nl    UNCONN     4352   0                                                                  tcpdiag:ss/5177                                                                                 *                     
nl    UNCONN     768    0                                                                  tcpdiag:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                     xfrm:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                  selinux:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                    audit:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                    audit:systemd/1                                                                               *                     
nl    UNCONN     0      0                                                                    audit:auditd/694                                                                              *                     
nl    UNCONN     0      0                                                                fiblookup:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                connector:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                      nft:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                   uevent:gnome-shell/1893                                                                        *                     
nl    UNCONN     0      0                                                                   uevent:kernel                                                                                  *                     
nl    UNCONN     0      0                                                                   uevent:libvirtd/1198                                                                           *                     
nl    UNCONN     0      0                                                                   uevent:-4145                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:ModemManager/776                                                                        *                     
nl    UNCONN     0      0                                                                   uevent:colord/2073                                                                             *                     
nl    UNCONN     0      0                                                                   uevent:-4157                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:systemd/1                                                                               *                     
nl    UNCONN     0      0                                                                   uevent:pulseaudio/1930                                                                         *                     
nl    UNCONN     0      0                                                                   uevent:udisksd/729                                                                             *                     
nl    UNCONN     0      0                                                                   uevent:tuned/1185                                                                              *                     
nl    UNCONN     0      0                                                                   uevent:-4129                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:systemd-logind/753                                                                      *                     
nl    UNCONN     0      0                                                                   uevent:gsd-media-keys/2021                                                                     *                     
nl    UNCONN     0      0                                                                   uevent:-4130                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4113                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:upowerd/1901                                                                            *                     
nl    UNCONN     0      0                                                                   uevent:-4158                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:X/1298                                                                                  *                     
nl    UNCONN     0      0                                                                   uevent:boltd/1984                                                                              *                     
nl    UNCONN     0      0                                                                   uevent:-4139                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-54524754                                                                               *                     
nl    UNCONN     0      0                                                                   uevent:NetworkManager/870                                                                      *                     
nl    UNCONN     0      0                                                                   uevent:-4140                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4131                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4158                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4157                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:colord/2073                                                                             *                     
nl    UNCONN     0      0                                                                   uevent:gsd-media-keys/2021                                                                     *                     
nl    UNCONN     0      0                                                                   uevent:boltd/1984                                                                              *                     
nl    UNCONN     0      0                                                                   uevent:pulseaudio/1930                                                                         *                     
nl    UNCONN     0      0                                                                   uevent:gnome-shell/1893                                                                        *                     
nl    UNCONN     0      0                                                                   uevent:upowerd/1901                                                                            *                     
nl    UNCONN     0      0                                                                   uevent:libvirtd/1198                                                                           *                     
nl    UNCONN     0      0                                                                   uevent:-54524754                                                                               *                     
nl    UNCONN     0      0                                                                   uevent:-4145                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:tuned/1185                                                                              *                     
nl    UNCONN     0      0                                                                   uevent:X/1298                                                                                  *                     
nl    UNCONN     0      0                                                                   uevent:-4140                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4139                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:NetworkManager/870                                                                      *                     
nl    UNCONN     0      0                                                                   uevent:ModemManager/776                                                                        *                     
nl    UNCONN     0      0                                                                   uevent:udisksd/729                                                                             *                     
nl    UNCONN     0      0                                                                   uevent:-4131                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4130                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:-4129                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:systemd-logind/753                                                                      *                     
nl    UNCONN     0      0                                                                   uevent:-4113                                                                                   *                     
nl    UNCONN     0      0                                                                   uevent:systemd/1                                                                               *                     
nl    UNCONN     0      0                                                                     genl:NetworkManager/870                                                                      *                     
nl    UNCONN     0      0                                                                     genl:kernel                                                                                  *                     
nl    UNCONN     0      0                                                               scsi-trans:kernel                                                                                  *                     
p_dgr UNCONN     0      0                                                                      arp:virbr0                                                                                  *                     
p_dgr UNCONN     0      0                                                                      arp:eth0                                                                                    *                     
u_str LISTEN     0      128                                                   @/tmp/.ICE-unix/1840 29479                                                                                  * 0                    
u_str LISTEN     0      10                                                     @/tmp/dbus-q5sCHifn 31750                                                                                  * 0                    
u_str LISTEN     0      5                                                     /var/run/lsm/ipc/sim 23056                                                                                  * 0                    
u_str LISTEN     0      5                                                    /var/run/lsm/ipc/simc 23058                                                                                  * 0                    
u_str LISTEN     0      128                                         /var/run/libvirt/virtlogd-sock 18709                                                                                  * 0                    
u_str LISTEN     0      128                                                /var/run/cups/cups.sock 18712                                                                                  * 0                    
u_str LISTEN     0      128                                                  /var/run/rpcbind.sock 18714                                                                                  * 0                    
u_str LISTEN     0      128                                               /run/lvm/lvmpolld.socket 11804                                                                                  * 0                    
u_str LISTEN     0      128                                                     @/tmp/.X11-unix/X0 29033                                                                                  * 0                    
u_str LISTEN     0      128                                          /var/run/libvirt/libvirt-sock 27435                                                                                  * 0                    
u_str LISTEN     0      128                                       /var/run/libvirt/libvirt-sock-ro 27437                                                                                  * 0                    
u_str LISTEN     0      128                                                      /tmp/.X11-unix/X0 29034                                                                                  * 0                    
u_str LISTEN     0      20                                     /var/run/libvirt/libvirt-admin-sock 27439                                                                                  * 0                    
u_str LISTEN     0      128                                        /var/run/libvirt/virtlockd-sock 18739                                                                                  * 0                    
u_str LISTEN     0      128                                           /var/run/avahi-daemon/socket 18742                                                                                  * 0                    
u_str LISTEN     0      128                                            /run/dbus/system_bus_socket 18745                                                                                  * 0                    
u_str LISTEN     0      128                                                /run/lvm/lvmetad.socket 11834                                                                                  * 0                    
u_str LISTEN     0      128                             /var/run/spice-vdagentd/spice-vdagent-sock 18748                                                                                  * 0                    
u_str LISTEN     0      100                                                          public/pickup 28521                                                                                  * 0                    
u_str LISTEN     0      10                                                     @/tmp/dbus-DDQNvigj 31752                                                                                  * 0                    
u_str LISTEN     0      128                                         @ISCSID_UIP_ABSTRACT_NAMESPACE 18750                                                                                  * 0                    
u_str LISTEN     0      100                                                            public/qmgr 28528                                                                                  * 0                    
u_str LISTEN     0      100                                                           public/flush 28550                                                                                  * 0                    
u_str LISTEN     0      100                                                           public/showq 28565                                                                                  * 0                    
u_dgr UNCONN     0      0                                                      /run/systemd/notify 6748                                                                                   * 0                    
u_dgr UNCONN     0      0                                               /run/systemd/cgroups-agent 6750                                                                                   * 0                    
u_str LISTEN     0      10                                          /var/lib/gssproxy/default.sock 17094                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/relay 28562                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/error 28568                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/retry 28571                                                                                  * 0                    
u_str LISTEN     0      128                                            /run/systemd/journal/stdout 6761                                                                                   * 0                    
u_dgr UNCONN     0      0                                              /run/systemd/journal/socket 6764                                                                                   * 0                    
u_str LISTEN     0      128                                                   /run/systemd/private 11629                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                 /dev/log 6766                                                                                   * 0                    
u_str LISTEN     0      100                                                         private/bounce 28538                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/defer 28541                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/trace 28544                                                                                  * 0                    
u_str LISTEN     0      100                                                         private/verify 28547                                                                                  * 0                    
u_str LISTEN     0      100                                                        private/discard 28574                                                                                  * 0                    
u_str LISTEN     0      10                                                     @/tmp/dbus-bt0fEHnX 31753                                                                                  * 0                    
u_str LISTEN     0      100                                                       private/proxymap 28553                                                                                  * 0                    
u_str LISTEN     0      100                                                     private/proxywrite 28556                                                                                  * 0                    
u_str LISTEN     0      100                                                           private/smtp 28559                                                                                  * 0                    
u_dgr UNCONN     0      0                                                   /run/systemd/shutdownd 11681                                                                                  * 0                    
u_str LISTEN     0      128                                                  @/tmp/dbus-ZK1RGgbDSP 29442                                                                                  * 0                    
u_str LISTEN     0      10                                                  /var/run/mcelog-client 21942                                                                                  * 0                    
u_str LISTEN     0      10                                                     @/tmp/dbus-VyAPcYHI 30663                                                                                  * 0                    
u_seq LISTEN     0      128                                                      /run/udev/control 11708                                                                                  * 0                    
u_str LISTEN     0      128                                           @ISCSIADM_ABSTRACT_NAMESPACE 18741                                                                                  * 0                    
u_str LISTEN     0      10                                                      /run/gssproxy.sock 17095                                                                                  * 0                    
u_str LISTEN     0      10                                                     @/tmp/dbus-1WJW8vdf 31749                                                                                  * 0                    
u_str LISTEN     0      100                                                         private/tlsmgr 28532                                                                                  * 0                    
u_str LISTEN     0      100                                                        private/rewrite 28535                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/local 28577                                                                                  * 0                    
u_str LISTEN     0      100                                                        private/virtual 28580                                                                                  * 0                    
u_str LISTEN     0      100                                                           private/lmtp 28583                                                                                  * 0                    
u_str LISTEN     0      100                                                          private/anvil 28586                                                                                  * 0                    
u_str LISTEN     0      100                                                         private/scache 28589                                                                                  * 0                    
u_dgr UNCONN     0      0                                             /var/run/chrony/chronyd.sock 20700                                                                                  * 0                    
u_str LISTEN     0      10                                               /var/run/abrt/abrt.socket 22760                                                                                  * 0                    
u_str LISTEN     0      128                                                    /tmp/.ICE-unix/1840 29480                                                                                  * 0                    
u_str LISTEN     0      100                                                         public/cleanup 28525                                                                                  * 0                    
u_str LISTEN     0      128                                                  @/tmp/dbus-anSesXhA5C 30465                                                                                  * 0                    
u_str LISTEN     0      5                                                /run/user/42/pulse/native 29694                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 18824                                                                                  * 18825                
u_dgr UNCONN     0      0                                                                        * 17079                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 2032392                                                                                * 6766                 
u_dgr UNCONN     0      0                                                                        * 18218                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 11245                                                                                  * 6764                 
u_dgr UNCONN     0      0                                                                        * 9916                                                                                   * 6764                 
u_dgr UNCONN     0      0                                                                        * 22762                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 20961                                                                                  * 6766                 
u_dgr UNCONN     0      768                                                                      * 18825                                                                                  * 18824                
u_dgr UNCONN     0      0                                                                        * 9907                                                                                   * 6748                 
u_dgr UNCONN     0      0                                                                        * 2036960                                                                                * 6766                 
u_dgr UNCONN     0      0                                                                        * 27128                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 11261                                                                                  * 11262                
u_dgr UNCONN     0      0                                                                        * 32650                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 20337                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 33816                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 19250                                                                                  * 6764                 
u_dgr UNCONN     0      0                                                                        * 29678                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 22733                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 30735                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 29410                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 18074                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 33291                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 20693                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 26507                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 33817                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 34830                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 17957                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 27642                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 33536                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 23464                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 18676                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 30015                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 24712                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 30913                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 20686                                                                                  * 6766                 
u_dgr UNCONN     0      0                                                                        * 34171                                                                                  * 0                    
u_dgr UNCONN     0      0                                                                        * 11262                                                                                  * 11261                
raw   UNCONN     46080  0                                                                     [::]:ipv6-icmp                                                                           [::]:*                    
udp   UNCONN     0      0                                                                        *:897                                                                                    *:*                    
udp   UNCONN     0      0                                                                        *:50172                                                                                  *:*                    
udp   UNCONN     0      0                                                                        *:openvpn                                                                                *:*                    
udp   UNCONN     0      0                                                                        *:mdns                                                                                   *:*                    
udp   UNCONN     0      0                                                            192.168.122.1:domain                                                                                 *:*                    
udp   UNCONN     0      0                                                                 *%virbr0:bootps                                                                                 *:*                    
udp   UNCONN     0      0                                                                        *:sunrpc                                                                                 *:*                    
udp   UNCONN     0      0                                                                127.0.0.1:323                                                                                    *:*                    
udp   UNCONN     0      0                                                                     [::]:897                                                                                 [::]:*                    
udp   UNCONN     0      0                                                                     [::]:sunrpc                                                                              [::]:*                    
udp   UNCONN     0      0                                                                    [::1]:323                                                                                 [::]:*                    
tcp   LISTEN     0      128                                                                      *:sunrpc                                                                                 *:*                    
tcp   LISTEN     0      5                                                            192.168.122.1:domain                                                                                 *:*                    
tcp   LISTEN     0      128                                                                      *:ssh                                                                                    *:*                    
tcp   LISTEN     0      128                                                              127.0.0.1:ipp                                                                                    *:*                    
tcp   LISTEN     0      100                                                              127.0.0.1:smtp                                                                                   *:*                    
tcp   LISTEN     0      128                                                              127.0.0.1:x11-ssh-offset                                                                         *:*                    
tcp   LISTEN     0      128                                                                   [::]:sunrpc                                                                              [::]:*                    
tcp   LISTEN     0      128                                                                   [::]:ssh                                                                                 [::]:*                    
tcp   LISTEN     0      128                                                                  [::1]:ipp                                                                                 [::]:*                    
tcp   LISTEN     0      100                                                                  [::1]:smtp                                                                                [::]:*                    
tcp   LISTEN     0      128                                                                  [::1]:x11-ssh-offset                                                                      [::]:*                    
[root@openvpn ~]
4.4查看套接字的摘要信息

查看套接字连接的总体统计数据,包括TCP、UDP、IPv4和IPv6连接的数量:

[root@myiscsi ~]# ss -s
Total: 633 (kernel 825)
TCP:   14 (estab 1, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*   825       -         -        
RAW   0         0         0        
UDP   10        7         3        
TCP   13        8         5        
INET   23        15        8        
FRAG   0         0         0        

[root@myiscsi ~]

Python运维实践
Python运维实践,专注于互联网技术的总结与交流,内容涉及Python自动化运维、Django框架、园区网络技术、linux云计算、系统架构及网络空间安全等知识的实践与分享。
 最新文章