在Linux上使用ss命令
ss 命令是 Linux 系统上 netstat 命令的后继者。系统管理员使用该命令查看有关网络连接的信息。它允许您检查连接的状态、来源和目的地等信息。此外,ss 显示路由表、接口统计信息、伪装连接和多播成员资格。
在本指南中,您将通过示例和说明了解如何使用 ss 命令。我们将向您展示其最常见的用途以及有效使用它所需了解的所有信息。
在本教程中您将学习:
如何使用
ss命令
Linux 上的 ss 命令
常用选项
以下是此命令最常用的选项及其说明。
- -t
- 仅显示 TCP 连接。
- -a
- 显示侦听和非侦听连接。
- -s
- 显示连接统计信息的摘要。
- -n
- 显示数字地址,而不是尝试确定符号主机、端口或用户名。
- -p
- 显示哪些进程正在使用套接字。
- -e
- 显示有关套接字的扩展信息。
使用示例
注意
最好登录root帐户或使用sudo执行ss命令,因为它的许多功能需要管理员权限才能使用使用权。
让我们从最基本的 ss 命令开始,它很简单:
# ss
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 17421 * 17420
u_str ESTAB 0 0 * 79695844 * 0
u_str ESTAB 0 0 * 16718 * 16719
u_str ESTAB 0 0 * 79695893 * 0
u_str ESTAB 0 0 * 14139 * 14637
u_str ESTAB 0 0 /run/systemd/journal/stdout 14637 * 14139
u_str ESTAB 0 0 /run/systemd/journal/stdout 15486 * 15483
u_str ESTAB 0 0 * 18974 * 18975
u_str ESTAB 0 0 * 16303 * 16302
u_str ESTAB 0 0 * 15483 * 15486
此输出向我们显示有关系统上所有当前连接的信息。如果有客户端连接到计算机(例如连接到 Web 服务器的 Web 浏览器),您还会看到此处列出的这些连接。让我们看看每一列代表什么:
| Column | Description |
|---|---|
| Netid | The type of socket. It’s common to see a lot of u_str in this column, which is stream socket. Other types include IPv6 or ICMP sockets. |
| State | The state of the connection. Only useful for TCP connections since UDP is a stateless protocol. |
| Recv-Q | The number of bytes not copied by the user program connected to this socket. |
| Send-Q | The number of bytes not acknowledged by the remote host. |
| Local Address:Port | The local socket and port number used for a connection. |
| Peer Address:Port | The remote socket and port number used for a connection. |
要列出当前建立的 TCP 套接字,请使用 -t 选项。如果您还想列出侦听(未建立)的 TCP 套接字,请使用 -t -a。对于 UDP 套接字,将 -t 替换为 -u。
# ss -t
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 10.0.0.1:44798 10.0.0.1:mysql
ESTAB 0 0 10.0.0.1:mysql 192.168.71.65:54556
ESTAB 0 0 10.0.0.1:mysql 192.168.71.65:54564
ESTAB 0 0 10.0.0.1:44800 10.0.0.1:mysql
ESTAB 0 0 10.0.0.1:mysql 192.168.71.65:54558
ESTAB 0 0 10.0.0.1:mysql 10.0.0.1:44802
ESTAB 0 0 10.0.0.1:ssh 10.0.0.23:39374
使用 -p 选项显示哪些进程正在使用套接字。
# ss -t -p
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 0 10.0.2.15:39658 52.84.129.32:https users:(("MainThread",pid=3434,fd=132))
ESTAB 0 0 10.0.2.15:57130 13.249.94.65:https users:(("MainThread",pid=3434,fd=149))
ESTAB 0 0 10.0.2.15:34382 23.46.30.149:http users:(("MainThread",pid=3434,fd=128))
使用 -s 显示有关所有类型连接的统计信息摘要。
# ss -s
Total: 153 (kernel 376)
TCP: 14 (estab 7, closed 1, orphaned 0, synrecv 0, timewait 1/0), ports 0
Transport Total IP IPv6
* 376 - -
RAW 1 0 1
UDP 4 4 0
TCP 13 11 2
INET 18 15 3
FRAG 0 0 0
结论
ss 是一个用于 Linux 的一体化网络信息命令。在本指南中,我们通过示例和常用选项学习了如何使用 ss 命令。如果您想了解该命令的更多选项,请务必查看手册页。

