# 如何通过命令行查看已建立的服务器连接

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/37987.html | 发布：2026-03-20 | 更新：2026-03-20 | 分类：投稿分享

## 一、常用命令概览

 

在Linux服务器上，可通过以下三个主流命令查看当前已建立的TCP连接（即状态为 `ESTABLISHED` 的连接）：

 
 
- **`netstat`**（传统工具，部分新系统需手动安装） 
- **`ss`**（`iproute2` 套件组件，性能更优，推荐首选） 
- **`lsof`**（功能强大，可关联进程与端口，需安装） 

 

## 二、使用 `ss` 查看已建立连接（推荐）

 

`ss` 是现代Linux发行版默认预装的高性能替代方案，执行速度快、资源占用低。

 

```
# 查看所有已建立的TCP连接（含本地/远程IP和端口）
ss -tn state established

# 显示对应进程名和PID（需root权限）
sudo ss -tunlp state established

# 仅显示IPv4的已建立连接（排除IPv6干扰）
ss -t4n state established
```

 

**参数说明：**  
`-t`：TCP协议；`-n`：数字格式（不解析主机名/服务名）；`-l`：监听状态（此处不用）；`-p`：显示进程信息（需sudo）；`-u`：UDP（本例不适用）；`-4`：仅IPv4。

 

![游戏服务器租用需要注意哪些方面？](https://www.dreamfly.com.hk/uploads/allimg/250319/115944XQ-0.png)

 

## 三、使用 `netstat` 查看（兼容性高）

 

若系统支持 `netstat`（如CentOS 7、Ubuntu 18.04及旧版本），可使用：

 

```
# 查看所有ESTABLISHED连接
netstat -tn | grep ESTABLISHED

# 显示进程与连接详情（需root）
sudo netstat -tunlp | grep ESTABLISHED
```

 

**注意：**Debian/Ubuntu 22.04+ 及 CentOS/RHEL 8+ 默认不再预装 `net-tools`，可运行 `sudo apt install net-tools`（Debian/Ubuntu）或 `sudo dnf install net-tools`（RHEL/CentOS 8+）补装。

 

## 四、使用 `lsof` 定位连接所属进程

 

当需明确某连接由哪个服务（如nginx、mysql、ssh）发起或接受时，`lsof` 更直观：

 

```
# 查看所有已建立的TCP连接及其进程
sudo lsof -iTCP -sTCP:ESTABLISHED -n -P

# 筛选特定端口（如22端口SSH连接）
sudo lsof -i :22 -sTCP:ESTABLISHED
```

 

**提示：**`lsof` 需安装：`sudo apt install lsof`（Debian/Ubuntu）或 `sudo yum install lsof`（CentOS 7）/`sudo dnf install lsof`（CentOS 8+）。

 

## 五、实用技巧与安全建议

 
 
- **快速统计连接数：**`ss -tn state established | wc -l` 
- **排查异常外连：**结合 `awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr` 分析高频远程IP 
- **安全审计：**定期检查非预期的ESTABLISHED连接，警惕可疑IP或未知进程 
- **替代方案：**对于容器化环境（Docker/K8s），可配合 `docker exec -it <container> ss -tn state established` 查看容器内连接 

 

## 六、常见连接状态说明

 

| 状态 | 含义 |
|---|---|
| `ESTABLISHED` | 连接已成功建立，双方正在通信（本文聚焦状态） |
| `LISTEN` | 服务正在监听端口（如Web服务器等待请求） |
| `TIME_WAIT` | TCP连接关闭后的等待状态（正常现象） |
| `CLOSE_WAIT` | 对端已关闭，本端尚未关闭（需检查应用是否未正确释放连接） |

 

*推荐服务器配置：*

 

|  |  | 硬盘 | 带宽 | IP数 | 月付 |
|---|---|---|---|---|---|
| Xeon E3 | 16G | 1TB HD | 1G 50T流量 | 1+8*/27(可用233个) | 1180 |
| E5-2683v4 | 64G | 1TB HD | 1G 50T流量 | 1+8*/27(可用233个) | 1860 |
| Xeon E3-1230 | 16G | 1TB SATA | 1G/20T流量 | 5+1C(可用258个) | 1199 |

  

租用服务器，详细咨询QQ：80496086  
了解更多服务器及资讯，请关注梦飞科技官方网站 https://www.mfisp.com/，感谢您的支持！

 

![](https://www.mfisp.com/wp-content/uploads/thumb/2021/10/fill_w120_h120_g0_mark_2021101402074182.jpg) 

[![](https://www.mfisp.com/wp-content/uploads/thumb/2021/10/fill_w120_h120_g0_mark_2021101402074182.jpg)](https://www.mfisp.com/shop/243.html)

## [香港超微服务器-HKSIE31230A](https://www.mfisp.com/shop/243.html)[出售]

 

￥1100

 

￥1299

 
 
- 库存：9.9k 
- 已售：45 
- 人气：186

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
