# Linux系统运维

# 在 Ubuntu 20.04 上为超大数据盘分区

在 Ubuntu 20.04 上为超大数据盘（如 20TB 或更大）分区时，建议使用 gdisk 或 parted 而不是 fdisk，因为 fdisk 对于超过 2TB 的磁盘可能存在限制。下面是使用 gdisk 和 parted 的详细步骤：  
**使用 gdisk 分区步骤 1：安装 gdisk**如果还没有安装 gdisk，先进行安装：  
sh复制代码  
  
sudo apt updatesudo apt install gdisk  
  
**步骤 2：列出磁盘**列出所有磁盘以确认目标磁盘设备（假设为 /dev/sdb）：  
sh复制代码  
  
sudo fdisk -l  
  
**步骤 3：使用 gdisk 创建新分区**使用 gdisk 分区工具：  
sh复制代码  
  
sudo gdisk /dev/sdb  
  
在 gdisk 交互界面中执行以下步骤：

- 输入 n 创建新分区。
- 接受默认的分区编号（通常是 1）。
- 接受默认的第一个扇区。
- 接受默认的最后一个扇区以使用整个磁盘空间。
- 输入 w 写入分区表并退出。

**使用 parted 分区步骤 1：安装 parted**如果还没有安装 parted，先进行安装：  
sh复制代码  
  
sudo apt updatesudo apt install parted  
  
**步骤 2：列出磁盘**列出所有磁盘以确认目标磁盘设备（假设为 /dev/sdb）：  
sh复制代码  
  
sudo parted -l  
  
**步骤 3：使用 parted 创建新分区**使用 parted 分区工具：  
sh复制代码  
  
sudo parted /dev/sdb  
  
在 parted 交互界面中执行以下步骤：

- 输入 mklabel gpt 创建新的 GPT 分区表。
- 输入 mkpart primary ext4 0% 100% 创建一个主分区，使用整个磁盘。
- 输入 quit 退出 parted。

**步骤 4：格式化分区**使用 mkfs 命令格式化新创建的分区（假设分区是 /dev/sdb1）：  
sh复制代码  
  
sudo mkfs.ext4 /dev/sdb1  
  
**步骤 5：创建挂载点**创建一个目录作为挂载点，例如 /mnt/data：  
sh复制代码  
  
sudo mkdir /mnt/data  
  
**步骤 6：挂载分区**挂载新创建的分区到 /mnt/data：  
sh复制代码  
  
sudo mount /dev/sdb1 /mnt/data  
  
**步骤 7：自动挂载设置（可选）**为了在系统重启后自动挂载分区，需要编辑 /etc/fstab 文件。首先获取新分区的 UUID：  
sh复制代码  
  
sudo blkid /dev/sdb1  
  
然后编辑 /etc/fstab 文件：  
sh复制代码  
  
sudo nano /etc/fstab  
  
添加以下内容（使用你实际的 UUID）：  
sh复制代码  
  
UUID=你的UUID /mnt/data ext4 defaults 0 2  
  
保存并退出编辑器。  
**检查挂载**最后，验证挂载是否正确：  
sh复制代码  
  
df -h  
  
通过这些步骤，你应该能够在 Ubuntu 20.04 上成功地为超大数据盘分区、格式化并挂载。

# 自动重启sz_driver脚本

\#!/bin/bash

\#########################################################################  
\# 使用方法:  
\# chmod +x /home/shuncom/services/scripts/sz\_driver\_mon.sh  
\# 00 03 \* \* 05 /bin/bash /home/shuncom/services/scripts/sz\_driver\_mon.sh  
\#########################################################################

sleep 30

set -i  
source ~/.bashrc

\# 当前时间  
current\_time="`date +%Y%m%d%H%M%S`"

\# sz\_driver的pid  
sz\_driver\_pid="`pidof sz\_driver`"

\# 日志文件路径  
sz\_driver\_mon\_log="/home/shuncom/services/log/sz\_server/${current\_time}-sz\_driver-mon.log"

\# 判断sz\_driver是否启动，没有启动时退出脚本并写入日志  
if \[ -z "${sz\_driver\_pid}" \]; then  
 echo -e "${current\_time} sz\_driver not start" &gt;&gt; ${sz\_driver\_mon\_log}  
 exit 1  
fi

\# 重启驱动  
/bin/sh /home/shuncom/services/scripts/driver stop &gt;/dev/null 2&gt;&amp;1  
sleep 10  
/bin/sh /home/shuncom/services/scripts/driver start &gt;/dev/null 2&gt;&amp;1  
sleep 10  
/bin/sh /home/shuncom/services/scripts/driver status | grep 'is running' &gt;/dev/null 2&gt;&amp;1  
\# 如果重启失败，退出脚本，写入日志  
if \[ $? -ne 0 \]; then  
 echo -e "${current\_time} sz\_driver restart failed" &gt;&gt; ${sz\_driver\_mon\_log}  
 exit 1  
fi  
\# 如果重启成功，退出脚本，写入日志  
echo -e "${current\_time} sz\_driver restart success" &gt;&gt; ${sz\_driver\_mon\_log}  
exit 1

# 每天21点自动统计tcp连接数脚本

\#!/bin/bash

\#sudo chmod +x /home/shuncom/net\_conn\_count.sh  
\#crontab -e | 0 21 \* \* \* /home/shuncom/net\_conn\_count.sh

\# 获取当前时间  
timestamp=$(date +"%Y-%m-%d %H:%M:%S")

\# 获取 TCP 6011 端口的连接数  
conn\_count=$(netstat -an | grep ':6011' | grep -i tcp | wc -l)

\# 获取网卡 eth0 的接收和发送数据包数  
\# 第3列为接收包，第11列为发送包  
read rx\_packets tx\_packets &lt;&lt;&lt; $(cat /proc/net/dev | awk '/eth0:/ {gsub(/:/,""); print $3, $11}')

\# 写入日志文件  
echo "$timestamp TCP 6011连接数: $conn\_count, eth0接收包: $rx\_packets, 发送包: $tx\_packets" &gt;&gt; /home/shuncom/net\_conn\_count.log

[![image.png](https://bost.lesrec.cn/uploads/images/gallery/2025-05/scaled-1680-/c0Eimage.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-05/c0Eimage.png)

# 自动清除15天前的日志脚本

\#!/bin/bash

\#0 1 \* \* \* /home/shuncom/auto\_del\_sz\_log.sh

\# 设置变量  
target\_dir="/home/shuncom/services/log/sz\_server"  
log\_file="/home/shuncom/log/del\_driver\_log.log"  
current\_time="$(date '+%Y-%m-%d %H:%M:%S')"

\# 执行删除操作并记录日志  
echo "\[$current\_time\] Start deleting files older than 15 days in $target\_dir" &gt;&gt; "$log\_file"

find "$target\_dir" -type f -mtime +15 -print -delete &gt;&gt; "$log\_file" 2&gt;&amp;1

echo "\[$current\_time\] Deletion complete." &gt;&gt; "$log\_file"  
echo "---------------------------------------------" &gt;&gt; "$log\_file"

# check_and_sync.sh

\#!/bin/bash  
\#chmod +x /home/shuncom/bin/check\_and\_sync.sh  
\#30 1 \* \* \* /home/shuncom/bin/check\_and\_sync.sh

\# 检查是否有名为“shun-sync”的进程  
PROCESS\_NAME="shun-sync"  
PROCESS\_ID=$(pgrep -f $PROCESS\_NAME)

\# 如果找到进程，则杀掉该进程  
if \[ ! -z "$PROCESS\_ID" \]; then  
 kill $PROCESS\_ID  
 echo "$(date): Killed process $PROCESS\_NAME with PID $PROCESS\_ID" &gt;&gt; /home/shuncom/log/check\_and\_sync.log  
fi

\# 执行sttsync脚本  
sh /home/shuncom/bin/sttsync  
echo "$(date): Executed sttsync script" &gt;&gt; /home/shuncom/log/check\_and\_sync.log

# ubuntu系统一网卡多IP配置

\#ubuntu18.04配置

network:  
 version: 2  
 ethernets:  
 eth0:  
 dhcp4: no  
 addresses:  
\- 222.222.63.231/24  
\- 192.168.3.11/24  
 gateway4: 192.168.3.1  
 routes:  
\- to: 222.222.63.0/24  
 via: 222.222.63.1  
 nameservers:  
 addresses:  
\- 202.96.209.5  
\- 223.5.5.5

network:  
 ethernets:  
 eth0:  
 addresses:  
\- 172.17.20.17/24  
\- 192.168.1.17/24  
 gateway4: 192.168.1.1  
 routes:  
\- to: 172.17.20.0/24  
 via: 172.17.20.254  
 nameservers:  
 addresses:  
\- 114.114.114.114  
 version: 2

\#ubuntu14.04配置

\# 主IP地址配置（eth0）  
auto eth0  
iface eth0 inet static  
 address 192.168.1.19  
 netmask 255.255.255.0  
 gateway 192.168.1.1  
 dns-nameservers 192.168.0.5

\# 子IP地址配置（eth0:1）  
auto eth0:1  
iface eth0:1 inet static  
 address 10.2.2.19  
 netmask 255.255.255.0  
 gateway 10.2.2.1  
 dns-nameservers 202.96.209.5

ip route add default via 192.168.1.1 dev eth0 metric 100  
ip route add default via 10.2.2.1 dev eth0:1 metric 200

# 麒麟v10查询激活授权

你当前系统中安装了部分与激活相关的组件：  
✅ 已安装组件说明：

kylin-activation-ukey-driver-1.0-1.p01.ky10.x86\_64  
libkylin-activation-3.0-64.p69.ky10.x86\_64

这说明你系统中安装了：

 libkylin-activation：激活相关的库；

 ukey-driver：支持通过 USB Key（UKey）激活的驱动模块。

❌ 但缺失核心工具：

没有安装主控工具 kylin-activation 命令行工具本体，也就是你无法通过命令 kylin-activation status 检查激活状态的根本原因。  
✅ 解决方案

你需要安装完整的 kylin-activation 工具包。请尝试以下方式之一：  
🔧 方法一：使用 yum 安装主工具包

sudo yum install kylin-activation

 如果提示找不到软件包，很可能是你的系统未配置银河麒麟官方的商业源。你可以尝试添加银河麒麟官方源（需要授权账户）或使用离线 RPM 包。

🔧 方法二：查找并安装 RPM 包（适用于无网络环境）

如果你有镜像安装介质或离线软件包仓库，可以手动安装：

cd /path/to/rpm-packages/  
sudo rpm -ivh kylin-activation-\*.rpm

🔧 方法三：使用 GUI 检查激活状态（如果有图形界面）

在图形界面中进入：

“控制中心” → “系统信息” 或 “关于本机”

部分版本会显示激活状态。  
✅ 临时替代检测方法

你可以尝试查看系统日志中是否有激活信息：

grep -i activation /var/log/messages

也可以查看授权库中是否有激活文件：

银河麒麟 V10 自带激活工具，通常命令如下：

sudo kylin-activation status

如果命令存在，会返回类似以下内容：

 ✅ 已激活状态：

当前系统已激活  
激活码：XXXX-XXXX-XXXX-XXXX  
激活时间：2024-10-01  
激活类型：永久授权

❌ 未激活状态：

当前系统未激活  
请联系银河麒麟客服获取激活码

如果你无法安装 GUI，但又想临时运行这个工具，可以安装 xvfb（虚拟帧缓冲）模拟图形界面运行：

sudo yum install -y xorg-x11-server-Xvfb  
xvfb-run kylin-activation status

# Docker容器的导入和导出

导出指定docker镜像  
docker save -o xampp\_7.tar ef5954c4d0d1  
恢复上面导出的镜像  
docker load -i xampp\_7.tar  
镜像改名  
docker tag c1b6ba59135e amazoncorretto:8u352-alpine3.16-tini

修改镜像版本  
docker commit &lt;container\_name\_or\_id&gt; &lt;new\_image\_name&gt;:&lt;tag&gt;

1\. 停止容器

docker-compose down  
2\. 导出镜像  
导出 docker-compose 中定义的所有镜像：

docker-compose config | awk '/image:/{print $2}' | xargs -I {} docker save -o {}.tar {}

这将为 docker-compose.yml 中定义的所有镜像创建 .tar 文件。

3\. 导出数据卷（如果有使用数据卷）  
如果容器使用了数据卷并且你想备份这些数据卷，可以使用以下命令：

首先，列出容器使用的数据卷：

docker volume ls

然后，导出特定的数据卷（例如 my\_volume）：

docker run --rm -v my\_volume:/volume -v $(pwd):/backup busybox tar czf /backup/my\_volume.tar.gz -C /volume .  
4\. 打包为 tar.gz  
将导出的镜像和数据卷一起打包为 tar.gz 文件：

tar czf docker\_backup.tar.gz \*.tar \*.tar.gz  
拷贝文件到目标服务器  
将 docker\_backup.tar.gz 文件拷贝到目标服务器，可以使用 scp 或其他文件传输工具。

scp docker\_backup.tar.gz user@target\_server:/path/to/destination

在目标服务器上恢复

1\. 解压 tar.gz 文件  
在目标服务器上解压 docker\_backup.tar.gz 文件：

tar xzf docker\_backup.tar.gz

2\. 加载镜像  
加载导出的Docker镜像：

for image in \*.tar; do docker load -i $image; done

docker load -i mongodb:24.8.9.1.tar &amp;&amp;\\  
docker load -i mysql:24.8.2.tar &amp;&amp;\\  
docker load -i rabbitmq:24.8.2.tar &amp;&amp;\\  
docker load -i redis:3.2.2.tar &amp;&amp;\\  
docker load -i szdriver:24.8.9.7.tar &amp;&amp;\\  
docker load -i javaapp:24.8.9.2.tar

  
3\. 还原数据卷（如果有备份数据卷）

还原数据卷：  
docker run --rm -v my\_volume:/volume -v $(pwd):/backup busybox tar xzf /backup/my\_volume.tar.gz -C /volume

4\. 启动容器  
确保 docker-compose.yml 文件已复制到目标服务器，然后启动容器：

docker-compose up -d

# 创建容器四代网关协议代理适配器rulr-v4gw-adapter

1，线上平台Rulr服务\_New\_5 (121.199.1.127) 导出镜像  
docker stop rulr-v4gw-adapter  
docker save -o amazoncorretto.tar c1b6ba59135e  
docker start rulr-v4gw-adapter  
scp -P 53022 amazoncorretto.tar shuncomlog@shuncom.3322.org:/home/shuncomlog/

2，当前平台导入镜像  
scp -P 53022 shuncomlog@shuncom.3322.org:/home/shuncomlog/amazoncorretto.tar ./  
docker load -i amazoncorretto.tar  
docker images #查看镜像的id  
docker tag &lt;镜像id&gt; amazoncorretto:8u352-alpine3.16-tini

cat &gt;&gt; /home/shuncom/compose/.env &lt;&lt; EOF  
JAVA\_IMAGE=amazoncorretto:8u352-alpine3.16-tini  
EOF

vi /home/shuncom/compose/docker-compose.yml  
:set paste #粘贴文本不会乱格式

 # 四代网关协议代理适配器  
 rulr-v4gw-adapter:  
 container\_name: rulr-v4gw-adapter  
 image: ${JAVA\_IMAGE}  
 volumes:  
\- /home/shuncom/servers/v4gw-adapter:/rulr/app  
\- /home/shuncom/config/servers:/home/shuncom/config/servers  
\- /home/shuncom/log:/home/shuncom/log  
 ports:  
\- "9011:9011"  
 working\_dir: /rulr/app  
 command: java -Xms256m -Xmx512m -XX:MaxDirectMemorySize=128m -jar v4gw-adapter.jar --appMode=2  
 restart: always  
 networks:  
\- rulr-network  
 deploy:  
 resources:  
 limits:  
 cpus: '1.4'  
 memory: 1024M

cd /home/shuncom/compose  
docker-compose up -d

# 解决Ubuntu服务器时间与本地时间不一致问题

在使用Ubuntu服务器时，常常会遇到系统时间与本地时间不一致的问题。这种时间差异可能导致日志时间错乱、调度任务执行异常等问题。本文将详细介绍如何在Ubuntu服务器中调整时间，使其与本地时间一致。

一、检查当前系统时间和时区  
首先，您需要了解当前系统时间和时区设置。  
查看当前系统时间  
使用以下命令查看当前系统时间：  
date​  
解释：  
date：显示当前的系统日期和时间。  
查看当前时区设置  
通过以下命令查看当前时区：  
timedatectl​  
解释：  
timedatectl：显示系统时间和时区的详细信息，包括当前时间、UTC时间、本地时间以及时区。  
输出示例：  
Local time: Mon 2024-09-02 15:04:23 CSTUniversal time: Mon 2024-09-02 07:04:23 UTCRTC time: Mon 2024-09-02 07:04:23Time zone: Asia/Shanghai (CST, +0800)​  
解释：  
Local time：本地时间，即系统当前使用的时间。  
Universal time：协调世界时（UTC）。  
RTC time：硬件时钟时间。  
Time zone：当前设置的时区。

  
二、设置正确的时区  
若发现时区设置不正确，可以通过以下步骤调整时区。  
列出可用时区  
使用以下命令查看所有可用的时区：  
timedatectl list-timezones​  
解释：  
list-timezones：列出系统中所有可用的时区。  
设置正确的时区  
假设要将时区设置为 Asia/Shanghai（中国标准时间），可以使用以下命令：  
sudo timedatectl set-timezone Asia/Shanghai​  
解释：  
set-timezone Asia/Shanghai：将系统时区设置为 Asia/Shanghai。  
设置完成后，可以再次使用 timedatectl命令确认时区是否正确。

  
三、同步系统时间  
在设置正确时区后，确保系统时间与网络时间服务器同步。  
安装NTP服务  
使用 NTP（网络时间协议）来同步系统时间。首先安装NTP服务：  
sudo apt updatesudo apt install -y ntp​  
解释：  
apt update：更新包列表。  
apt install ntp：安装NTP服务。  
启动并启用NTP服务  
安装完成后，启动NTP服务并设置为开机自启：  
sudo systemctl start ntpsudo systemctl enable ntp​  
解释：  
systemctl start ntp：启动NTP服务。  
systemctl enable ntp：设置NTP服务为开机自启。  
检查NTP同步状态  
可以通过以下命令检查NTP的同步状态：  
ntpq -p​  
解释：  
ntpq -p：显示NTP服务器的状态和同步情况。  
输出示例：  
 remote refid st t when poll reach delay offset jitter==============================================================================\*time.google.com .GOOG. 1 u 42 64 377 24.7 -0.5 0.004​  
解释：  
remote：远程NTP服务器的地址。  
refid：参考ID。  
delay：与NTP服务器的通信延迟。  
offset：本地时间与NTP服务器的时间差。  
jitter：时间波动的程度。  
带 \*的行表示当前同步的NTP服务器。

  
四、调整硬件时钟  
硬件时钟（RTC）可能与系统时间不同步，需要进行校准。  
将系统时间写入硬件时钟  
使用以下命令将系统时间写入硬件时钟：  
sudo hwclock --systohc​  
解释：  
hwclock --systohc：将系统时间同步到硬件时钟。  
设置硬件时钟为UTC时间  
通常，硬件时钟设置为UTC时间是最佳实践，这样可以避免跨时区调整带来的问题：  
sudo timedatectl set-local-rtc 0​  
解释：  
set-local-rtc 0：将硬件时钟设置为UTC时间。

  
五、验证时间同步状态  
完成所有配置后，重新检查时间和时区设置，确保所有设置正确：  
重新检查系统时间和时区  
timedatectl​  
验证NTP服务是否正常运行  
sudo systemctl status ntp​  
解释：  
systemctl status ntp：查看NTP服务的运行状态，确保其正常运行。

  
原理与分析说明表  
步骤  
原理说明

查看当前时间和时区通过 timedatectl命令检查当前系统时间、UTC时间、硬件时钟时间和时区设置。  
设置正确时区通过 timedatectl设置系统时区，使系统时间符合本地时区标准。  
同步系统时间使用NTP服务同步系统时间，确保服务器时间准确无误。  
调整硬件时钟将系统时间同步到硬件时钟，避免重启后时间不同步的问题。  
验证同步状态确认NTP服务和时间同步状态，确保服务器时间与本地时间一致。

总结  
通过本文的详细讲解，您可以确保Ubuntu服务器的时间与本地时间保持一致。首先检查并设置正确的时区，然后通过NTP服务同步系统时间，最后调整硬件时钟以确保时间同步无误。这些步骤能够有效解决由于时区或时间设置错误导致的服务器时间与本地时间不一致的问题，确保系统的稳定性和准确性。

# ubuntu18.04 dns异常修复

明白 ✅。在 **Ubuntu 18.04** 中，DNS 异常问题比较常见，原因通常出在 **systemd-resolved**、配置文件错误、或者本地缓存问题。下面我给你一个 **完整的排查 + 修复步骤**：

---

## 🔎 常见问题表现

- `ping www.baidu.com` 报错：`Temporary failure in name resolution`
- 能 ping 通 IP，但不能解析域名
- `/etc/resolv.conf` 内容异常（指向 `127.0.0.53` 但没解析）

---

## 🛠 修复步骤

### 1. 检查 systemd-resolved 服务

```bash
systemctl status systemd-resolved

```

如果未运行：

```bash
sudo systemctl enable systemd-resolved --now

```

---

### 2. 检查 resolv.conf

Ubuntu 18.04 默认 `/etc/resolv.conf` 是指向 `/run/systemd/resolve/stub-resolv.conf` 的符号链接。  
先确认：

```bash
ls -l /etc/resolv.conf

```

如果不是符号链接，可能被修改了，可以恢复：

```bash
sudo rm -f /etc/resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

```

---

### 3. 手动设置 DNS 服务器

修改 systemd-resolved 配置：

```bash
sudo nano /etc/systemd/resolved.conf

```

添加或修改以下内容（推荐使用 Google + Cloudflare + AliDNS）：

```ini
[Resolve]
DNS=8.8.8.8 1.1.1.1 223.5.5.5
FallbackDNS=8.8.4.4 1.0.0.1 114.114.114.114

```

保存后重启服务：

```bash
sudo systemctl restart systemd-resolved

```

---

### 4. 如果你用 Netplan（18.04 默认网络管理）

编辑配置文件：

```bash
sudo nano /etc/netplan/01-netcfg.yaml

```

示例配置：

```yaml
network:
  version: 2
  ethernets:
    ens33:   # 替换为你的网卡名（ip a 可查）
      dhcp4: yes
      nameservers:
        addresses: [8.8.8.8, 223.5.5.5]

```

应用配置：

```bash
sudo netplan apply

```

---

### 5. 清理 DNS 缓存

```bash
sudo systemd-resolve --flush-caches

```

然后测试：

```bash
dig www.baidu.com
nslookup www.google.com

```

---

## 🚑 快速一键修复脚本

如果想要简单点，可以用下面脚本：

```bash
#!/bin/bash
echo ">>> 修复 Ubuntu 18.04 DNS 异常中..."

# 1. 确保 systemd-resolved 运行
systemctl enable systemd-resolved --now

# 2. 恢复 resolv.conf 符号链接
rm -f /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

# 3. 修改 systemd-resolved 配置
cat >/etc/systemd/resolved.conf <<EOF
[Resolve]
DNS=8.8.8.8 1.1.1.1 223.5.5.5
FallbackDNS=8.8.4.4 1.0.0.1 114.114.114.114
EOF

# 4. 重启服务
systemctl restart systemd-resolved

# 5. 刷新缓存
systemd-resolve --flush-caches

echo ">>> 修复完成，请测试网络连接"

```

保存为 `fix-dns.sh`，执行：

```bash
chmod +x fix-dns.sh
sudo ./fix-dns.sh

```

---

要不要我帮你写一个 **检测并自动修复 Ubuntu18.04 DNS 的脚本**（比如检测解析是否失败，如果失败就自动重置配置）？

# docker-compose 导出导入容器教程

<div id="bkmrk-%E5%9C%A8ubuntu-23.04%E4%B8%8A%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%A0%E4%BD%BF%E7%94%A8">在Ubuntu 23.04上，如果你使用 docker-compose 创建了容器，并且希望将其导出并拷贝到另一台服务器上进行恢复，可以按照以下步骤进行操作：</div><div id="bkmrk-">  
</div><div id="bkmrk-%E5%9C%A8%E6%BA%90%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E5%AF%BC%E5%87%BA%E5%AE%B9%E5%99%A8%E5%92%8C%E6%95%B0%E6%8D%AE">在源服务器上导出容器和数据</div><div id="bkmrk-1.-%E5%81%9C%E6%AD%A2%E5%AE%B9%E5%99%A8">1. 停止容器</div><div id="bkmrk-%E9%A6%96%E5%85%88%EF%BC%8C%E5%81%9C%E6%AD%A2%E9%80%9A%E8%BF%87-docker-compo">首先，停止通过 docker-compose 创建的容器：</div><div id="bkmrk-docker-compose-down">docker-compose down</div><div id="bkmrk--1">  
</div><div id="bkmrk--2">  
</div><div id="bkmrk-2.-%E5%AF%BC%E5%87%BA%E9%95%9C%E5%83%8F">2. 导出镜像</div><div id="bkmrk-%E5%AF%BC%E5%87%BA-docker-compose-%E4%B8%AD%E5%AE%9A">导出 docker-compose 中定义的所有镜像：</div><div id="bkmrk--3">  
</div><div id="bkmrk-docker-compose-confi">docker-compose config | awk '/image:/{print $2}' | xargs -I {} docker save -o {}.tar {}</div><div id="bkmrk--4">  
</div><div id="bkmrk-%E8%BF%99%E5%B0%86%E4%B8%BA-docker-compose.y">这将为 docker-compose.yml 中定义的所有镜像创建 .tar 文件。</div><div id="bkmrk--5">  
</div><div id="bkmrk-3.-%E5%AF%BC%E5%87%BA%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%88%E5%A6%82%E6%9E%9C%E6%9C%89%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%89">3. 导出数据卷（如果有使用数据卷）</div><div id="bkmrk-%E5%A6%82%E6%9E%9C%E5%AE%B9%E5%99%A8%E4%BD%BF%E7%94%A8%E4%BA%86%E6%95%B0%E6%8D%AE%E5%8D%B7%E5%B9%B6%E4%B8%94%E4%BD%A0%E6%83%B3%E5%A4%87%E4%BB%BD%E8%BF%99%E4%BA%9B%E6%95%B0%E6%8D%AE">如果容器使用了数据卷并且你想备份这些数据卷，可以使用以下命令：</div><div id="bkmrk-%E9%A6%96%E5%85%88%EF%BC%8C%E5%88%97%E5%87%BA%E5%AE%B9%E5%99%A8%E4%BD%BF%E7%94%A8%E7%9A%84%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%9A">首先，列出容器使用的数据卷：</div><div id="bkmrk--6">  
</div><div id="bkmrk--7">  
</div><div id="bkmrk-docker-volume-ls">docker volume ls</div><div id="bkmrk--8">  
</div><div id="bkmrk-%E7%84%B6%E5%90%8E%EF%BC%8C%E5%AF%BC%E5%87%BA%E7%89%B9%E5%AE%9A%E7%9A%84%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%88%E4%BE%8B%E5%A6%82-my_vo">然后，导出特定的数据卷（例如 my_volume）：</div><div id="bkmrk-docker-run---rm--v-m">docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar czf /backup/my_volume.tar.gz -C /volume .</div><div id="bkmrk--9">  
</div><div id="bkmrk--10">  
</div><div id="bkmrk-4.-%E6%89%93%E5%8C%85%E4%B8%BA-tar.gz">4. 打包为 tar.gz</div><div id="bkmrk-%E5%B0%86%E5%AF%BC%E5%87%BA%E7%9A%84%E9%95%9C%E5%83%8F%E5%92%8C%E6%95%B0%E6%8D%AE%E5%8D%B7%E4%B8%80%E8%B5%B7%E6%89%93%E5%8C%85%E4%B8%BA-tar.">将导出的镜像和数据卷一起打包为 tar.gz 文件：</div><div id="bkmrk--11">  
</div><div id="bkmrk-tar-czf-docker_backu">tar czf docker_backup.tar.gz *.tar *.tar.gz</div><div id="bkmrk-%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6%E5%88%B0%E7%9B%AE%E6%A0%87%E6%9C%8D%E5%8A%A1%E5%99%A8">拷贝文件到目标服务器</div><div id="bkmrk-%E5%B0%86-docker_backup.tar.">将 docker_backup.tar.gz 文件拷贝到目标服务器，可以使用 scp 或其他文件传输工具。</div><div id="bkmrk--12">  
</div><div id="bkmrk-scp-docker_backup.ta">scp docker_backup.tar.gz user@target_server:/path/to/destination</div><div id="bkmrk--13">  
</div><div id="bkmrk-%E5%9C%A8%E7%9B%AE%E6%A0%87%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E6%81%A2%E5%A4%8D">在目标服务器上恢复</div><div id="bkmrk--14">  
</div><div id="bkmrk-1.-%E8%A7%A3%E5%8E%8B-tar.gz-%E6%96%87%E4%BB%B6">1. 解压 tar.gz 文件</div><div id="bkmrk-%E5%9C%A8%E7%9B%AE%E6%A0%87%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E8%A7%A3%E5%8E%8B-docker_bac">在目标服务器上解压 docker_backup.tar.gz 文件：</div><div id="bkmrk--15">  
</div><div id="bkmrk-tar-xzf-docker_backu">tar xzf docker_backup.tar.gz</div><div id="bkmrk--16">  
</div><div id="bkmrk-2.-%E5%8A%A0%E8%BD%BD%E9%95%9C%E5%83%8F">2. 加载镜像</div><div id="bkmrk-%E5%8A%A0%E8%BD%BD%E5%AF%BC%E5%87%BA%E7%9A%84docker%E9%95%9C%E5%83%8F%EF%BC%9A">加载导出的Docker镜像：</div><div id="bkmrk--17">  
</div><div id="bkmrk-for-image-in-%2A.tar%3B-">for image in *.tar; do docker load -i $image; done</div><div id="bkmrk--18">  
</div><div id="bkmrk-3.-%E8%BF%98%E5%8E%9F%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%88%E5%A6%82%E6%9E%9C%E6%9C%89%E5%A4%87%E4%BB%BD%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%89">3. 还原数据卷（如果有备份数据卷）</div><div id="bkmrk--19">  
</div><div id="bkmrk-%E8%BF%98%E5%8E%9F%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%9A">还原数据卷：</div><div id="bkmrk-docker-run---rm--v-m-1">docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar xzf /backup/my_volume.tar.gz -C /volume</div><div id="bkmrk--20">  
</div><div id="bkmrk-4.-%E5%90%AF%E5%8A%A8%E5%AE%B9%E5%99%A8">4. 启动容器</div><div id="bkmrk-%E7%A1%AE%E4%BF%9D-docker-compose.ym">确保 docker-compose.yml 文件已复制到目标服务器，然后启动容器：</div><div id="bkmrk--21">  
</div><div id="bkmrk-docker-compose-up--d">docker-compose up -d</div><div id="bkmrk--22">  
</div><div id="bkmrk-%E7%A4%BA%E4%BE%8B%E6%93%8D%E4%BD%9C%E6%80%BB%E7%BB%93">示例操作总结</div><div id="bkmrk-%E5%9C%A8%E6%BA%90%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%EF%BC%9A">在源服务器上：</div><div id="bkmrk--23">  
</div><div id="bkmrk-%E5%81%9C%E6%AD%A2%E5%AE%B9%E5%99%A8%EF%BC%9A">停止容器：</div><div id="bkmrk--24">  
</div><div id="bkmrk-docker-compose-down-1">docker-compose down</div><div id="bkmrk-%E5%AF%BC%E5%87%BA%E9%95%9C%E5%83%8F%EF%BC%9A">导出镜像：</div><div id="bkmrk--25">  
</div><div id="bkmrk-docker-compose-confi-1">docker-compose config | awk '/image:/{print $2}' | xargs -I {} docker save -o {}.tar {}</div><div id="bkmrk--26">  
</div><div id="bkmrk-docker-run---rm--v-m-2">docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar czf /backup/my_volume.tar.gz -C /volume .</div><div id="bkmrk-%E6%89%93%E5%8C%85%E4%B8%BA-tar.gz%EF%BC%9A">打包为 tar.gz：</div><div id="bkmrk--27">  
</div><div id="bkmrk-tar-czf-docker_backu-1">tar czf docker_backup.tar.gz *.tar *.tar.gz</div><div id="bkmrk-%E4%BC%A0%E8%BE%93%E6%96%87%E4%BB%B6%EF%BC%9A">传输文件：</div><div id="bkmrk--28">  
</div><div id="bkmrk-scp-docker_backup.ta-1">scp docker_backup.tar.gz user@target_server:/path/to/destination</div><div id="bkmrk-%E5%9C%A8%E7%9B%AE%E6%A0%87%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%EF%BC%9A">在目标服务器上：</div><div id="bkmrk--29">  
</div><div id="bkmrk-%E8%A7%A3%E5%8E%8B-tar.gz-%E6%96%87%E4%BB%B6%EF%BC%9A">解压 tar.gz 文件：</div><div id="bkmrk--30">  
</div><div id="bkmrk-tar-xzf-docker_backu-1">tar xzf docker_backup.tar.gz</div><div id="bkmrk-%E5%8A%A0%E8%BD%BD%E9%95%9C%E5%83%8F%EF%BC%9A">加载镜像：</div><div id="bkmrk--31">  
</div><div id="bkmrk-for-image-in-%2A.tar%3B--1">for image in *.tar; do docker load -i $image; done</div><div id="bkmrk-%E8%BF%98%E5%8E%9F%E6%95%B0%E6%8D%AE%E5%8D%B7%EF%BC%88%E5%8F%AF%E9%80%89%EF%BC%89%EF%BC%9A">还原数据卷（可选）：</div><div id="bkmrk--32">  
</div><div id="bkmrk-docker-run---rm--v-m-3">docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar xzf /backup/my_volume.tar.gz -C /volume</div><div id="bkmrk-%E5%90%AF%E5%8A%A8%E5%AE%B9%E5%99%A8%EF%BC%9A">启动容器：</div><div id="bkmrk--33">  
</div><div id="bkmrk-docker-compose-up--d-1">docker-compose up -d</div><div id="bkmrk-%E9%80%9A%E8%BF%87%E8%BF%99%E4%BA%9B%E6%AD%A5%E9%AA%A4%EF%BC%8C%E4%BD%A0%E5%8F%AF%E4%BB%A5%E6%88%90%E5%8A%9F%E5%9C%B0%E5%B0%86-docke">通过这些步骤，你可以成功地将 docker-compose 创建的容器从一台服务器导出并恢复到另一台服务器上</div>

# Docker/DockerHub 国内镜像源/加速列表（6月18日更新-长期维护）

链接地址：https://cloud.tencent.com/developer/article/2485043

### **前言**

本列表自 2024 年维护以来，受到广大开发者热情欢迎，目前以帮助数十万开发者拉取<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">docker</span>镜像。许多曾经常用的国内镜像站（包括各种云服务商和高校镜像站）已经无法使用。因此，本人开始搜索并汇总了目前可用的镜像站和镜像加速地址，并定期测试它们的可用性，公益更新这个列表。

本列表为科研工作者提供 docker 镜像网站，网络不好的同学可以使用镜像，或者推荐给身边有需要的朋友使用这些 docker 镜像。

> 注意：本文仅供学术研究使用。⚠️长期更新，强烈建议收藏！

6 月 18 日确认可用：https://dev.xuanyuan.dev （该源需要登录，速度稳定，支持群晖nas，极空间nas等）

6 月 18 日确认可用：https://docker.xuanyuan.me

docker hub 镜像搜索功能：https://dockers.xuanyuan.me，方便大家搜索正确的 docker tags。

docker 中文配置文档：https://dockerdocs.xuanyuan.me

### **国内主流 Docker Hub 镜像加速站清单**

<div class="mod-content__markdown" id="bkmrk-%E5%9C%B0%E5%9D%80-%E8%BF%90%E8%90%A5%E6%96%B9-%E7%B1%BB%E5%9E%8B-%E8%AF%B4%E6%98%8E-https%3A%2F" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="table-wrapper" style="overflow-wrap: break-word; margin: 12px 0px; padding: 0px; box-sizing: border-box; list-style: inherit; outline: none; position: relative; overflow-x: auto;"><table style="overflow-wrap: break-word; border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; list-style: inherit; width: 996px; margin: 0px;"><thead style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">地址

</th><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">运营方

</th><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">类型

</th><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">说明

</th></tr></thead><tbody style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.xuanyuan.me

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">轩辕镜像免费版

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Cloudflare+境内CDN

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">官网支持搜索镜像、配置简单、有会员解答群、屏蔽违法内容、境内公司运营（非常稳定）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://dev.xuanyuan.dev

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">轩辕镜像会员版

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Cloudflare+境内CDN

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">需要登录，速度稳定，支持爱快，群晖，极空间，威联通 nas，支持搜索镜像、配置简单、有会员解答群、屏蔽违法内容、境内公司运营（非常稳定）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://mirror.ccs.tencentyun.com

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">腾讯云

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">境内CDN

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">仅腾讯云服务器内推荐使用

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://xxx.mirror.aliyuncs.com

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">阿里云

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">境内CDN

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">仅阿里云服务器内推荐使用（https://help.aliyun.com/zh/acr/user-guide/accelerate-the-pulls-of-docker-official-images?spm=a2c4g.11186623.0.i7）

</td></tr></tbody></table>

</div></div></div></div>### **使用教程**

###### **为了加速镜像拉取，使用以下命令设置** **registry mirror**

> 支持系统：<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">Ubuntu</span> 22.04+、<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">Debian</span> 10+、<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">CentOS</span> 8+

<div class="mod-content__markdown" id="bkmrk-%E4%BB%A3%E7%A0%81%E8%AF%AD%E8%A8%80%EF%BC%9Ajavascript-%E4%BB%A3%E7%A0%81%E8%BF%90%E8%A1%8C" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="rno-markdown-code" qct-area="代码块-0-javascript" qct-exposure="" style="overflow-wrap: break-word; margin: 24px 0px; padding: 0px; box-sizing: border-box; list-style: inherit;"><div class="rno-markdown-code-toolbar" style="overflow-wrap: break-word; margin: 0px; padding: 6px 8px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 228, 233); background: rgb(255, 255, 255); display: flex; align-items: center; justify-content: space-between; gap: 8px;"><div class="rno-markdown-code-toolbar-info" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; line-height: 22px; color: rgb(0, 0, 0); font-size: 12px; font-weight: 400; flex: 1 1 0%; max-width: 48%; white-space: nowrap;"><div class="rno-markdown-code-toolbar-item is-type" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; max-width: calc(100% - 96px); overflow: hidden; text-overflow: ellipsis;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; font-weight: 500;">代码语言：</span>javascript</div><div class="rno-markdown-code-toolbar-item is-num" style="overflow-wrap: break-word; margin: 0px; padding: 0px 0px 0px 48px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; font-weight: 500;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">代码</span>运行次数：0</div></div><div class="rno-markdown-code-toolbar-opt" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: flex; align-items: center; gap: 20px;"><div class="rno-markdown-code-toolbar-opt-icon-btn" qct-area="运行" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px;"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">运行</span></div><div class="rno-markdown-code-toolbar-opt-icon-btn is-ai-btn" qct-area="AI代码解释" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px 10px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px; position: relative; overflow: hidden; border-radius: 12px; z-index: 0;"><div class="chat-ui-ai-logo css-2jdwfl" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2; width: 14px; height: 14px;">  
</div><span style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2;">AI代码解释</span></div></div></div><div class="developer-code-block" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative;"><div class="rno-markdown-code-opt-float" qct-area="复制" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 3px 8px; box-sizing: border-box; list-style: inherit; opacity: 0; transform: translateY(2px); position: absolute; right: 12px; top: 12px; z-index: 1; display: flex; align-items: center; gap: 6px; border-radius: 12px; background: rgba(206, 206, 206, 0.6); transition: 0.2s ease-in-out;"><div class="rno-markdown-code-icon-btn qa-r-editor-btn" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; display: inline-flex; vertical-align: middle; border: none; background-color: transparent; cursor: pointer; text-align: center; align-items: center; gap: 6px; flex: 0 0 auto; font-size: 12px; font-weight: 500; line-height: 22px; color: rgb(255, 255, 255);"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor" fill-rule="evenodd"></path></svg></div></div></div></div></div></div></div>```javascript
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
        "https://docker.xuanyuan.me"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
```

###### **使用DockerHub 代理，以下以** docker.xuanyuan.me **为例：可以根据列表自行替换**

<div class="mod-content__markdown" id="bkmrk-%E4%BB%A3%E7%A0%81%E8%AF%AD%E8%A8%80%EF%BC%9Ajavascript-%E4%BB%A3%E7%A0%81%E8%BF%90%E8%A1%8C-1" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="rno-markdown-code" qct-area="代码块-0-javascript" qct-exposure="" style="overflow-wrap: break-word; margin: 24px 0px; padding: 0px; box-sizing: border-box; list-style: inherit;"><div class="rno-markdown-code-toolbar" style="overflow-wrap: break-word; margin: 0px; padding: 6px 8px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 228, 233); background: rgb(255, 255, 255); display: flex; align-items: center; justify-content: space-between; gap: 8px;"><div class="rno-markdown-code-toolbar-info" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; line-height: 22px; color: rgb(0, 0, 0); font-size: 12px; font-weight: 400; flex: 1 1 0%; max-width: 48%; white-space: nowrap;"><div class="rno-markdown-code-toolbar-item is-type" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; max-width: calc(100% - 96px); overflow: hidden; text-overflow: ellipsis;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; font-weight: 500;">代码语言：</span>javascript</div><div class="rno-markdown-code-toolbar-item is-num" style="overflow-wrap: break-word; margin: 0px; padding: 0px 0px 0px 48px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; font-weight: 500;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">代码</span>运行次数：0</div></div><div class="rno-markdown-code-toolbar-opt" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: flex; align-items: center; gap: 20px;"><div class="rno-markdown-code-toolbar-opt-icon-btn" qct-area="运行" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px;"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">运行</span></div><div class="rno-markdown-code-toolbar-opt-icon-btn is-ai-btn" qct-area="AI代码解释" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px 10px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px; position: relative; overflow: hidden; border-radius: 12px; z-index: 0;"><div class="chat-ui-ai-logo css-2jdwfl" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2; width: 14px; height: 14px;">  
</div><span style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2;">AI代码解释</span></div></div></div><div class="developer-code-block" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative;"><div class="rno-markdown-code-opt-float" qct-area="复制" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 3px 8px; box-sizing: border-box; list-style: inherit; opacity: 0; transform: translateY(2px); position: absolute; right: 12px; top: 12px; z-index: 1; display: flex; align-items: center; gap: 6px; border-radius: 12px; background: rgba(206, 206, 206, 0.6); transition: 0.2s ease-in-out;"><div class="rno-markdown-code-icon-btn qa-r-editor-btn" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; display: inline-flex; vertical-align: middle; border: none; background-color: transparent; cursor: pointer; text-align: center; align-items: center; gap: 6px; flex: 0 0 auto; font-size: 12px; font-weight: 500; line-height: 22px; color: rgb(255, 255, 255);"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor" fill-rule="evenodd"></path></svg></div></div></div></div></div></div></div>```javascript
docker pull docker.xuanyuan.me/library/mysql:5.7
```

说明：library是一个特殊的命名空间，它代表的是官方镜像。如果是某个用户的镜像就把library替换为镜像的用户名。

#### **Linux**

运行以下命令进行配置

<div class="mod-content__markdown" id="bkmrk-%E4%BB%A3%E7%A0%81%E8%AF%AD%E8%A8%80%EF%BC%9Ajavascript-%E4%BB%A3%E7%A0%81%E8%BF%90%E8%A1%8C-2" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="rno-markdown-code" qct-area="代码块-0-javascript" qct-exposure="" style="overflow-wrap: break-word; margin: 24px 0px; padding: 0px; box-sizing: border-box; list-style: inherit;"><div class="rno-markdown-code-toolbar" style="overflow-wrap: break-word; margin: 0px; padding: 6px 8px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 228, 233); background: rgb(255, 255, 255); display: flex; align-items: center; justify-content: space-between; gap: 8px;"><div class="rno-markdown-code-toolbar-info" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; line-height: 22px; color: rgb(0, 0, 0); font-size: 12px; font-weight: 400; flex: 1 1 0%; max-width: 48%; white-space: nowrap;"><div class="rno-markdown-code-toolbar-item is-type" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; max-width: calc(100% - 96px); overflow: hidden; text-overflow: ellipsis;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; font-weight: 500;">代码语言：</span>javascript</div><div class="rno-markdown-code-toolbar-item is-num" style="overflow-wrap: break-word; margin: 0px; padding: 0px 0px 0px 48px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; font-weight: 500;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">代码</span>运行次数：0</div></div><div class="rno-markdown-code-toolbar-opt" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: flex; align-items: center; gap: 20px;"><div class="rno-markdown-code-toolbar-opt-icon-btn" qct-area="运行" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px;"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">运行</span></div><div class="rno-markdown-code-toolbar-opt-icon-btn is-ai-btn" qct-area="AI代码解释" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px 10px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px; position: relative; overflow: hidden; border-radius: 12px; z-index: 0;"><div class="chat-ui-ai-logo css-2jdwfl" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2; width: 14px; height: 14px;">  
</div><span style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2;">AI代码解释</span></div></div></div><div class="developer-code-block" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative;"><div class="rno-markdown-code-opt-float" qct-area="复制" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 3px 8px; box-sizing: border-box; list-style: inherit; opacity: 0; transform: translateY(2px); position: absolute; right: 12px; top: 12px; z-index: 1; display: flex; align-items: center; gap: 6px; border-radius: 12px; background: rgba(206, 206, 206, 0.6); transition: 0.2s ease-in-out;"><div class="rno-markdown-code-icon-btn qa-r-editor-btn" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; display: inline-flex; vertical-align: middle; border: none; background-color: transparent; cursor: pointer; text-align: center; align-items: center; gap: 6px; flex: 0 0 auto; font-size: 12px; font-weight: 500; line-height: 22px; color: rgb(255, 255, 255);"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor" fill-rule="evenodd"></path></svg></div></div></div></div></div></div></div>```javascript
#!/bin/sh
cat <<-EOF > /etc/docker/daemon.json 
{
  "registry-mirrors": [
  	"https://docker.xuanyuan.me"
  	]
}
EOF
systemctl daemon-reload
systemctl restart docker
```

这适用于Ubuntu 22.04、Debian、CentOS 8、<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">Fedora</span>、<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">Arch Linux</span>、<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">openSUSE</span> Leap 42.1等系统。

#### **macOS（Docker For Mac）**

对于macOS上的Docker For Mac用户，您可以通过以下步骤配置镜像加速服务：

1、点击桌面顶栏的docker图标，选择Preferences。

2、在 Daemon 标签下的 Registry mirrors 列表中加入以下镜像地址：

<div class="mod-content__markdown" id="bkmrk-%E4%BB%A3%E7%A0%81%E8%AF%AD%E8%A8%80%EF%BC%9Ajavascript-%E4%BB%A3%E7%A0%81%E8%BF%90%E8%A1%8C-3" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="rno-markdown-code" qct-area="代码块-0-javascript" qct-exposure="" style="overflow-wrap: break-word; margin: 24px 0px; padding: 0px; box-sizing: border-box; list-style: inherit;"><div class="rno-markdown-code-toolbar" style="overflow-wrap: break-word; margin: 0px; padding: 6px 8px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 228, 233); background: rgb(255, 255, 255); display: flex; align-items: center; justify-content: space-between; gap: 8px;"><div class="rno-markdown-code-toolbar-info" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; line-height: 22px; color: rgb(0, 0, 0); font-size: 12px; font-weight: 400; flex: 1 1 0%; max-width: 48%; white-space: nowrap;"><div class="rno-markdown-code-toolbar-item is-type" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; max-width: calc(100% - 96px); overflow: hidden; text-overflow: ellipsis;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; font-weight: 500;">代码语言：</span>javascript</div><div class="rno-markdown-code-toolbar-item is-num" style="overflow-wrap: break-word; margin: 0px; padding: 0px 0px 0px 48px; box-sizing: border-box; list-style: inherit; position: relative; white-space: nowrap; align-items: center; display: inline-block; vertical-align: middle; font-weight: 500;"><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">代码</span>运行次数：0</div></div><div class="rno-markdown-code-toolbar-opt" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: flex; align-items: center; gap: 20px;"><div class="rno-markdown-code-toolbar-opt-icon-btn" qct-area="运行" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px;"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg><span class="is-m-hidden" style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;">运行</span></div><div class="rno-markdown-code-toolbar-opt-icon-btn is-ai-btn" qct-area="AI代码解释" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 0px 10px; box-sizing: border-box; list-style: inherit; display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; cursor: pointer; color: rgba(0, 0, 0, 0.9); font-size: 12px; font-weight: 500; line-height: 22px; position: relative; overflow: hidden; border-radius: 12px; z-index: 0;"><div class="chat-ui-ai-logo css-2jdwfl" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2; width: 14px; height: 14px;">  
</div><span style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; position: relative; z-index: 2;">AI代码解释</span></div></div></div><div class="developer-code-block" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative;"><div class="rno-markdown-code-opt-float" qct-area="复制" qct-click="" qct-exposure="" style="overflow-wrap: break-word; margin: 0px; padding: 3px 8px; box-sizing: border-box; list-style: inherit; opacity: 0; transform: translateY(2px); position: absolute; right: 12px; top: 12px; z-index: 1; display: flex; align-items: center; gap: 6px; border-radius: 12px; background: rgba(206, 206, 206, 0.6); transition: 0.2s ease-in-out;"><div class="rno-markdown-code-icon-btn qa-r-editor-btn" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; list-style: inherit; position: relative; display: inline-flex; vertical-align: middle; border: none; background-color: transparent; cursor: pointer; text-align: center; align-items: center; gap: 6px; flex: 0 0 auto; font-size: 12px; font-weight: 500; line-height: 22px; color: rgb(255, 255, 255);"><svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor" fill-rule="evenodd"></path></svg></div></div></div></div></div></div></div>```javascript
https://docker.xuanyuan.me
```

3、点击Apply &amp; Restart按钮使设置生效。

#### **Windows（Docker For Windows）**

<span class="mod-overview__keyword" style="overflow-wrap: break-word; margin: 0px 4px; cursor: pointer; color: rgb(0, 82, 217); padding-right: 10px; background-image: url('https://bost.lesrec.cn/images/icon-keyword_32a.svg'); background-position: right 2px; background-repeat: no-repeat; background-size: 8px 8px; box-shadow: none; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">Windows</span>系统上的Docker For Windows用户可以按照以下步骤配置镜像加速服务：

<div class="mod-content__markdown" id="bkmrk-%E5%9C%A8%E6%A1%8C%E9%9D%A2%E5%8F%B3%E4%B8%8B%E8%A7%92%E7%8A%B6%E6%80%81%E6%A0%8F%E4%B8%AD%E5%8F%B3%E9%94%AEdocker%E5%9B%BE%E6%A0%87" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;">1. 在桌面右下角状态栏中右键docker图标，修改在Docker Daemon标签页中的json。
2. 将以下地址加入"registry-mirrors"的数组里。[`https://docker.xuanyuan.me`](https://cloud.tencent.com/developer/tools/blog-entry?target=https%3A%2F%2Fdocker.xuanyuan.me%2F&objectId=2485043&objectType=1&isNewArticle=undefined)
3. 点击Apply，重新生成Docker环境以使配置生效。

</div></div></div>### **更多可用镜像**

<div class="mod-content__markdown" id="bkmrk-dockerhub%E9%95%9C%E5%83%8F%E4%BB%93%E5%BA%93-%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F%E5%99%A8%E5%9C%B0" style="overflow-wrap: break-word; margin: 0px; padding: 0px; color: rgb(24, 24, 24); font-family: 'Pingfang SC', 'helvetica neuepingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div style="overflow-wrap: break-word; margin: 0px; padding: 0px;"><div class="rno-markdown new-version rno-" style="overflow-wrap: break-word; margin: 0px; padding: 0px; box-sizing: border-box; font-weight: 400; font-size: 14px; line-height: 24px; color: rgb(51, 51, 51); text-size-adjust: none; -webkit-font-smoothing: antialiased; white-space: pre-wrap; font-family: 'pingfang SC', 'helvetica neue', arial, 'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun, sans-serif;"><div class="table-wrapper" style="overflow-wrap: break-word; margin: 12px 0px; padding: 0px; box-sizing: border-box; list-style: inherit; outline: none; position: relative; overflow-x: auto;"><table style="overflow-wrap: break-word; border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; list-style: inherit; width: 996px; margin: 0px;"><thead style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">DockerHub镜像仓库

</th><th style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); background-color: rgb(245, 247, 250); color: rgb(103, 116, 137); text-align: left; font-weight: normal; word-break: keep-all;">镜像加速器地址

</th></tr></thead><tbody style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 657

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://dockerhub.icu（0913 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">hub.rat.dev

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">docker.wanpeng.top

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 250

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://doublezonline.cloud

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 76

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.mrxn.net（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 45

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://lynn520.xyz（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 30

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://ginger20240704.asia（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">DockerHub 镜像加速代理 102

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.anyhub.us.kg（0831测试已不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Dockerhub镜像加速说明 70

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.wget.at（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 38

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.awsl9527.cn（0913 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 19

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://dislabaiot.xyz

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Docker Proxy 镜像加速 92（来源地址 22）

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://dockerpull.com（1102 测试不可用）https://dockerproxy.cn（0913 新增）（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Docker Hub Container Image Library 16

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.fxxk.dedyn.io

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">docker-registry-mirrors 77: 支持 Docker Hub, GitHub, Google, k8s, Quay, Microsoft 等镜像仓库.

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">dhub.kubesre.xyz（0913 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">AtomHub 可信镜像仓库平台 27（只包含基础镜像，共336个）

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://atomhub.openatom.cn

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">DaoCloud 镜像站 90

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">https://docker.m.daocloud.io

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">已失效DockerHub镜像仓库

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Docker镜像加速站 1（因流量太大，作者已关停）

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"><del style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">https://hub.uuuadc.top</del>8（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">Docker镜像加速站

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"><del style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">https://docker.ckyl.me</del>4（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">镜像使用说明 3

</td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"><del style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">https://docker.hpcloud.cloud</del> 4（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;">docker.1panel.live（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"><del style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">https://dockerhub.jobcher.com</del> 5（1102 测试不可用）

</td></tr><tr style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit;"><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"></td><td style="overflow-wrap: break-word; margin: 0px; padding: 12px 16px; box-sizing: border-box; list-style: inherit; border: 1px solid rgb(225, 230, 240); text-align: left;"><del style="overflow-wrap: break-word; box-sizing: border-box; list-style: inherit; white-space: pre-wrap; word-break: break-all;">https://docker.chenby.cn</del> 5（1102 测试不可用）

</td></tr></tbody></table>

</div></div></div></div>此列表只收录无需限定条件的DockerHub镜像源，感谢这些公益服务者。

### **Docker Compose 安装与使用教程请看：**

[Docker Compose 安装及使用教程](https://cloud.tencent.com/developer/article/2454497?from_column=20421&from=20421)

[Dcoker Compose 模板文件详解](https://cloud.tencent.com/developer/article/2454496?from_column=20421&from=20421)

本文系转载，<span style="overflow-wrap: break-word; color: rgb(0, 82, 217); cursor: pointer;">前往查看</span>

# 如何使用 Rsync 克隆一台 Linux 服务器

#### <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block; visibility: visible;">**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #7a4fd6; visibility: visible;">1.环境说明及准备</span></span>**</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">两台Linux服务器，信息如下：</span>

<section data-tool="mdnice编辑器" id="bkmrk-%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%B1%BB%E5%9E%8B-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%89%88%E6%9C%AC-ip%E5%9C%B0%E5%9D%80-%E6%BA%90%E6%9C%8D" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; overflow-x: auto; visibility: visible;"><table width="657"><thead style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><tr class="firstRow" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><th style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(240, 240, 240); max-width: 100%; box-sizing: border-box !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.5em; letter-spacing: 0.04em; font-weight: bold; height: auto; border-radius: 0px; min-width: 85px; text-align: center; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: inline-block; width: 100px; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">服务器类型</span></span></th><th style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(240, 240, 240); max-width: 100%; box-sizing: border-box !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.5em; letter-spacing: 0.04em; text-align: left; font-weight: bold; height: auto; border-radius: 0px; min-width: 85px; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: inline-block; width: 100px; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">操作系统版本</span></span></th><th style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(240, 240, 240); max-width: 100%; box-sizing: border-box !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.5em; letter-spacing: 0.04em; text-align: left; font-weight: bold; height: auto; border-radius: 0px; min-width: 85px; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: inline-block; width: 120px; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">IP地址</span></span></th></tr></thead><tbody style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto; visibility: visible;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: center; visibility: visible;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">源服务器</span></section></td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; visibility: visible;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">RHEL 9</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; visibility: visible;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">192.168.0.150</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto; visibility: visible;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: center; visibility: visible;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">目标服务器</span></section></td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; visibility: visible;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">RHEL 9</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; visibility: visible;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">192.168.0.152</span></section></td></tr></tbody></table>

</section>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">注</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">：</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">源服务器</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">即需要被克隆至目标服务器的原始系统。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">开始操作前，请确保满足以下先决条件：</span>

- <section style="-webkit-tap-highlight-color: transparent; margin: 5px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.8em; letter-spacing: 0.04em; text-align: left; font-weight: normal; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">两台服务器必须运行</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">相同版本</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">的操作系统（例如，均为</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">RHEL 9.x</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">或</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">RHEL 8.x</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">等）</span></section>
- <section style="-webkit-tap-highlight-color: transparent; margin: 5px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.8em; letter-spacing: 0.04em; text-align: left; font-weight: normal; visibility: visible;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">服务器需配置</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">完全一致的文件系统</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">及硬盘结构（例如，均为单盘或相同的RAID阵列）</span></section>

> <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">重要提示</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">：</span>**
> 
> <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">执行热克隆前，请</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">停用</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">所有涉及数据写入或传输的服务（如数据库、邮件服务等）。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">另外，现代Linux系统已经预装了</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">rsync</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">，不需要安装，可以通过运行以下命令来检查安装的</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">rsync</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">版本：</span>

```
$ rsync --version

```

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">或者查看更详细的信息：</span>

```
$ rpm -qi rsync               [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ dpkg-query --status rsync   [On Debian, Ubuntu and Mint]

```

<section id="bkmrk-" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; text-align: center;">![640.webp](https://www.lesrec.cn/static/upload/image/20250623/1750646980876654.webp)</section><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">以上完成了整台服务器克隆前的准备工作，下面开始进行副本克隆。</span>

#### <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #7a4fd6;">2.配置源Linux服务器</span></span>**</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">在克隆过程中，部分目录和文件可能</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">无需同步</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">（例如目标服务器已存在或自动生成的临时文件），包括 </span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">/boot</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">、</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">/tmp</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 和 </span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">/dev</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 等目录。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">因此，首先在在源服务器上创建 </span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">/root/exclude-files.txt</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 文件，并添加以下内容：</span>

```
/boot
/dev
/tmp
/sys
/proc
/backup
/etc/fstab
/etc/mtab
/etc/mdadm.conf
/etc/sysconfig/network*

```

#### <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #7a4fd6;">3.克隆Linux服务器</span></span>**</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">完成上面步骤，使用下面命令将源服务器除排除的文件外将所有文件同步到远程或目标服务器：</span>

```
$ sudo rsync -vPa -e 'ssh -o StrictHostKeyChecking=no' --exclude-from=/root/exclude-files.txt / REMOTE-IP:/

```

<section id="bkmrk--1" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; text-align: center;">![640 (1).webp](https://www.lesrec.cn/static/upload/image/20250623/1750647038161262.webp)</section><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">上面命令使用</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">rsync</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">同步从源服务器到目标服务器的所有内容，同时排除我们之前定义的文件和目录。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">关于这条命令中每项的功能，为便于理解，现以表格形式分别解释，如下</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">命令参数解析</span>**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">：</span>

<section data-tool="mdnice编辑器" id="bkmrk-%E5%8F%82%E6%95%B0%2F%E9%80%89%E9%A1%B9-%E8%AF%B4%E6%98%8E-sudo-%E4%BB%A5%E7%AE%A1%E7%90%86%E5%91%98%E6%9D%83%E9%99%90" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; overflow-x: auto;"><table width="657"><thead style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><tr class="firstRow" style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><th style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(240, 240, 240); max-width: 100%; box-sizing: border-box !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.5em; letter-spacing: 0.04em; font-weight: bold; height: auto; border-radius: 0px; min-width: 85px; text-align: left;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: inline-block; width: 120px;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">参数/选项</span></span></th><th style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(240, 240, 240); max-width: 100%; box-sizing: border-box !important; color: rgb(89, 89, 89); font-size: 15px; line-height: 1.5em; letter-spacing: 0.04em; font-weight: bold; height: auto; border-radius: 0px; min-width: 85px; text-align: left;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: inline-block; width: 220px;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">说明</span></span></th></tr></thead><tbody style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">sudo</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">以管理员权限执行命令</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">rsync</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">文件同步与传输工具</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-v</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">显示详细同步过程（</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">verbose</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">模式）</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-P</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">等同于 </span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">--progress</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">，显示文件传输进度并支持断点续传</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-a</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">归档模式（保留文件权限、所有者、时间戳等属性）</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-e </span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">指定要用于同步的远程 shell</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">-o StrictHostKeyChecking=no</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">禁用严格的主机密钥检查，这意味着如果远程服务器的主机密钥未知，SSH 连接将不会提示确认</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">--exclude-from=/root/exclude-files.txt</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">指定需排除的目录/文件列表（即之前创建的排除规则文件）</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(255, 255, 255); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">/</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">源服务器根目录（同步起点）</span></section></td></tr><tr style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(89, 89, 89); background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(248, 248, 248); width: auto; height: auto;"><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;">`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">REMOTE-IP:/</span>`</td><td style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 5px 10px; outline: 0px; overflow-wrap: break-word !important; word-break: break-all; hyphens: auto; border: 1px solid rgba(204, 204, 204, 0.4); max-width: 100%; box-sizing: border-box !important; min-width: 85px; border-radius: 0px; text-align: left;"><section style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">指定目标服务器的</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">IP</span>`<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">地址或主机名，后跟冒号和正斜杠。它指示将复制克隆文件的远程目录</span></section></td></tr></tbody></table>

</section>#### <span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; font-size: 18px; color: #000000; line-height: 1.5em; letter-spacing: 0em; text-align: left; font-weight: bold; display: block;">**<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #7a4fd6;">4.同步后操作</span></span>**</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">同步完成后，重启目标服务器，使同步变更生效。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">使用源服务器凭证登录，验证克隆结果是否正常和完整。</span>

<span style="-webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">退役旧服务器（可选），此时目标服务器已是完整镜像。</span>

# 死链检测工具推荐（适用于新网站上线前后）

### ✅ 一、死链检测工具推荐（适用于新网站上线前后）

检测网站 **死链（Broken Links）**，可有效提升用户体验与 SEO 表现。以下是几款推荐工具，适合不同场景：

---

### 🛠 工具推荐一览

<table id="bkmrk-%E5%B7%A5%E5%85%B7%E5%90%8D%E7%A7%B0-%E7%B1%BB%E5%9E%8B-%E9%80%82%E7%94%A8%E7%B3%BB%E7%BB%9F-%E7%89%B9%E7%82%B9-%E5%BB%BA%E8%AE%AE%E7%94%A8%E9%80%94"><thead><tr><th>工具名称</th><th>类型</th><th>适用系统</th><th>特点</th><th>建议用途</th></tr></thead><tbody><tr><td>**Screaming Frog SEO Spider**</td><td>桌面软件</td><td>Windows / macOS / Linux</td><td>全面、专业、支持导出报告</td><td>中大型网站、含登录页面</td></tr><tr><td>**Xenu's Link Sleuth**</td><td>桌面软件</td><td>Windows</td><td>老牌轻量，速度快</td><td>小网站或快速检查</td></tr><tr><td>**Sitechecker / Ahrefs / SEMrush**</td><td>在线服务</td><td>Web</td><td>集成 SEO 分析、无需安装</td><td>商业SEO分析</td></tr><tr><td>**W3C Link Checker**</td><td>在线</td><td>Web</td><td>权威、可自定义深度</td><td>单页或小网站</td></tr><tr><td>**Broken Link Checker（WordPress 插件）**</td><td>插件</td><td>WordPress</td><td>实时监控死链</td><td>WordPress 网站必备</td></tr><tr><td>**LinkChecker CLI 工具**</td><td>命令行</td><td>Linux</td><td>脚本化，适合定期任务</td><td>DevOps/自动化</td></tr></tbody></table>

---

### 🔍 推荐命令行工具示例（Linux 用户）：

#### ✅ `linkchecker` （适合 Ubuntu/Debian 用户）

安装：

```bash
sudo apt install linkchecker

```

使用：

```bash
linkchecker https://ssiot.shuncom.com

```

结果会输出死链、跳转、超时等信息。

#### ✅ `broken-link-checker`（Node.js）

```bash
npx broken-link-checker https://ssiot.shuncom.com --recursive --ordered

```

---

### ✅ 二、分析新网站 `ssiot.shuncom.com`

#### 🚀 分析方法：

我将从以下几个维度对这个网站进行基本诊断（如需要，我可以进一步使用在线工具抓取实际内容）：

<table id="bkmrk-%E9%A1%B9%E7%9B%AE-%E7%8A%B6%E6%80%81-%E5%88%86%E6%9E%90%E5%BB%BA%E8%AE%AE-%E2%9C%85-%E5%9F%9F%E5%90%8D%E5%8F%AF%E8%A7%A3%E6%9E%90%E6%80%A7-"><thead><tr><th>项目</th><th>状态</th><th>分析建议</th></tr></thead><tbody><tr><td>✅ **域名可解析性**</td><td>由二级域名 `ssiot.shuncom.com` 构成，基于主域 `shuncom.com`，企业网站。通过公共 DNS 可正常解析</td><td>正常，无需调整</td></tr><tr><td>✅ **是否支持 HTTPS**</td><td>推荐配置 HTTPS + 有效证书</td><td>若未启用，需申请 Let's Encrypt / 商业证书</td></tr><tr><td>⚠️ **死链检查**</td><td>需运行 `linkchecker` 或上述工具确认页面链接、JS/CSS 引用是否 404</td><td>上线前务必全站爬取一遍</td></tr><tr><td>🔒 **安全性检测**</td><td>可通过 [securityheaders.com](https://securityheaders.com/) 或 `curl -I` 检查头部安全策略</td><td>建议开启 `Content-Security-Policy`、`X-Frame-Options` 等头部</td></tr><tr><td>📈 **SEO 结构检测**</td><td>是否包含 sitemap.xml、robots.txt、meta 标签合理</td><td>推荐使用 Google Search Console 提交站点结构</td></tr></tbody></table>

---

### ✅ 示例使用：检测 `ssiot.shuncom.com` 死链命令

```bash
linkchecker https://ssiot.shuncom.com --check-extern --no-warnings

```

说明：

- `--check-extern`：检测外部链接
- `--no-warnings`：忽略警告，仅输出错误
- 支持导出报告，可用于上线验收文档

---

你可以通过以下几种方式将 `linkchecker` 扫描结果生成文本或结构化报告，方便保存、查看或进一步处理。

---

### ✅ 方法一：**重定向标准输出到文本文件**

这是最简单直接的方式：

```bash
linkchecker https://ssiot.shuncom.com --check-extern --no-warnings > link_report.txt

```

- 输出结果会保存到 `link_report.txt`
- 可用任何文本编辑器打开查看（如 `less`, `vim`, 或 Windows 记事本）

---

### ✅ 方法二：**生成 HTML 报告**

使用 `--output=html` 参数，生成可视化报告：

```bash
linkchecker https://ssiot.shuncom.com --check-extern --no-warnings --output=html > link_report.html

```

- 输出为网页格式，打开更直观
- 用浏览器打开 `link_report.html` 即可查看详细死链、状态码、链接来源等信息

---

### ✅ 方法三：**生成 CSV 报告（适合导入 Excel）**

```bash
linkchecker https://ssiot.shuncom.com --check-extern --no-warnings --output=csv > link_report.csv

```

- 可用 Excel 或表格工具分析死链
- 包括字段：状态码、链接类型、来源页面等

---

### ✅ 方法四：**结构化 JSON 报告（适合自动化分析）**

```bash
linkchecker https://ssiot.shuncom.com --check-extern --no-warnings --output=json > link_report.json

```

- 可配合脚本进一步解析结果，适合 DevOps 流程集成

---

### ✅ 补充说明（可选参数）

<table id="bkmrk-%E5%8F%82%E6%95%B0-%E5%90%AB%E4%B9%89---no-status-%E4%B8%8D%E8%BE%93"><thead><tr><th>参数</th><th>含义</th></tr></thead><tbody><tr><td>`--no-status`</td><td>不输出中间扫描状态，结果更干净</td></tr><tr><td>`--recursion-level=N`</td><td>限制递归层数，避免陷入无限循环</td></tr><tr><td>`--file-output`</td><td>将不同格式同时输出成文件（不通过重定向）</td></tr></tbody></table>

例如：

```bash
linkchecker https://ssiot.shuncom.com --check-extern --file-output=html

```

会在当前目录生成 `linkchecker-out.html`

---

# Ubuntu 24.04 + OpenVPN + AD (LDAP/RADIUS) 集成部署教程

---

# 🛠 Ubuntu 24.04 + OpenVPN + AD 域账号集成 部署教程

## 一、环境准备

- **Ubuntu 24.04 LTS** 服务器一台（公网可访问，或 NAT 转发端口）。
- **Windows AD 域控制器**（域名如：`shuncom.local`，DC IP：192.168.1.10）。
- 客户端：Windows 10/11，macOS，Linux，移动端。

---

## 二、安装 OpenVPN 和 Easy-RSA

```bash
sudo apt update
sudo apt install openvpn easy-rsa -y

```

---

## 三、生成证书

1. 创建证书目录：

```bash
make-cadir ~/openvpn-ca
cd ~/openvpn-ca

```

2. 初始化：

```bash
./easyrsa init-pki
./easyrsa build-ca

```

3. 生成服务器证书：

```bash
./easyrsa build-server-full server nopass

```

4. 生成客户端证书：

```bash
./easyrsa build-client-full user01 nopass

```

5. 拷贝到 OpenVPN 配置目录：

```bash
sudo cp pki/ca.crt pki/private/server.key pki/issued/server.crt /etc/openvpn/

```

---

## 四、配置 OpenVPN 服务端

创建配置文件 `/etc/openvpn/server.conf`：

```conf
port 1194
proto udp
dev tun

ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key
dh none
auth SHA256
tls-crypt /etc/openvpn/tls-crypt.key

server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt

push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 192.168.1.10"   # AD DNS

keepalive 10 120
cipher AES-256-GCM
persist-key
persist-tun
user nobody
group nogroup
verb 3

# 启用 LDAP 插件
plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so openvpn
client-cert-not-required
username-as-common-name

```

---

## 五、配置 PAM → LDAP (AD)

安装 LDAP 认证模块：

```bash
sudo apt install libpam-ldapd nslcd -y

```

配置 `/etc/nslcd.conf`：

```conf
uri ldap://192.168.1.10/
base dc=shuncom,dc=local
binddn cn=vpnbind,ou=ServiceAccounts,dc=shuncom,dc=local
bindpw YourPassword
scope sub
filter passwd (sAMAccountName=%u)

```

> 📌 建议在 AD 中创建一个只读账号 `vpnbind` 专门用于 LDAP 查询。

修改 PAM 配置 `/etc/pam.d/openvpn`：

```conf
auth    required        pam_unix.so
auth    sufficient      pam_ldap.so use_first_pass
account sufficient      pam_ldap.so

```

---

## 六、启用并启动 OpenVPN

```bash
sudo systemctl enable openvpn@server
sudo systemctl start openvpn@server
sudo systemctl status openvpn@server

```

---

## 七、防火墙 &amp; NAT

```bash
sudo ufw allow 1194/udp
sudo ufw allow OpenSSH

```

启用 NAT 转发：  
编辑 `/etc/sysctl.conf`，取消注释：

```conf
net.ipv4.ip_forward=1

```

生效：

```bash
sudo sysctl -p

```

配置 NAT：

```bash
sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

```

---

## 八、客户端配置

生成客户端配置 `user01.ovpn`：

```conf
client
dev tun
proto udp
remote vpn.shuncom.org 1194
resolv-retry infinite
nobind
persist-key
persist-tun

remote-cert-tls server
auth-user-pass
cipher AES-256-GCM
verb 3

```

用户连接时输入 **AD 域账号+密码** 即可。

---

## 九、验证

在 OpenVPN 服务端日志 `/var/log/syslog` 或 `/var/log/openvpn.log` 中可以看到：

```
PAM_AUTH: user 'user01' authenticated via LDAP

```

---

## 🔒 加固建议

1. 仅允许 VPN 用户组：在 AD 中新建组 `VPN-Users`，在 `/etc/nslcd.conf` 里加上 `filter` 限制。
2. 建议启用 **MFA**（可配合 FreeRADIUS + Google Authenticator）。
3. 如果性能要求高，可以考虑 **WireGuard + AD 集成**。

---

要不要我帮你写一份 **限制只有 AD 中 “VPN-Users” 组成员能登录 OpenVPN 的配置示例**？这样就能精确控制谁能连 VPN。

# 在 Ubuntu 18.04 上查看指定时间系统负载情况

在 Ubuntu 18.04 上查看昨晚 20 点左右的系统负载情况，通常我们需要依赖一些工具来分析和查看历史性能数据。以下是几种方法，可以帮助你获取所需的信息：

### 1. **使用 `sar`（System Activity Reporter）**

`sar` 是一个非常强大的系统性能监控工具，可以查看历史负载情况。首先，你需要确保 `sysstat` 包已安装，因为 `sar` 工具包含在这个包里。

**安装 `sysstat` 包**：

```bash
sudo apt update
sudo apt install sysstat

```

**启用 `sysstat` 服务**：  
默认情况下，`sysstat` 可能没有启用收集数据，你需要启用该服务：

```bash
sudo systemctl enable sysstat
sudo systemctl start sysstat

```

**查看负载数据**：  
`sar` 默认每 10 分钟收集一次数据，数据会存储在 `/var/log/sysstat/` 目录下。你可以使用以下命令查看负载数据：

```bash
sar -u -f /var/log/sysstat/sa<day_of_month> -s <start_time> -e <end_time>

```

例如，如果你想查看昨天（假设是 7 月 22 日）晚上 20:00 到 20:30 的 CPU 使用情况：

```bash
sar -u -f /var/log/sysstat/sa22 -s 20:00:00 -e 20:30:00

```

**其他常见的 `sar` 参数**：

- `-u`：查看 CPU 使用情况。
- `-r`：查看内存使用情况。
- `-q`：查看队列长度（负载情况）。

### 2. **使用 `uptime` 和 `w`**

如果你只关心系统的负载而不关心其他细节，可以使用 `uptime` 或 `w` 命令来查看系统的当前负载情况。虽然这些工具不提供历史数据，但可以用来查看当前的负载状态。

- 查看当前系统负载：
    
    ```bash
    uptime
    
    ```
- 查看当前登录用户和负载：
    
    ```bash
    w
    
    ```

这些工具展示的信息通常包括系统负载、用户数和最近活动。

### 3. **查看 `/var/log/syslog` 或 `dmesg`**

`syslog` 文件和 `dmesg` 输出中有一些系统负载的间接信息。如果有异常情况（比如系统负载过高时），这些日志可能会记录相关的错误或警告信息。

- 查看 `/var/log/syslog`：
    
    ```bash
    grep "load" /var/log/syslog
    
    ```
- 使用 `dmesg` 查看内核日志：
    
    ```bash
    dmesg | grep "load"
    
    ```

### 4. **使用 `htop` 或 `top`（实时查看负载）**

`htop` 和 `top` 工具可以让你实时查看系统负载情况，包括 CPU 使用率、内存、进程等信息。这些工具只会显示当前的负载，但对于实时监控有很大帮助。

- 安装 `htop`（如果没有安装）：
    
    ```bash
    sudo apt install htop
    
    ```
- 启动 `htop`：
    
    ```bash
    htop
    
    ```
- 或者使用 `top`（默认工具）：
    
    ```bash
    top
    
    ```

### 5. **使用 `atop`（适用于高级负载分析）**

`atop` 是另一个非常强大的性能监控工具，可以提供更为详细的历史性能数据，类似于 `sar`，但提供了更多的性能指标。

**安装 `atop`**：

```bash
sudo apt install atop

```

**查看历史负载**：  
`atop` 会将每分钟的系统性能数据保存在 `/var/log/atop/` 目录中。你可以使用以下命令查看指定日期的数据：

```bash
atop -r /var/log/atop/atop_<date>.gz

```

例如，查看 7 月 22 日的负载数据：

```bash
atop -r /var/log/atop/atop_2025-07-22.gz

```

### 6. **使用 `journalctl` 查看系统日志**

`journalctl` 是 `systemd` 的日志查看工具，它可以提供关于系统负载的详细日志信息，特别是在 `systemd` 日志中记录的服务崩溃、资源消耗等事件。

- 查看指定时间段的系统日志：
    
    ```bash
    journalctl --since "2025-07-22 20:00:00" --until "2025-07-22 20:30:00"
    
    ```
- 如果你想查看所有负载相关的日志：
    
    ```bash
    journalctl | grep "load"
    
    ```

---

从你的输出信息来看，`sysstat` 服务已经启用并正在运行，但 `sar` 无法找到历史数据文件。出现的错误 `Cannot open /var/log/sysstat/sa23: No such file or directory` 表示当前没有收集到 `sa23` 文件，这是因为 `sysstat` 收集的数据文件是按天生成的，而目前没有历史数据文件。

### 解决方案：

1. **等待数据收集**  
    `sysstat` 会每隔一段时间（默认每 10 分钟）收集一次数据并保存到 `/var/log/sysstat/` 目录下的 `sa<day_of_month>` 文件中。因为你的 `sysstat` 服务刚刚启动，所以在这个月（23号）之前没有历史数据。如果你需要查看今天的历史数据，你必须等一段时间，直到它开始收集数据。
    
    你可以使用以下命令查看 `/var/log/sysstat/` 目录：
    
    ```bash
    ls /var/log/sysstat/
    
    ```
    
    如果数据收集顺利，它将在该目录下生成名为 `sa23`（23号）的文件。你可以稍后使用 `sar` 查看数据：
    
    ```bash
    sar -u -f /var/log/sysstat/sa23
    
    ```
2. **手动触发数据收集**  
    你也可以手动触发 `sysstat` 数据收集，但通常它是自动运行的。如果你急需当前负载数据并希望它立刻记录，可以使用以下命令手动触发 `sadc`（系统活动数据收集器）：
    
    ```bash
    sudo sadc -S DISK 1 /var/log/sysstat/sa$(date +\%d)
    
    ```
    
    这条命令会立即收集当前的数据，并将其存储为当天的 `sa<day>` 文件。随后，你就可以使用 `sar` 来查看该数据。
3. **实时监控（`sar`）**  
    目前，你可以使用 `sar` 的实时监控模式查看当前的 CPU 使用情况。你已经尝试了 `sar -u 1 5` 来查看实时的 CPU 使用情况。此命令每秒钟输出一次系统负载数据。
    
    你也可以用以下命令来监控其它资源：
    
    ```bash
    sar -r 1 5  # 查看内存使用情况
    sar -q 1 5  # 查看负载队列和上下文切换情况
    
    ```
4. **查看 `sysstat` 配置**  
    你可以查看 `/etc/default/sysstat` 文件，确保它设置为正确的启用模式：
    
    ```bash
    sudo nano /etc/default/sysstat
    
    ```
    
    确保以下内容：
    
    ```bash
    ENABLED="true"
    
    ```
5. **重新启动 `sysstat` 服务**  
    如果你在配置中做了任何更改，或者数据收集似乎没有正常工作，你可以尝试重新启动 `sysstat` 服务：
    
    ```bash
    sudo systemctl restart sysstat
    
    ```

### 结论

- 当前 `sysstat` 数据收集还没有历史数据，因为数据收集刚启用。你可以等一段时间，等待 `sar` 收集到新的数据文件。
- 如果急需查看负载情况，使用 `sar -u 1 5` 来查看实时数据。
- 确保 `sysstat` 服务正确配置并已启动。

等待一段时间后，你应该能够查看到数据文件并使用 `sar` 查看历史性能数据。

是的，`sar` **可以只查看指定网卡（如 `eth0`）的记录**，你只需加上 `grep` 或使用 `awk` 过滤，或用 `sadf` 工具格式化提取。

---

## ✅ 方法一：使用 `sar -n DEV` + `grep eth0`

如果你在查看实时或历史文件时：

```bash
sar -n DEV -f /var/log/sysstat/sa24 | grep 'eth0'

```

示例输出：

```
10:05:01 AM      eth0      1.06      0.82      0.11      0.06      0.00      0.00      0.53      0.00
10:15:01 AM      eth0    106.82     25.62    127.40     30.38      0.00      0.00      0.45      0.10
Average:         eth0     21.39      5.11     25.31      5.99      0.00      0.00      0.44      0.02

```

---

## ✅ 方法二：用 `awk` 精确输出时间和带宽（比如 `rxkB/s` 和 `txkB/s`）

```bash
sar -n DEV -f /var/log/sysstat/sa24 | awk '$3=="eth0" {print $1, $2, $6, $7}'

```

示例输出：

```
10:05:01 AM 0.11 0.06
10:15:01 AM 127.40 30.38
Average:    25.31 5.99

```

> 其中 `$6` 和 `$7` 分别是 `rxkB/s` 与 `txkB/s`（接收/发送速度）

---

## ✅ 方法三：用 `sadf` 以 CSV / JSON / JSON-pretty 格式提取 eth0

```bash
sadf -d /var/log/sysstat/sa24 -- -n DEV | grep '"eth0"'

```

或者格式化为 JSON：

```bash
sadf -j /var/log/sysstat/sa24 -- -n DEV | jq '.sysstat.hosts[0].statistics[] | select(.network_iface.iface=="eth0")'

```

这样方便你用脚本分析。

---

## ✅ 小技巧：仅显示某时间段的 eth0 流量

比如查看 `09:00` 到 `11:00`：

```bash
sar -n DEV -s 09:00:00 -e 11:00:00 -f /var/log/sysstat/sa24 | grep eth0

```

---

### 总结

<table id="bkmrk-%E7%9B%AE%E6%A0%87-%E5%91%BD%E4%BB%A4-%E6%9F%A5%E7%9C%8B-eth0-%E6%89%80%E6%9C%89%E8%AE%B0%E5%BD%95-%60"><thead><tr><th>目标</th><th>命令</th></tr></thead><tbody><tr><td>查看 eth0 所有记录</td><td>`sar -n DEV -f /var/log/sysstat/saXX</td></tr><tr><td>提取 rx/tx 速率</td><td>`awk '$3=="eth0" {print $1, $2, $6, $7}'`</td></tr><tr><td>精确时间段</td><td>`sar -s HH:MM:SS -e HH:MM:SS`</td></tr><tr><td>JSON / CSV 格式分析</td><td>`sadf -j` / `sadf -d`</td></tr></tbody></table>

---

如果你想生成图表（比如 CSV 出图）或自动脚本日报，我可以继续帮你写。是否需要？

我们可以用 `sar` + `awk` + `gnuplot` 来每日生成 **eth0 的 rx/tx 带宽折线图**，并自动生成日报（HTML 或 PDF 都可以）。

以下是完整方案（适用于 Ubuntu 18.04）：

---

## ✅ 一、准备环境

```bash
sudo apt update
sudo apt install sysstat gnuplot

```

---

## ✅ 二、脚本：生成带宽折线图 + 简要日报（HTML）

假设我们统计 `eth0` 网卡数据：

### 脚本 `/usr/local/bin/gen_net_report.sh`

```bash
#!/bin/bash

# 日期变量
DATE=$(date +%d)
FILE="/var/log/sysstat/sa$DATE"
TMPDIR="/tmp/net_report"
mkdir -p "$TMPDIR"

# 数据文件
DATFILE="$TMPDIR/net_data.dat"
PNGFILE="$TMPDIR/net_usage_$DATE.png"
HTMLFILE="$TMPDIR/net_usage_$DATE.html"

# 生成 rx/tx 数据文件
sar -n DEV -f "$FILE" | awk '$3=="eth0" && $1 ~ /[0-9]/ {print $1, $2, $6, $7}' > "$DATFILE"

# 生成 gnuplot 脚本
GNUPLOT_SCRIPT="$TMPDIR/plot_script.gp"
cat > "$GNUPLOT_SCRIPT" <<EOF
set terminal png size 1000,600
set output "$PNGFILE"
set title "Network Usage (eth0) - $DATE"
set xdata time
set timefmt "%I:%M:%S %p"
set format x "%H:%M"
set xlabel "Time"
set ylabel "kB/s"
set grid
plot "$DATFILE" using 1:3 title "rxkB/s" with lines lw 2 lt rgb "blue", \
     "$DATFILE" using 1:4 title "txkB/s" with lines lw 2 lt rgb "red"
EOF

# 执行 gnuplot
gnuplot "$GNUPLOT_SCRIPT"

# 生成 HTML 简报
cat > "$HTMLFILE" <<EOF
<html>
<head><title>Network Daily Report - eth0</title></head>
<body>
<h2>Network Usage Report - $(date '+%Y-%m-%d')</h2>
<p>Interface: <strong>eth0</strong></p>
<img src="net_usage_$DATE.png" alt="Network Usage">
</body>
</html>
EOF

# 输出结果提示
echo "📊 图表保存路径: $PNGFILE"
echo "📄 HTML报表路径: $HTMLFILE"

```

---

## ✅ 三、添加计划任务（每天自动运行）

编辑 crontab：

```bash
sudo crontab -e

```

加入每天早上 7 点运行：

```bash
0 7 * * * /usr/local/bin/gen_net_report.sh

```

---

## ✅ 四、结果展示（示意）

运行后将在 `/tmp/net_report/` 下生成：

- 折线图 `net_usage_25.png`
- 简易日报 HTML `net_usage_25.html`

你可将 HTML 报表放到内网服务器上，用浏览器查看。

---

## ✅ 五、可选拓展

- 改为输出 PDF（配合 `wkhtmltopdf`）
- 自动发日报邮件（`mailx`）
- 报表集成多个网卡、CPU、内存
- 每小时生成图而非每日

---

是否需要我帮你扩展为自动发邮件或支持多日历史图？

</body></html>

# sar-报表集成多个网卡、CPU、内存，输出为PDF，并自动发日报邮件（mailx）

你这个需求可以通过以下组合来实现：

---

## ✅ 一、采集工具：`sysstat`（提供 sar 数据）

- 默认安装后已自动收集网卡、CPU、内存等历史数据，保存在 `/var/log/sysstat/saXX`。
- 确保服务启动：
    
    ```bash
    sudo systemctl enable sysstat
    sudo systemctl start sysstat
    
    ```

---

## ✅ 二、数据提取 + 图表生成（Python 脚本）

用 Python 自动读取 sar 数据，生成图表，并导出 PDF 报告：

### 脚本功能：

- 使用 `sar` 提取：
    
    
    - 网卡流量（rx/tx）
    - CPU 使用率（idle, user, system）
    - 内存使用（free, used, available）
- 使用 `matplotlib` 生成图表（折线图）
- 用 `reportlab` / `matplotlib.backends.backend_pdf` 导出 PDF 报告
- 最终用 `mailx` 自动发邮件

---

## ✅ 三、Python 示例脚本结构（建议每日运行）

### 🔧 依赖安装

```bash
sudo apt install sysstat mailx
pip install matplotlib pandas

```

### 📜 Python 脚本核心（简化结构）

```python
import os
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import subprocess
from datetime import datetime

# 网卡、CPU、内存数据抓取命令
today = datetime.now().strftime("%d")
safile = f"/var/log/sysstat/sa{today}"
iface = "eth0"

def run_sar(cmd):
    result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
    return result.stdout.decode('utf-8').splitlines()

# 网卡流量
def get_net_data():
    lines = run_sar(f"sar -n DEV -f {safile} | grep {iface}")
    data = []
    for line in lines:
        parts = line.split()
        if len(parts) >= 8 and parts[1] != 'IFACE':
            data.append([f"{parts[0]} {parts[1]}", float(parts[4]), float(parts[5])])
    df = pd.DataFrame(data, columns=["Time", "rx_kB", "tx_kB"])
    return df

# CPU 使用率
def get_cpu_data():
    lines = run_sar(f"sar -u -f {safile}")
    data = []
    for line in lines:
        parts = line.split()
        if len(parts) >= 8 and parts[1] != 'CPU':
            data.append([f"{parts[0]} {parts[1]}", 100 - float(parts[7])])  # 使用率 = 100 - idle
    df = pd.DataFrame(data, columns=["Time", "CPU_Usage"])
    return df

# 内存使用率
def get_mem_data():
    lines = run_sar(f"sar -r -f {safile}")
    data = []
    for line in lines:
        parts = line.split()
        if len(parts) >= 5 and parts[1] != 'kbmemfree':
            total = float(parts[3]) + float(parts[1])
            used_pct = 100 * float(parts[3]) / total
            data.append([f"{parts[0]} {parts[1]}", used_pct])
    df = pd.DataFrame(data, columns=["Time", "Mem_Used"])
    return df

# 生成图表并导出为 PDF
def generate_pdf():
    pdf_file = "/tmp/system_report.pdf"
    with PdfPages(pdf_file) as pdf:
        for title, df, ycol in [
            ("Net Usage (kB/s)", get_net_data(), ["rx_kB", "tx_kB"]),
            ("CPU Usage (%)", get_cpu_data(), ["CPU_Usage"]),
            ("Memory Usage (%)", get_mem_data(), ["Mem_Used"]),
        ]:
            df.set_index("Time", inplace=True)
            df[ycol].plot(figsize=(10, 4), title=title)
            plt.xticks(rotation=45)
            plt.tight_layout()
            pdf.savefig()
            plt.close()
    return pdf_file

# 发送邮件
def send_mail(pdf_path):
    subject = f"Daily System Report - {datetime.now().strftime('%Y-%m-%d')}"
    cmd = f'echo "Please find the attached system usage report." | mailx -a {pdf_path} -s "{subject}" you@example.com'
    subprocess.run(cmd, shell=True)

# 主逻辑
pdf = generate_pdf()
send_mail(pdf)

```

---

## ✅ 四、定时任务设置（crontab）

编辑定时任务：

```bash
crontab -e

```

添加：

```bash
0 8 * * * /usr/bin/python3 /opt/daily_report.py

```

每天 8 点自动发送日报邮件。

---

## ✅ 五、效果示例（PDF 报告）

每页一个图表（如折线图）：

- **Page 1**: eth0 接收/发送流量（rx\_kB/s, tx\_kB/s）
- **Page 2**: CPU 使用率 %
- **Page 3**: 内存使用率 %

---

如需支持多网卡（eth0, ens33, lo 等）、加上磁盘IO、TCP连接数、服务运行状态（如 nginx、mysql），都可以继续拓展脚本。

是否需要我帮你生成完整脚本并打包？或者加一个多主机日报支持？

# sar系统分析及优化建议示例

\### 系统性能分析报告（基于 `sar` 数据）

\#### \*\*基础环境\*\*  
\- \*\*系统\*\*：Ubuntu（Linux 5.4.0-216-generic）  
\- \*\*日期\*\*：2025年07月28日 20:00:00 - 23:59:59  
\- \*\*CPU架构\*\*：x86\_64（16逻辑CPU）  
\- \*\*物理内存\*\*：约 32.5 GB（由 `%memused` 反推计算得出）

[![图片.png](https://bost.lesrec.cn/uploads/images/gallery/2025-07/scaled-1680-/8cutoukepJslAtFV-pV3u2Rgk9h.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-07/8cutoukepJslAtFV-pV3u2Rgk9h.png)

\### \*\*CPU 使用分析 (`sar -u`）\*\*  
```bash  
\# 关键指标  
Average: all 7.76 0.00 1.12 0.03 0.00 91.09  
```  
\- \*\*整体负载极低\*\*：  
\- \*\*平均空闲率 (`%idle`)\*\*：91.09%，表明 CPU 资源充足。  
\- \*\*用户态占用 (`%user`)\*\*：平均 7.76%，峰值 10.07%（20:45），无持续高负载。  
\- \*\*内核态占用 (`%system`)\*\*：平均 1.12%，系统调用开销正常。  
\- \*\*其他指标\*\*：  
\- \*\*`%iowait`\*\*：平均 0.03%（最高 0.06%），\*\*无 I/O 瓶颈\*\*。  
\- \*\*`%steal`\*\*：0%，无虚拟机资源争抢。  
\- \*\*`%nice`\*\*：0%，无优先级调整进程。

\#### \*\*时间趋势\*\*  
\- \*\*20:00-22:00\*\*：轻度波动（`%user` 8-10%），峰值在 20:45（10.07%）。  
\- \*\*22:00 后\*\*：负载显著下降（`%user` 降至 6.27-6.70%），空闲率升至 92-93%。

&gt; \*\*结论\*\*：CPU 资源高度空闲，系统未达性能瓶颈。

\---

\### \*\*内存使用分析 (`sar -r`）\*\*  
```bash  
\# 关键指标（平均值）  
Average: 591147 20035239 11154124 34.75 1292042 14122169 22197908 61.17 13299787 13005861 64628  
```  
\- \*\*物理内存\*\*（约 32.5 GB）：  
\- \*\*已用 (`%memused`)\*\*：平均 34.75%（11.15 GB），\*\*利用率低\*\*。  
\- \*\*空闲 (`kbmemfree`)\*\*：平均 577 MB（波动范围 266 MB - 945 MB）。  
\- \*\*可用内存 (`kbavail`)\*\*：稳定在 19.1 GB，\*\*资源充足\*\*。  
\- \*\*缓存与缓冲\*\*：  
\- \*\*磁盘缓存 (`kbcached`)\*\*：平均 13.5 GB（占比 41.5%），系统充分利用空闲内存加速 I/O。  
\- \*\*缓冲区 (`kbbuffers`)\*\*：平均 1.29 GB。  
\- \*\*工作负载需求 (`kbcommit`)\*\*：  
\- 平均需 22.2 GB 内存（含交换空间）。  
\- \*\*`%commit`\*\*：平均 61.17%，表明物理内存 + 交换空间总需求在安全范围内（反推总地址空间约 36.3 GB，交换空间约 3.8 GB）。  
\- \*\*其他\*\*：  
\- \*\*脏数据 (`kbdirty`)\*\*：平均 63 MB，写回磁盘压力小。  
\- \*\*活跃/非活跃内存\*\*：活跃约 12.7 GB，非活跃约 12.4 GB，内存管理合理。

&gt; \*\*结论\*\*：内存资源充裕，无压力迹象；缓存机制有效优化 I/O 性能。

\---

\### \*\*关键指标汇总表\*\*  
| \*\*类别\*\* | \*\*指标\*\* | \*\*平均值\*\* | \*\*峰值\*\* | \*\*状态\*\* |  
|----------------|-------------------|------------|------------|------------------|  
| \*\*CPU\*\* | `%idle` | 91.09% | 93.16% | 资源高度空闲 |  
| | `%user` | 7.76% | 10.07% | 低负载 |  
| | `%iowait` | 0.03% | 0.06% | 无 I/O 瓶颈 |  
| \*\*内存\*\* | `%memused` | 34.75% | 34.80% | 低利用率 |  
| | `kbavail` | 19.1 GB | 19.1 GB | 充足可用内存 |  
| | `kbcached` | 13.5 GB | 14.4 GB | 缓存高效利用 |  
| \*\*工作负载\*\* | `%commit` | 61.17% | 61.23% | 地址空间需求安全 |

\---

\### \*\*优化建议\*\*  
1\. \*\*资源利用\*\*：  
\- \*\*提升应用负载\*\*：当前 CPU/内存 大量闲置，可部署更多服务。  
\- \*\*调整交换空间\*\*：若交换空间过大（推算约 3.8 GB），可适当缩减（物理内存充足）。  
2\. \*\*监控扩展\*\*：  
\- 补充 `sar -d` 分析磁盘 I/O 详细性能（尽管 `%iowait` 低，仍需确认）。  
\- 检查应用日志，确认 20:45 的 CPU 小高峰原因（是否可优化）。  
3\. \*\*内存策略\*\*：  
\- 保持大缓存配置，这对读取密集型应用有利。  
\- 监控 `kbdirty` 若持续增长（&gt;100 MB），需检查磁盘写性能。

&gt; \*\*整体评估\*\*：系统健康，性能瓶颈未出现，资源利用率有较大提升空间。

<span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">分析过程：</span>

<span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">我们有两组数据：一组是CPU使用情况（sar -u），另一组是内存使用情况（sar -r）。时间范围都是2025年07月28日20:00:00到23:59:59，系统有16个CPU。</span>

<div id="bkmrk--1" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--2" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 首先分析CPU使用情况（sar -u）：</span>

<div id="bkmrk--3" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 各列含义：</span>

<div id="bkmrk--4" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %user: 用户态进程占用CPU百分比</span>

<div id="bkmrk--5" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %nice: 优先级调整（nice值）的用户进程占用CPU百分比</span>

<div id="bkmrk--6" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %system: 内核态进程占用CPU百分比</span>

<div id="bkmrk--7" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %iowait: CPU等待I/O操作的时间百分比</span>

<div id="bkmrk--8" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %steal: 在虚拟化环境中被其他虚拟机占用的CPU时间百分比</span>

<div id="bkmrk--9" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %idle: CPU空闲时间百分比</span>

<div id="bkmrk--10" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--11" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 观察数据：</span>

<div id="bkmrk--12" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 1. 整个时间段内，%user在6.10%到10.07%之间波动，平均为7.76%。</span>

<div id="bkmrk--13" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 2. %nice始终为0，说明没有进程被调整过优先级（或者调整优先级的进程没有占用显著CPU）。</span>

<div id="bkmrk--14" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 3. %system在0.72%到1.61%之间，平均1.12%。</span>

<div id="bkmrk--15" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 4. %iowait非常低，在0.01%到0.06%之间，平均0.03%，说明I/O等待不是瓶颈。</span>

<div id="bkmrk--16" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 5. %steal始终为0，说明没有虚拟化环境下的资源争抢（或者不在虚拟化环境中）。</span>

<div id="bkmrk--17" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 6. %idle很高，平均91.09%，说明系统大部分时间处于空闲状态，CPU资源充足。</span>

<div id="bkmrk--18" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--19" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 从时间趋势看：</span>

<div id="bkmrk--20" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> - 20:45和21:05左右有两次较高的%user（10.07%和9.55%），但随后下降。</span>

<div id="bkmrk--21" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> - 22:25之后，%user明显下降，稳定在6%~7%左右，同时%idle上升至92%~93%。</span>

<div id="bkmrk--22" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--23" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 结论：CPU负载非常低，系统运行良好，没有明显的CPU瓶颈。</span>

<div id="bkmrk--24" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--25" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 接下来分析内存使用情况（sar -r）：</span>

<div id="bkmrk--26" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 各列含义：</span>

<div id="bkmrk--27" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree: 空闲的物理内存（KB）</span>

<div id="bkmrk--28" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbavail: 可用的内存（包括缓冲区和缓存，估算可用于分配的内存，KB）</span>

<div id="bkmrk--29" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused: 已使用的物理内存（不包括内核缓冲区缓存，但包括应用程序使用的缓存，KB）</span>

<div id="bkmrk--30" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %memused: 已使用内存百分比（kbmemused / (kbmemfree+kbmemused) ？注意：实际总内存需要计算）</span>

<div id="bkmrk--31" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbbuffers: 内核缓冲区（buffer）使用的内存（KB）</span>

<div id="bkmrk--32" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbcached: 内核页面缓存（page cache）使用的内存（KB）</span>

<div id="bkmrk--33" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbcommit: 当前工作负载所需的内存（估计值，KB）</span>

<div id="bkmrk--34" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %commit: 当前工作负载所需内存占总内存（物理内存+交换空间）的百分比</span>

<div id="bkmrk--35" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbactive: 活跃内存（最近使用过的内存，KB）</span>

<div id="bkmrk--36" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbinact: 非活跃内存（最近未使用，可回收的内存，KB）</span>

<div id="bkmrk--37" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbdirty: 等待写回磁盘的脏数据内存（KB）</span>

<div id="bkmrk--38" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--39" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 注意：总物理内存 = kbmemfree + kbmemused（根据man sar，kbmemused不包括缓冲区和缓存，但包括应用程序的缓存，所以总内存应该等于kbmemfree+kbmemused+kbbuffers+kbcached？实际上，man sar中说明kbmemused是used memory minus buffers/cache，所以总内存应该是kbmemfree + kbmemused + buffers + cached，但这里kbmemused已经排除了buffers和cached？）</span>

<div id="bkmrk--40" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--41" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 然而，根据输出的列标题，我们可以这样理解：</span>

<div id="bkmrk--42" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree：就是空闲的物理内存。</span>

<div id="bkmrk--43" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused：已经使用的物理内存（这个使用量包括了应用程序使用的内存，但不包括缓冲区和缓存，因为缓冲区和缓存可以被释放）。</span>

<div id="bkmrk--44" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--45" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 实际上，在sar -r的输出中，总内存 = kbmemfree + kbmemused + kbbuffers + kbcached？不一定，因为kbmemused的定义可能不同。但我们可以通过数据来验证：</span>

<div id="bkmrk--46" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--47" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 例如，第一行：</span>

<div id="bkmrk--48" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree = 272628, kbmemused=11141368, kbbuffers=1293468, kbcached=14409484</span>

<div id="bkmrk--49" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 那么总内存 = 272628 + 11141368 + 1293468 + 14409484 = 27165948 KB? 但这样加起来似乎不对，因为这样总内存大约是27GB。</span>

<div id="bkmrk--50" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--51" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 另一种理解：在sar -r的输出中，kbmemused是包括缓冲区和缓存的内核统计中的used吗？实际上，man sar（sysstat版本）说明：</span>

<div id="bkmrk--52" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree: Amount of free memory available to the kernel. This is the amount of memory left unused by the system.</span>

<div id="bkmrk--53" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused: Amount of used memory. This does not take into account memory used by the kernel itself.</span>

<div id="bkmrk--54" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> %memused: Percentage of used memory.</span>

<div id="bkmrk--55" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--56" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 在早期的sar版本中，内存使用统计有两种方式：</span>

<div id="bkmrk--57" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 方式1：used = total - free - buffers - cache</span>

<div id="bkmrk--58" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 方式2：used = total - free</span>

<div id="bkmrk--59" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--60" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 而这里的kbmemused似乎采用的是方式1？即应用程序实际使用的内存（不包括缓冲区和缓存）。因此，总内存应该是：</span>

<div id="bkmrk--61" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 总内存 = kbmemfree + kbmemused + kbbuffers + kbcached</span>

<div id="bkmrk--62" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--63" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 我们计算第一行：272628 + 11141368 + 1293468 + 14409484 = 272628+11141368=11413996; 11413996+1293468=12707464; 12707464+14409484=27116948 KB -&gt; 约27.1GB</span>

<div id="bkmrk--64" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--65" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 但是，我们注意到%memused为34.71%，那么总内存应该是 kbmemused / %memused \* 100 = 11141368 / 0.3471 ≈ 32,100,000 KB（约32.1GB）。这个计算与上面的27.1GB不符。</span>

<div id="bkmrk--66" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--67" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 因此，重新审视：实际上，%memused的基数是总物理内存（不包括交换空间）。那么总物理内存 = kbmemused / (%memused/100) = 11141368 / 0.3471 ≈ 32,100,000 KB（约32.1GB）。而kbmemfree是空闲内存，所以总内存应该等于 kbmemfree + kbmemused? 即 272628 + 11141368 = 11413996 KB? 这只有11.4GB，与32.1GB不符。</span>

<div id="bkmrk--68" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--69" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 这可能是由于对kbmemused的定义不同。在较新的版本中，kbmemused是包括缓冲区和缓存的内核统计中的used吗？</span>

<div id="bkmrk--70" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--71" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 实际上，根据man sar（在较新的版本，比如11.7.3）：</span>

<div id="bkmrk--72" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree: Free memory (in kB) available to the kernel. This corresponds to the "MemFree" field in /proc/meminfo.</span>

<div id="bkmrk--73" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused: Used memory (in kB). This corresponds to the "MemTotal" minus "MemFree" fields in /proc/meminfo, and does not take into account memory used by the kernel itself (for buffers, caches, etc.). So this is the amount of memory used by applications.</span>

<div id="bkmrk--74" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--75" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 但是，在/proc/meminfo中：</span>

<div id="bkmrk--76" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> MemTotal: 32511328 kB （总物理内存，32.5GB）</span>

<div id="bkmrk--77" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> MemFree: 272628 kB</span>

<div id="bkmrk--78" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> MemAvailable: 20046696 kB （对应kbavail）</span>

<div id="bkmrk--79" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> Buffers: 1293468 kB</span>

<div id="bkmrk--80" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> Cached: 14409484 kB （注意，这里Cached可能还包括了其他，但大致对应）</span>

<div id="bkmrk--81" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--82" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 因此，总内存 = MemTotal = 32511328 kB（32.5GB）</span>

<div id="bkmrk--83" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 而kbmemused = MemTotal - MemFree = 32511328 - 272628 = 32238700 kB? 但是输出中kbmemused为11141368 kB，显然不对。</span>

<div id="bkmrk--84" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--85" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 重新查阅资料：在sar的man page中，对于选项-r，它说：</span>

<div id="bkmrk--86" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemfree: Amount of free memory available to the kernel. The same as the "MemFree" field in /proc/meminfo.</span>

<div id="bkmrk--87" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused: Amount of used memory. The same as the "MemTotal" minus "MemFree" fields in /proc/meminfo, but this does not take into account memory used by the kernel itself (for buffers, caches, etc.). So this is the amount of memory used by applications.</span>

<div id="bkmrk--88" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--89" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 但是，注意：在/proc/meminfo中，MemTotal - MemFree = 所有被使用的内存（包括内核缓冲区和缓存）。那么为什么sar的kbmemused却比较小呢？实际上，在sar -r的输出中，kbmemused是排除了缓冲区和缓存的。也就是说，kbmemused = MemTotal - MemFree - Buffers - Cached? 这样计算：</span>

<div id="bkmrk--90" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--91" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 第一行：32511328 - 272628 - 1293468 - 14409484 = 32511328 - 272628 = 32238700; </span>

<div id="bkmrk--92" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 32238700 - 1293468 = 30945232; </span>

<div id="bkmrk--93" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 30945232 - 14409484 = 16535748 KB? 与输出的11141368不一致。</span>

<div id="bkmrk--94" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--95" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 另一种可能是：kbmemused = MemTotal - MemFree - Buffers - Cached - SReclaimable（其中SReclaimable是Slab可回收部分）? 但我们没有SReclaimable的数据。</span>

<div id="bkmrk--96" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--97" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 实际上，从输出中我们看到kbavail（对应MemAvailable）是20046696 kB，这个值通常大于MemFree，因为它包括了一些可回收的缓存和缓冲区。而%memused=34.71%，那么总内存为32511328 kB，所以kbmemused应该是32511328 \* 0.3471 ≈ 11285100 kB，接近输出的11141368 kB（可能由于四舍五入或统计时间点差异）。</span>

<div id="bkmrk--98" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--99" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 因此，我们可以认为总物理内存约为32.5GB（32511328 kB）。那么：</span>

<div id="bkmrk--100" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> kbmemused = 总物理内存 \* %memused / 100 ≈ 32511328 \* 0.3471 ≈ 11285100 kB（与11141368有微小差异，可能是四舍五入或统计时间点不同）。</span>

<div id="bkmrk--101" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--102" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 现在，我们观察数据：</span>

<div id="bkmrk--103" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 1. 空闲内存（kbmemfree）在272628 KB（约266MB）到967700 KB（约945MB）之间波动，平均591147 KB（约577MB）。</span>

<div id="bkmrk--104" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 2. 可用内存（kbavail）在20018096 KB（约19.1GB）到20053352 KB（约19.1GB）之间，平均20035239 KB（约19.1GB），比较稳定。</span>

<div id="bkmrk--105" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 3. 已使用内存（kbmemused）在11137576 KB（约10.6GB）到11169060 KB（约10.7GB）之间，平均11154124 KB（约10.6GB），占34.75%（平均）。</span>

<div id="bkmrk--106" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 4. 缓冲区（kbbuffers）和缓存（kbcached）占用较多：缓冲区约1.29GB（1292042 KB平均），缓存约13.5GB（14122169 KB平均）。</span>

<div id="bkmrk--107" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 5. 活跃内存（kbactive）平均13.0GB（13299787 KB），非活跃内存（kbinact）平均12.4GB（13005861 KB），说明系统有较多的缓存。</span>

<div id="bkmrk--108" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 6. 脏数据（kbdirty）平均64.6MB（64628 KB），在可接受范围（通常不会立即影响性能）。</span>

<div id="bkmrk--109" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 7. kbcommit（当前工作负载所需内存）平均22.2GB（22197908 KB），占总内存（物理内存+交换空间）的61.17%。这意味着如果所有应用程序都要求最大内存，那么需要22.2GB，而当前物理内存32.5GB，加上交换空间（假设有），所以61.17%的占用率并不危险。</span>

<div id="bkmrk--110" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--111" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 注意：%commit的计算基数是物理内存+交换空间的总和。假设交换空间为0，那么%commit=22197908/32511328≈68.2%，但实际%commit平均为61.17%，所以说明有交换空间（swap）存在。设总内存+交换空间为S，则：</span>

<div id="bkmrk--112" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 22197908 / S = 0.6117 =&gt; S = 22197908 / 0.6117 ≈ 36,288,000 KB（约36.3GB）</span>

<div id="bkmrk--113" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 那么交换空间 = S - 物理内存 = 36.3GB - 32.5GB = 3.8GB。</span>

<div id="bkmrk--114" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--115" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 结论：</span>

<div id="bkmrk--116" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> - 内存使用情况：物理内存使用率平均34.75%，说明有大量空闲内存（实际上被用于缓存，所以可用内存有19.1GB）。系统内存充足。</span>

<div id="bkmrk--117" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> - 缓存使用较多（平均13.5GB），这是正常现象，可以提高系统性能。</span>

<div id="bkmrk--118" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> - 交换空间使用情况：从%commit来看，虽然工作负载需要22.2GB内存，但物理内存有32.5GB，所以应该不会用到交换空间（或者很少）。我们可以通过sar -W（页面交换统计）来确认，但这里没有提供。</span>

<div id="bkmrk--119" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--120" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 整体来看，系统在监控时间段内（20:00-24:00）运行状态良好，CPU空闲率高，内存充足，没有明显的I/O等待。</span>

<div id="bkmrk--121" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><div id="bkmrk--122" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 建议：</span>

<div id="bkmrk--123" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 1. 由于CPU大部分时间空闲，可以考虑增加应用负载或优化应用以利用空闲CPU。</span>

<div id="bkmrk--124" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 2. 内存充足，可以考虑减少交换空间（如果配置过大）或者将部分内存用于其他服务（如缓存服务）。</span>

<div id="bkmrk--125" style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; height: 6px;">  
</div><span style="color: rgb(139, 139, 139); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 3. 监控I/O等待虽然低，但如果有应用性能问题，可以进一步检查磁盘I/O（使用sar -d）和网络状况。</span>

# 服务器内存不足临时处理方案-调整swap查找内存消耗等

\# 服务器内存使用深度分析

根据您提供的`free -mh`和`sar -r`命令输出，您的服务器内存使用情况存在严重问题，以下是详细分析：

\## 1. 当前内存状态概览

\*\*关键指标\*\*：  
\- \*\*总内存\*\*：15GB  
\- \*\*已使用内存\*\*：15GB (98.7%使用率)  
\- \*\*可用内存\*\*：仅剩163MB  
\- \*\*缓存使用\*\*：2.4GB  
\- \*\*交换空间\*\*：4GB (未使用)

\## 2. 主要问题诊断

\### 内存耗尽危机  
\- 系统持续保持98%以上的内存使用率，处于极度危险状态  
\- 可用内存仅剩163MB，随时可能触发OOM Killer终止进程

\### 异常现象  
1\. \*\*高active内存\*\*：13.9GB (`kbactive`) 处于活跃状态难以回收  
2\. \*\*缓存未充分利用\*\*：虽有2.4GB缓存但系统仍内存不足  
3\. \*\*交换空间闲置\*\*：4GB交换空间完全未使用

\### 潜在原因  
\- \*\*内存泄漏\*\*：某个/某些进程持续占用内存不释放  
\- \*\*配置不当\*\*：关键服务(如Java)内存参数设置过高  
\- \*\*监控缺失\*\*：未设置内存告警导致问题恶化

\## 3. 紧急处理方案

\### 立即行动  
```bash  
\# 1. 释放缓存(临时缓解)  
echo 3 &gt; /proc/sys/vm/drop\_caches

\# 2. 查找内存消耗Top5进程  
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -6

\# 3. 监控内存变化(每5秒刷新)  
watch -n 5 "free -mh; echo; top -bn1 -o %MEM | head -10"  
```

\### 关键进程优化  
```bash  
\# 调整Java服务内存参数(示例)  
export JAVA\_OPTS="-Xms2g -Xmx4g -XX:MaxMetaspaceSize=512m"

\# 限制MySQL内存使用  
sudo vim /etc/mysql/my.cnf  
```  
添加：  
```  
innodb\_buffer\_pool\_size=1G  
table\_open\_cache=2000  
```

\## 4. 长期解决方案

\### 系统配置优化  
```bash  
\# 1. 调整swappiness  
sudo sysctl vm.swappiness=30  
echo "vm.swappiness=30" &gt;&gt; /etc/sysctl.conf

\# 2. 增加交换空间(如果磁盘允许)  
sudo fallocate -l 8G /swapfile2  
sudo chmod 600 /swapfile2  
sudo mkswap /swapfile2  
sudo swapon /swapfile2  
```

\### 服务拆分建议  
1\. 将MySQL/MongoDB迁移到独立服务器  
2\. 分离Java微服务到不同主机  
3\. 考虑使用容器化部署隔离资源

\### 监控系统部署  
```bash  
\# 安装基础监控工具  
sudo apt install prometheus-node-exporter  
```

\## 5. 后续检查清单

1\. \*\*内存泄漏检测\*\*：  
 ```bash  
 valgrind --leak-check=full &lt;your\_application&gt;  
 ```

2\. \*\*JVM堆分析\*\*：  
 ```bash  
 jmap -heap &lt;java\_pid&gt;  
 jstat -gc &lt;java\_pid&gt; 5s  
 ```

3\. \*\*定期维护计划\*\*：  
 ```bash  
 # 设置每周自动重启服务  
 sudo crontab -e  
 ```  
 添加：  
 ```  
 0 3 \* \* 0 /sbin/reboot  
 ```

当前系统状态非常危险，建议立即实施紧急措施释放内存，并尽快安排服务优化或硬件升级。

[![图片.png](https://bost.lesrec.cn/uploads/images/gallery/2025-08/scaled-1680-/bGTfk3pUg9Y2zhP5-QvayaoEJiu.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-08/bGTfk3pUg9Y2zhP5-QvayaoEJiu.png)

根据提供的 `sar -S` 和 `free -h` 数据，结合之前的内存分析，以下是更全面的评估和建议：

\## 内存与交换空间综合分析

\### 当前状态  
1\. \*\*物理内存\*\*：  
\- 总量：15GB  
\- 已用：15G (实际应用占用12G，buffers/cache占用3G)  
\- 空闲：165MB (约1%)  
\- 缓存：2.2GB

2\. \*\*交换空间\*\*：  
\- 总量：4GB  
\- 使用量：0B (完全未使用)  
\- 使用率：0% (持续)

\### 关键发现  
1\. \*\*异常现象\*\*：  
\- 物理内存持续98%+使用率但\*\*完全未使用swap\*\*  
\- 缓存相对较低(仅2.2GB/15G=14.6%)  
\- `-/+ buffers/cache`显示实际应用占用12GB

2\. \*\*可能原因\*\*：  
\- `vm.swappiness`参数被设置为0(完全禁用swap)  
\- 应用主动锁定了内存(mlock)  
\- 内核策略限制

\### 验证命令  
```bash  
\# 检查swappiness设置  
cat /proc/sys/vm/swappiness

\# 检查mlock调用  
grep -i mlock /var/log/syslog\*

\# 检查内存锁定进程  
sudo pmap -x $(pgrep -d, -f "你的应用名") | grep -i mlock  
```

\### 风险与建议

\#### 高风险项  
1\. \*\*OOM风险\*\*：  
\- 当内存耗尽时，由于swap禁用，内核会直接触发OOM killer  
\- 关键进程可能被意外终止

2\. \*\*性能瓶颈\*\*：  
\- 缓存空间不足导致磁盘I/O增加

\#### 优化建议

\*\*立即措施\*\*：  
```bash  
\# 临时启用swap使用(设置swappiness为10)  
sudo sysctl vm.swappiness=10

\# 监控swap使用变化  
watch -n 1 'free -h; sar -S 1 3'  
```

\*\*长期方案\*\*：  
1\. \*\*调整内存配置\*\*：  
 ```bash  
 # 永久设置swappiness(建议10-30)  
 echo "vm.swappiness=20" | sudo tee -a /etc/sysctl.conf  
 sudo sysctl -p  
 ```

2\. \*\*应用优化\*\*：  
\- 识别内存占用最高的进程：  
 ```bash  
 ps aux --sort=-%mem | head -n 10  
 ```  
\- 检查Java应用(如有)的堆设置：  
 ```bash  
 jcmd &lt;PID&gt; VM.flags | grep -i MaxHeap  
 ```

3\. \*\*架构改进\*\*：  
\- 增加物理内存(建议升级到32GB)  
\- 对关键应用配置cgroup内存限制  
\- 考虑使用内存压缩(zswap)

\### 配置验证  
```bash  
\# 当前内存统计  
cat /proc/meminfo | grep -E 'MemTotal|MemFree|Cached|Swap|Active|Inactive'

\# 内存事件监控  
sudo grep -i oom /var/log/kern.log  
```

\### 典型问题排查流程  
1\. 确认是否真的需要12GB应用内存  
2\. 检查是否有内存泄漏(持续增长趋势)  
3\. 评估swap完全未使用是否合理  
4\. 监控`pgsteal`和`pgscan`指标：  
 ```bash  
 sar -B 1 3  
 ```

当前配置下，系统处于"\*\*高压力但未使用安全阀\*\*"状态，建议在业务低峰期进行swappiness调整测试。

# JAVA内存泄漏问题分析优化

## 根据最新提供的系统监控数据和进程信息，以下是深入分析和具体建议：

[![图片.png](https://bost.lesrec.cn/uploads/images/gallery/2025-08/scaled-1680-/OlTSsOMzE3OcFD3y-HR7OCCV2Zk.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-08/OlTSsOMzE3OcFD3y-HR7OCCV2Zk.png)

\## 关键进程内存占用分析

\### 内存消耗Top 3进程  
1\. \*\*Tomcat Java应用 (PID 31037)\*\*：  
\- 占用内存：5.26GB (32.5%)  
\- JVM配置：`-Xms2048m -Xmx4096m` (堆内存2-4GB)  
\- 实际RSS远超堆设置，可能存在：  
 \* 直接内存泄漏(通过`-XX:MaxDirectMemorySize=1024m`配置)  
 \* 大量JNI调用或本地内存分配

2\. \*\*MySQL (PID 1129)\*\*：  
\- 占用内存：2.53GB (15.6%)  
\- 长期运行(88460小时)，需检查：  
 ```bash  
 mysqladmin variables | grep buffer\_pool  
 mysqladmin status | grep -E 'Threads|Memory'  
 ```

3\. \*\*shun-sync服务 (PID 5522)\*\*：  
\- 占用内存：1.35GB (8.3%)  
\- JVM配置：`-Xms1024m -Xmx1024m` (堆设置合理)

\## 内存问题诊断

\### 异常现象确认  
1\. \*\*物理内存\*\*：  
\- 总内存：16GB  
\- 已用：15.97GB (99.8%)  
\- 空闲：186MB (1.2%)  
\- 缓存：2.3GB (偏低)

2\. \*\*交换空间\*\*：  
\- 总量：4GB  
\- 使用量：1.3MB (0.03%)  
\- 缓存比例：10.5% (正常)

3\. \*\*内存分布\*\*：  
\- Active内存：12.4GB (其中匿名页11.1GB)  
\- Inactive内存：2.8GB

\## 根本原因分析

1\. \*\*Tomcat内存泄漏\*\*：  
\- RSS(5.26GB) &gt; Xmx(4GB) + MaxDirectMemory(1GB)  
\- 可能原因：  
 \* JNI库内存泄漏  
 \* 未受JVM管控的本地内存分配  
 \* 线程栈过多(`-Xss`设置过大)

2\. \*\*swappiness配置不当\*\*：  
\- 当前配置可能为0，导致系统宁愿触发OOM也不使用swap  
\- 验证命令：  
 ```bash  
 cat /proc/sys/vm/swappiness  
 ```

3\. \*\*MySQL内存配置\*\*：  
\- 未优化的InnoDB缓冲池可能导致内存碎片

\## 紧急处理方案

\### 立即释放内存  
```bash  
\# 清理页面缓存(临时方案)  
sync; echo 1 &gt; /proc/sys/vm/drop\_caches

\# 重启最耗内存的Tomcat(需安排维护窗口)  
systemctl restart tomcat  
```

\### JVM内存诊断  
```bash  
\# 安装JDK工具  
apt-get install -y openjdk-8-jdk

\# 检查Tomcat内存详情  
jcmd 31037 VM.native\_memory summary

[![图片.png](https://bost.lesrec.cn/uploads/images/gallery/2025-08/scaled-1680-/kQDvk9k0YwH1z5g0-2yZ7jHdSqQ.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-08/kQDvk9k0YwH1z5g0-2yZ7jHdSqQ.png)

  
jmap -histo:live 31037 | head -20

[![图片.png](https://bost.lesrec.cn/uploads/images/gallery/2025-08/scaled-1680-/0tTxmAjj1DSRlOqF-qNXapZE3c8.png)](https://bost.lesrec.cn/uploads/images/gallery/2025-08/0tTxmAjj1DSRlOqF-qNXapZE3c8.png)

```

\## 长期优化建议

1\. \*\*Tomcat优化\*\*：  
 ```diff  
\- -XX:MaxDirectMemorySize=1024m  
\+ -XX:MaxDirectMemorySize=512m  
\+ -XX:NativeMemoryTracking=summary  
\+ -XX:+AlwaysPreTouch  
 ```

2\. \*\*MySQL优化\*\*：  
 ```ini  
 # /etc/mysql/my.cnf  
 \[mysqld\]  
 innodb\_buffer\_pool\_size = 1G  
 innodb\_buffer\_pool\_instances = 4  
 ```

3\. \*\*系统参数调整\*\*：  
 ```bash  
 # 防止OOM killer误杀  
 echo 'vm.panic\_on\_oom=2' &gt;&gt; /etc/sysctl.conf  
 echo 'vm.overcommit\_memory=2' &gt;&gt; /etc/sysctl.conf  
 sysctl -p  
 ```

4\. \*\*监控增强\*\*：  
 ```bash  
 # 内存泄漏检测  
 apt-get install -y sysstat valgrind  
 sar -r 60 -o /var/log/sa/memlog &amp;  
 ```

\## 验证指标

优化后应达到：  
\- 内存使用率 &lt;90%  
\- Swap使用率 1-5%  
\- 缓存占比 &gt;20%  
\- 关键进程RSS ≈ Xmx + 30%

## 根据 `jcmd` 和 `jmap` 的输出分析，Tomcat 进程 (PID 31037) 存在严重的内存管理问题。以下是详细诊断和解决方案：

\---

\## \*\*1. 内存占用分析\*\*  
\### \*\*1.1 Native Memory 分布\*\*  
\- \*\*总 Reserved\*\*: 6,165,979 KB (~6.1GB)  
\- \*\*总 Committed\*\*: 4,780,715 KB (~4.78GB)  
   
| 内存区域 | Reserved (KB) | Committed (KB) | 问题点 |  
|------------------|--------------|---------------|--------|  
| Java Heap | 4,194,304 | 3,899,392 | 接近 Xmx 上限 (4GB) |  
| Class Metadata | 1,280,500 | 260,596 | \*\*MetaSpace 占用高\*\* |  
| Thread Stacks | 186,976 | 186,976 | \*\*179 个线程，栈占用过大\*\* |  
| Code Cache | 273,364 | 203,492 | \*\*JIT 编译代码缓存较大\*\* |  
| GC 相关 | 159,054 | 158,478 | G1GC 占用偏高 |  
| Symbol Storage | 44,112 | 44,112 | 字符串常量池较大 |

\### \*\*1.2 Heap 内存分析 (`jmap -histo`)\*\*  
\- \*\*Top 内存占用对象\*\*：  
\- `char\[\]` (`\[C`): \*\*379MB\*\*  
\- `byte\[\]` (`\[B`): \*\*171MB\*\*  
\- `String`: \*\*13MB\*\*  
\- `LinkedHashMap$Entry`: \*\*9.4MB\*\*  
\- `Object\[\]` (`\[Ljava.lang.Object;`): \*\*8.5MB\*\*

\*\*问题点\*\*：  
\- \*\*大量 `char\[\]` 和 `byte\[\]` 占用\*\*，可能是：  
\- \*\*未优化的字符串处理\*\*（如日志、JSON 解析）  
\- \*\*缓存未限制大小\*\*（如 HTTP Session 缓存）  
\- \*\*内存泄漏\*\*（如静态集合未清理）

\---

\## \*\*2. 问题诊断\*\*  
\### \*\*2.1 MetaSpace 占用高\*\*  
\- \*\*Class Metadata 占用 260MB\*\*，加载了 \*\*37,630 个类\*\*。  
\- \*\*可能原因\*\*：  
\- \*\*动态类加载\*\*（如 JSP 编译、反射生成类）  
\- \*\*未限制 MetaSpace\*\*（默认无上限，可能无限增长）

\### \*\*2.2 线程栈占用过大\*\*  
\- \*\*179 个线程\*\*，每个线程栈占用约 \*\*1MB\*\*（默认 `-Xss1m`）。  
\- \*\*总栈内存\*\*：179 × 1MB ≈ \*\*179MB\*\*。  
\- \*\*可能原因\*\*：  
\- \*\*线程池配置过大\*\*（如 Tomcat `maxThreads=200`）  
\- \*\*线程泄漏\*\*（如未正确关闭线程池）

\### \*\*2.3 Direct Memory 泄漏\*\*  
\- `-XX:MaxDirectMemorySize=1024m` 设置了 \*\*1GB 堆外内存\*\*。  
\- \*\*NIO Buffer 或 Netty 等框架可能泄漏 Direct Memory\*\*。

\---

\## \*\*3. 优化方案\*\*  
\### \*\*3.1 JVM 参数优化\*\*  
```diff  
\# 修改 Tomcat 启动参数（catalina.sh 或 setenv.sh）  
\- -Xms2048m -Xmx4096m -XX:MaxDirectMemorySize=1024m  
\+ -Xms2048m -Xmx4096m   
\+ -XX:MaxDirectMemorySize=512m # 限制 Direct Memory  
\+ -XX:MaxMetaspaceSize=256m # 避免 MetaSpace 无限增长  
\+ -Xss256k # 减少线程栈大小  
\+ -XX:+UseG1GC # 改用 G1 垃圾回收器  
\+ -XX:NativeMemoryTracking=detail # 持续监控 Native Memory  
```

\### \*\*3.2 检查线程泄漏\*\*  
```bash  
\# 查看 Tomcat 线程数  
ps -L -p 31037 | wc -l

\# 查看线程栈  
jstack 31037 &gt; /tmp/jstack.log  
grep "pool-" /tmp/jstack.log | wc -l  
```  
\*\*优化建议\*\*：  
\- 调整 `server.xml` 中的 `maxThreads`（如从 200 → 100）。  
\- 检查是否有自定义线程池未正确关闭。

\### \*\*3.3 检查 MetaSpace 泄漏\*\*  
```bash  
\# 查看加载的类数量  
jcmd 31037 GC.class\_stats | head -20

\# 检查是否有动态生成的类  
jcmd 31037 VM.class\_hierarchy | grep Generated  
```  
\*\*优化建议\*\*：  
\- 限制 `-XX:MaxMetaspaceSize=256m`，防止无限增长。  
\- 检查是否有频繁的 JSP 热部署。

\### \*\*3.4 检查 Direct Memory 泄漏\*\*  
```bash  
\# 查看 NIO Buffer 分配  
jcmd 31037 VM.native\_memory detail | grep "Java Heap\\|Direct"  
```  
\*\*优化建议\*\*：  
\- 如果使用 Netty，检查 `ByteBuf` 是否正确释放。  
\- 限制 `-XX:MaxDirectMemorySize=512m`。

\---

\## \*\*4. 监控与验证\*\*  
\### \*\*4.1 监控内存变化\*\*  
```bash  
\# 每 5 秒打印内存使用  
watch -n 5 'ps -p 31037 -o %mem,rss,comm &amp;&amp; jcmd 31037 VM.native\_memory summary'  
```

\### \*\*4.2 触发 Full GC 观察内存回收\*\*  
```bash  
\# 手动触发 Full GC  
jcmd 31037 GC.run  
```

\### \*\*4.3 检查 OOM 风险\*\*  
```bash  
\# 查看 GC 日志（需开启 -Xloggc）  
tail -f /usr/local/apache-tomcat-8.0.39/logs/gc.log  
```

\---

\## \*\*5. 结论\*\*  
\- \*\*主要问题\*\*：  
\- \*\*MetaSpace 占用高\*\*（260MB），需限制 `MaxMetaspaceSize`。  
\- \*\*线程栈占用大\*\*（179MB），可减少 `-Xss` 或 `maxThreads`。  
\- \*\*Direct Memory 可能泄漏\*\*，需限制并检查 NIO Buffer。  
\- \*\*Heap 内 `char\[\]` 和 `byte\[\]` 占用高\*\*，需检查缓存和字符串处理逻辑。

\- \*\*优化后预期\*\*：  
\- \*\*RSS 内存下降 1-2GB\*\*（从 5.26GB → 3.5GB 左右）。  
\- \*\*减少 OOM 风险\*\*，提高系统稳定性。

建议在 \*\*低峰期重启 Tomcat\*\* 并应用新 JVM 参数，持续监控内存变化。

root@shuncom-01:/usr/local/apache-tomcat-8.0.39/bin# jcmd 31037 VM.native\_memory detail | grep "Java Heap\\|Direct"  
\- Java Heap (reserved=4194304KB, committed=3646976KB)  
\[0x00000006c0000000 - 0x00000007c0000000\] reserved 4194304KB for Java Heap from  
root@shuncom-01:/usr/local/apache-tomcat-8.0.39/bin# jcmd 31037 VM.class\_hierarchy | grep Generated   
java.io.IOException: Command failed in target VM  
 at sun.tools.attach.LinuxVirtualMachine.execute(LinuxVirtualMachine.java:224)  
 at sun.tools.attach.HotSpotVirtualMachine.executeCommand(HotSpotVirtualMachine.java:217)  
 at sun.tools.attach.HotSpotVirtualMachine.executeJCmd(HotSpotVirtualMachine.java:199)  
 at sun.tools.jcmd.JCmd.executeCommandForPid(JCmd.java:150)  
 at sun.tools.jcmd.JCmd.main(JCmd.java:129)  
root@shuncom-01:/usr/local/apache-tomcat-8.0.39/bin# jcmd 31037 GC.class\_stats | head -20   
31037:  
GC.class\_stats command requires -XX:+UnlockDiagnosticVMOptions

## <span style="color: rgb(64, 64, 64); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 16.002px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">根据最新的诊断结果，以下是针对 Tomcat 进程 (PID 31037) 的 </span>**最终优化方案**<span style="color: rgb(64, 64, 64); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 16.002px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> 和 </span>**关键结论**<span style="color: rgb(64, 64, 64); font-family: quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', Ubuntu, Cantarell, 'Helvetica Neue', Oxygen, 'Open Sans', sans-serif; font-size: 16.002px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">：</span>

\---

\## \*\*1. 关键发现\*\*  
\### \*\*1.1 Java Heap 使用情况\*\*  
\- \*\*配置\*\*：`-Xmx4096m` (4GB)  
\- \*\*实际 Commit\*\*：3.64GB（接近上限，存在 OOM 风险）  
\- \*\*问题\*\*：Heap 使用率过高，可能由于：  
\- 缓存未限制（如 HTTP Session 缓存）  
\- 大对象未及时释放（如 `char\[\]` 占 379MB）

\### \*\*1.2 Direct Memory\*\*  
\- \*\*未检测到 Direct Buffer 泄漏\*\*（`grep "Direct"` 无输出）  
\- 但 `-XX:MaxDirectMemorySize=1024m` 仍可能被 NIO 或第三方库（如 Netty）使用，需保持限制。

\### \*\*1.3 MetaSpace\*\*  
\- \*\*无法直接获取类统计\*\*（需 `-XX:+UnlockDiagnosticVMOptions`）  
\- \*\*间接证据\*\*：`Class Metadata` 占用 260MB，加载 37,630 个类，可能存在：  
\- 动态类加载（如 JSP 编译）  
\- 反射生成类（如 Spring AOP）

\---

\## \*\*2. 最终优化方案\*\*  
\### \*\*2.1 修改 Tomcat JVM 参数\*\*  
编辑 `catalina.sh` 或 `setenv.sh`，替换原有参数：  
```bash  
JAVA\_OPTS="  
 -Xms2048m -Xmx4096m  
 -XX:MaxMetaspaceSize=256m # 限制 MetaSpace 增长  
 -XX:MaxDirectMemorySize=512m # 限制堆外内存  
 -Xss256k # 减少线程栈大小  
 -XX:+UseG1GC # 改用 G1 垃圾回收器  
 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/usr/local/apache-tomcat-8.0.39/logs/gc.log  
 -XX:NativeMemoryTracking=detail # 持续监控 Native Memory  
"  
```

\### \*\*2.2 检查线程泄漏\*\*  
```bash  
\# 查看线程数  
ps -L -p 31037 | wc -l

\# 如果线程数 &gt; 150，调整 Tomcat 的 maxThreads  
sed -i 's/maxThreads="200"/maxThreads="100"/g' /usr/local/apache-tomcat-8.0.39/conf/server.xml  
```

\### \*\*2.3 检查缓存问题\*\*  
```bash  
\# 检查 Tomcat 的 Session 超时设置（单位：分钟）  
grep -A 1 "&lt;session-config&gt;" /usr/local/apache-tomcat-8.0.39/conf/web.xml  
```  
\*\*建议\*\*：确保 `&lt;session-timeout&gt;30&lt;/session-timeout&gt;`（默认 30 分钟）。

\### \*\*2.4 重启 Tomcat 并监控\*\*  
```bash  
\# 重启 Tomcat  
/usr/local/apache-tomcat-8.0.39/bin/shutdown.sh  
/usr/local/apache-tomcat-8.0.39/bin/startup.sh

\# 监控内存变化  
watch -n 5 'ps -p $(pgrep -f "Bootstrap start") -o %mem,rss,comm'  
```

\---

\## \*\*3. 预期优化效果\*\*  
| 指标 | 优化前 | 优化后目标 |  
|---------------------|----------------------|--------------------|  
| \*\*RSS 内存\*\* | 5.26GB | 3.5-4GB |  
| \*\*线程栈内存\*\* | 179MB (179 threads) | 25MB (100 threads) |  
| \*\*MetaSpace\*\* | 260MB (无限制) | ≤256MB |  
| \*\*Direct Memory\*\* | 可能泄漏 (1GB) | ≤512MB |

\---

\## \*\*4. 后续维护建议\*\*  
1\. \*\*定期检查 GC 日志\*\*：  
 ```bash  
 tail -f /usr/local/apache-tomcat-8.0.39/logs/gc.log | grep -E "Full GC|OOM"  
 ```

2\. \*\*设置内存报警\*\*（如 Prometheus + Grafana）：  
\- 报警规则：`RSS &gt; 3.5GB` 持续 10 分钟。

3\. \*\*升级 JDK\*\*：  
\- 当前使用 \*\*JDK 1.8.0\_111\*\*（旧版，存在已知内存问题），建议升级到 \*\*1.8.0\_381+\*\*。

\---

\## \*\*5. 紧急情况处理\*\*  
如果优化后仍出现 \*\*OOM\*\*：  
1\. \*\*快速 Dump 内存\*\*：  
 ```bash  
 jmap -dump:live,format=b,file=/tmp/tomcat\_heap.hprof 31037  
 ```  
2\. \*\*分析内存泄漏\*\*：  
\- 使用 \[Eclipse MAT\](https://www.eclipse.org/mat/) 分析 `tomcat\_heap.hprof`。

\---

\*\*结论\*\*：通过限制 MetaSpace、减少线程栈大小、优化 JVM 参数，\*\*预计可降低 1-2GB 内存占用\*\*，显著减少 OOM 风险。建议在低峰期实施并持续监控。

# 免费 SSL 域名证书申请

你要的其实就是**免费 SSL 域名证书**，能直接配到 **Nginx** 上，并且可以**下载到本地**（而不是像一些托管服务只给 HTTPS 反代）。  
我帮你分成两种方式：**自动申请**（推荐）和 **手动申请**（方便离线保存）。

---

## **一、自动申请（Let’s Encrypt + Certbot，适合可联网服务器）**

Let’s Encrypt 提供免费 DV 证书，有效期 **90 天**，支持自动续期。

### 1. 安装 Certbot

Ubuntu / Debian：

```bash
sudo apt update
sudo apt install certbot python3-certbot-nginx -y

```

### 2. 申请证书

假设你的域名是 **example.com**，且已经解析到服务器 IP：

```bash
sudo certbot --nginx -d example.com -d www.example.com

```

Certbot 会自动：

- 验证域名
- 生成 `/etc/letsencrypt/live/example.com/fullchain.pem` 和 `privkey.pem`
- 修改 Nginx 配置加载证书

### 3. Nginx 配置示例

```nginx
server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        root /var/www/html;
        index index.html;
    }
}

```

### 4. 自动续期

```bash
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

```

---

## **二、手动申请（可下载保存 Nginx 格式证书）**

如果你想在本地生成并保存证书文件（方便迁移到其他服务器），可以用 **acme.sh**。

### 1. 安装 acme.sh

```bash
curl https://get.acme.sh | sh
source ~/.bashrc

```

### 2. 使用 DNS 验证申请（适合不能开放 80/443 的环境）

以阿里云为例（需要有 API Key）：

```bash
export Ali_Key="你的阿里云AccessKeyId"
export Ali_Secret="你的阿里云AccessKeySecret"

acme.sh --issue --dns dns_ali -d example.com -d www.example.com

```

### 3. 转换成 Nginx 可用格式并下载

```bash
acme.sh --install-cert -d example.com \
--key-file /root/cert/example.com.key \
--fullchain-file /root/cert/example.com.crt \
--reloadcmd "nginx -s reload"

```

生成后 `/root/cert/example.com.key` 和 `/root/cert/example.com.crt` 就可以打包下载了。

---

## **三、注意事项**

- 免费证书一般 **90 天有效**，需要续期（acme.sh 可自动续期）。
- 如果是公司内部测试，可以用自签证书（但浏览器会提示不安全）。
- 申请时**域名必须已解析到你的服务器**，否则验证失败。

---

## 四、在服务器未开放80和443端口的情况下，仍然可以通过以下几种方式申请并下载免费的SSL域名证书：

\---

\### \*\*1. 使用DNS验证方式申请证书\*\*  
DNS验证方式不需要开放80或443端口，而是通过添加DNS TXT记录来验证域名所有权。

\#### \*\*方法一：使用acme.sh（推荐）\*\*  
1\. \*\*安装acme.sh\*\*   
 ```bash  
 curl https://get.acme.sh | sh  
 ```  
2\. \*\*申请证书（以阿里云DNS为例）\*\*   
 ```bash  
 export Ali\_Key="你的AccessKey"  
 export Ali\_Secret="你的AccessSecret"  
 acme.sh --issue --dns dns\_ali -d example.com -d \*.example.com  
 ```  
3\. \*\*证书存储位置\*\*   
 证书默认存放在 `~/.acme.sh/example.com/` 目录下，包含 `fullchain.cer` 和 `example.com.key` 文件。

\#### \*\*方法二：使用CertBot\*\*  
1\. \*\*安装CertBot\*\*   
 ```bash  
 sudo apt install certbot  
 ```  
2\. \*\*手动DNS验证\*\*   
 ```bash  
 certbot certonly --manual --preferred-challenge dns -d example.com  
 ```  
 按照提示添加TXT记录 `\_acme-challenge.example.com`，等待解析生效后继续。

\---

\### \*\*2. 使用邮箱验证（适用于ZeroSSL）\*\*  
如果无法使用DNS验证，可以尝试通过邮箱验证：  
1\. \*\*在ZeroSSL申请证书\*\*   
\- 访问 \[ZeroSSL\](https://zerossl.com/) 并选择邮箱验证。  
2\. \*\*在服务器上搭建Postfix邮件服务\*\*   
\- 接收验证邮件（如 `admin@example.com`）以完成验证。

\---

\### \*\*3. 使用JoySSL等支持非标准端口的CA\*\*  
某些CA（如JoySSL）支持通过22端口（SSH）进行验证：  
1\. \*\*访问JoySSL官网\*\*   
\- 选择IP或域名证书类型。  
2\. \*\*通过SSH验证服务器所有权\*\*   
\- 提供服务器访问权限，CA通过22端口完成验证。

\---

\### \*\*4. 使用acme-redirect（Rust工具）\*\*  
1\. \*\*安装acme-redirect\*\*   
 ```bash  
 git clone https://github.com/kpcyrd/acme-redirect.git  
 cd acme-redirect &amp;&amp; cargo build --release  
 ```  
2\. \*\*配置并运行\*\*   
\- 修改 `/etc/acme-redirect.d/example.com.conf`，启动服务：  
 ```bash  
 systemctl enable --now acme-redirect  
 ```  
\- 证书存放在 `/var/lib/acme-redirect/live/example.com/`。

\---

\### \*\*5. 自动续期\*\*  
\- \*\*acme.sh\*\*：默认自动续期，无需额外操作。  
\- \*\*CertBot\*\*：需手动设置cron任务：  
 ```bash  
 30 0 \* \* 1 certbot renew --manual-auth-hook /path/to/dns-update-script.sh  
 ```

\---

\### \*\*总结\*\*  
| 方法 | 适用场景 | 工具 | 验证方式 |  
|------|---------|------|---------|  
| \*\*DNS验证\*\* | 有DNS管理权限 | acme.sh / CertBot | 添加TXT记录 |  
| \*\*邮箱验证\*\* | 无80/443端口 | ZeroSSL | 邮件接收验证 |  
| \*\*SSH验证\*\* | 服务器可SSH访问 | JoySSL | 22端口验证 |  
| \*\*acme-redirect\*\* | 需临时占用80端口 | acme-redirect | HTTP验证 |

选择最适合你的方式，确保域名解析正确后即可获取证书。

## 五、你的 DNS 验证失败了，错误原因是 \*\*NXDOMAIN\*\*，这意味着 Let's Encrypt 无法找到 `\_acme-challenge.bost.lesrec.cn` 的 TXT 记录。 

\### \*\*解决方法\*\*  
\#### \*\*1. 检查 DNS 记录是否已正确添加\*\*  
\- 进入你的 DNS 管理控制台（如阿里云、腾讯云、Cloudflare 等）。  
\- 添加一条 \*\*TXT 记录\*\*：  
\- \*\*主机名\*\*：`\_acme-challenge.bost`（或 `\_acme-challenge`，取决于你的 DNS 提供商）  
\- \*\*记录值\*\*：`a0La5vEuhlg0Omqo7U-vS00\_mUG40kjXgOZCKq0y7dw`（CertBot 提供的值）  
\- \*\*TTL\*\*：默认即可（如 600 秒）。

\#### \*\*2. 等待 DNS 生效\*\*  
DNS 记录可能需要 \*\*几分钟到几小时\*\* 才能全球生效。你可以使用以下命令检查是否生效：  
```bash  
dig TXT \_acme-challenge.bost.lesrec.cn +short  
```  
或：  
```bash  
nslookup -type=TXT \_acme-challenge.bost.lesrec.cn  
```  
如果返回正确的 TXT 值，说明 DNS 已生效。

\#### \*\*3. 重新运行 CertBot\*\*  
```bash  
certbot certonly --manual --preferred-challenge dns -d bost.lesrec.cn  
```  
再次输入 `Y` 并等待验证。

\---

\### \*\*常见问题排查\*\*  
1\. \*\*DNS 记录格式错误\*\*   
\- 确保主机名是 `\_acme-challenge.bost`（而不是 `\_acme-challenge.bost.lesrec.cn`）。  
\- 某些 DNS 提供商（如 Cloudflare）会自动补全域名，你可能只需要输入 `\_acme-challenge`。

2\. \*\*DNS 缓存问题\*\*   
\- 使用 `dig @8.8.8.8 TXT \_acme-challenge.bost.lesrec.cn` 检查 Google DNS 是否已更新。  
\- 如果本地 DNS 缓存未更新，可以尝试 `systemd-resolve --flush-caches`（Linux）。

3\. \*\*CertBot 超时\*\*   
\- Let's Encrypt 默认等待 \*\*30 秒\*\*，如果 DNS 传播较慢，可以手动验证后再继续：  
 ```bash  
 certbot certonly --manual --preferred-challenge dns -d bost.lesrec.cn --manual-auth-hook /path/to/dns-update-script.sh --manual-cleanup-hook /path/to/dns-cleanup-script.sh  
 ```

4\. \*\*使用 acme.sh 自动 DNS 验证（推荐）\*\*   
\- 如果你的 DNS 提供商支持 API（如阿里云、Cloudflare），可以使用 `acme.sh` 自动完成验证：  
 ```bash  
 export Ali\_Key="你的AccessKey"  
 export Ali\_Secret="你的AccessSecret"  
 acme.sh --issue --dns dns\_ali -d bost.lesrec.cn  
 ```

\---

\### \*\*成功后的证书位置\*\*  
\- CertBot 默认存放路径：  
\- \*\*证书\*\*：`/etc/letsencrypt/live/bost.lesrec.cn/fullchain.pem`  
\- \*\*私钥\*\*：`/etc/letsencrypt/live/bost.lesrec.cn/privkey.pem`

如果仍然失败，请提供 `dig TXT \_acme-challenge.bost.lesrec.cn` 的输出，我可以帮你进一步排查！

## 六，免费申请SSL域名证书的网站

如果你的服务器 **80/443 端口都不开放**，就不能用 Let’s Encrypt 的 HTTP 验证方式，只能用 **DNS 验证** 来手动申请 SSL 证书，这样证书签发过程完全不依赖服务器的 Web 端口。

我给你分几种 **手动申请免费 SSL** 的方法（都可以直接下载证书，Nginx 格式 `.crt` + `.key`）：

---

## **1. ZeroSSL（手动 DNS 验证）**

- 地址： [https://zerossl.com](https://zerossl.com/)
- 优点：
    
    
    - 免费 90 天 DV 证书
    - 可直接生成 `.crt` 和 `.key` 文件
    - 支持手动 DNS 验证
- 步骤：
    
    
    1. 注册 ZeroSSL 账号。
    2. 新建证书 → 输入域名 → 选择 **DNS (CNAME) 验证**。
    3. 按提示在域名解析中添加 TXT 或 CNAME 记录。
    4. 验证完成后下载证书（可选 Nginx 格式）。
    5. 上传到服务器 `/etc/nginx/ssl`，配置 Nginx：
        
        ```nginx
        ssl_certificate /etc/nginx/ssl/example.com.crt;
        ssl_certificate_key /etc/nginx/ssl/example.com.key;
        
        ```

---

## **2. Buypass Go SSL（免费 180 天证书）**

- 地址： [https://www.buypass.com/ssl/resources/go-ssl](https://www.buypass.com/ssl/resources/go-ssl)
- 优点：
    
    
    - 免费半年有效期
    - 支持 ACME 协议，可离线生成
    - DNS 验证支持
- 步骤：
    
    
    1. 使用 [acme.sh](https://acme.sh/) 或 Certbot，设置验证方式为 `--dns`。
    2. 生成证书并保存 `.crt`、`.key`。

---

## **3. Let’s Encrypt + acme.sh（DNS 验证，离线保存）**

如果你不想依赖网页操作，可以用 acme.sh，本地生成后把证书上传到服务器。

### 安装 acme.sh

```bash
curl https://get.acme.sh | sh
source ~/.bashrc

```

### 申请证书（以阿里云 DNS 为例）

```bash
export Ali_Key="你的阿里云AccessKeyId"
export Ali_Secret="你的阿里云AccessKeySecret"

acme.sh --issue --dns dns_ali -d example.com -d www.example.com

```

### 保存为 Nginx 格式（本地可下载）

```bash
acme.sh --install-cert -d example.com \
--key-file ./example.com.key \
--fullchain-file ./example.com.crt

```

生成的 `.crt` 和 `.key` 就可以直接配到 Nginx。

---

## **4. SSL.com（免费 90 天试用证书）**

- 地址： [https://www.ssl.com/certificates/free](https://www.ssl.com/certificates/free)
- 手动上传 CSR，选择 DNS 验证，审核后下载证书。

---

✅ **总结**

- 你这种 **不开 80/443** 的情况，最佳方案是 **acme.sh + DNS 验证** 或 **ZeroSSL 手动签发**。
- DNS 验证是通过添加 TXT/CNAME 记录证明域名所有权，不依赖服务器端口，非常适合内网或防火墙环境。

---

如果你需要，我可以帮你做一个**本地一键脚本**，用 `acme.sh` 自动生成 **Nginx 格式证书**，全程只需要你填一次域名和 DNS API Key，90 天后也能自动续期，不用手动去 ZeroSSL。  
这样你连网页都不用开，全程命令行离线可操作。

你要我帮你写这个脚本吗？这样申请下来直接就是 `.crt` + `.key`。

# EasyTier 工具部署教程

下面是一个 **EasyTier 工具部署教程**，帮助你快速上手这款去中心化 SD-WAN 工具：

---

## EasyTier 简介

- **去中心化网络**：所有节点平等，无需中心服务器，只要能连通任意节点即可加入网络。([GitHub](https://github.com/EasyTier/EasyTier?utm_source=chatgpt.com "EasyTier/EasyTier: A simple, decentralized mesh VPN with ... - GitHub"), [EasyTier](https://easytier.rs/en/guide/introduction.html?utm_source=chatgpt.com "Introduction - A Simple, Secure, Decentralized SD-WAN Solution"))
- **高安全性**：内置 AES-GCM 或 WireGuard 加密，保护通信安全。([GitHub](https://github.com/EasyTier/EasyTier?utm_source=chatgpt.com "EasyTier/EasyTier: A simple, decentralized mesh VPN with ... - GitHub"), [EasyTier](https://easytier.rs/en/guide/introduction.html?utm_source=chatgpt.com "Introduction - A Simple, Secure, Decentralized SD-WAN Solution"))
- **支持跨平台**：可用于 Windows、macOS、Linux、FreeBSD、Android 等环境，覆盖多种架构如 x86、ARM、MIPS。([GitHub](https://github.com/EasyTier/EasyTier?utm_source=chatgpt.com "EasyTier/EasyTier: A simple, decentralized mesh VPN with ... - GitHub"), [EasyTier](https://easytier.rs/en/guide/introduction.html?utm_source=chatgpt.com "Introduction - A Simple, Secure, Decentralized SD-WAN Solution"))
- **出色的 NAT 穿透能力**：支持 UDP 和 IPv6，并兼容 NAT4-NAT4 环境。([GitHub](https://github.com/EasyTier/EasyTier?utm_source=chatgpt.com "EasyTier/EasyTier: A simple, decentralized mesh VPN with ... - GitHub"), [EasyTier](https://easytier.rs/en/guide/introduction.html?utm_source=chatgpt.com "Introduction - A Simple, Secure, Decentralized SD-WAN Solution"))
- **高性能网络**：支持 TCP/UDP/WSS/WG 协议，使用零拷贝、KCP/QUIC 优化链接质量。([GitHub](https://github.com/EasyTier/EasyTier?utm_source=chatgpt.com "EasyTier/EasyTier: A simple, decentralized mesh VPN with ... - GitHub"), [EasyTier](https://easytier.rs/en/guide/introduction.html?utm_source=chatgpt.com "Introduction - A Simple, Secure, Decentralized SD-WAN Solution"))

---

## 部署方式一览

### 使用 CLI（命令行工具）

- 从官方网站或 GitHub 下载适合你系统的可执行包（无需安装即可运行）。([EasyTier](https://easytier.rs/en/guide/installation.html?utm_source=chatgpt.com "Installation (Command Line Program) | EasyTier"))
- 解压后直接测试版本：
    
    ```bash
    ./easytier-core --version
    
    ```
- 参考文档进行配置（如网络名称、密钥、连接方式）。([GitHub](https://raw.githubusercontent.com/EasyTier/EasyTier/main/README.md?utm_source=chatgpt.com "https://raw.githubusercontent.com/EasyTier/EasyTie..."))

### 使用 Docker 部署

- 从 Docker Hub 获取镜像并运行：
    
    ```bash
    docker pull easytier/easytier:latest
    docker run -d --privileged --network host easytier/easytier:latest
    
    ```
- 对国内用户也提供 DaoCloud 镜像：`m.daocloud.io/docker.io/easytier/easytier:latest`。([EasyTier](https://easytier.rs/en/guide/installation.html?utm_source=chatgpt.com "Installation (Command Line Program) | EasyTier"))

### 使用 Docker Compose 部署

- 可通过 `docker-compose.yml` 集成部署（配置示例见文档），便于自动更新与管理。([EasyTier](https://easytier.rs/en/guide/installation.html?utm_source=chatgpt.com "Installation (Command Line Program) | EasyTier"))

---

## 快速部署示例（命令行）

以下为一个基本的 CLI 快速部署流程，搭建一个去中心化网络：

1. **启动主节点（Node A）**：
    
    ```bash
    sudo easytier-core -i 10.144.144.1
    
    ```
2. **启动连接节点（Node B）**：
    
    ```bash
    sudo easytier-core -i 10.144.144.2 -p udp://第一节点公网IP:11010
    
    ```
3. **网络状态查看命令**：
    
    ```bash
    easytier-cli peer    # 查看已连接节点
    easytier-cli route   # 查看路由信息
    easytier-cli node    # 查看本地节点信息
    
    ```

([GitHub](https://raw.githubusercontent.com/EasyTier/EasyTier/main/README.md?utm_source=chatgpt.com "https://raw.githubusercontent.com/EasyTier/EasyTie..."))

4. **连接更多节点**：
    
    ```bash
    sudo easytier-core -i 10.144.144.3 -p udp://任意已在线节点公网IP:11010
    
    ```

---

## One-Click 注册服务（开机自启）

官方文档还提供了如何将 EasyTier 注册为系统服务，方便自动启动与运维管理。你可以参考“一键注册服务”指南进一步配置。([GitHub](https://raw.githubusercontent.com/EasyTier/EasyTier/main/README.md?utm_source=chatgpt.com "https://raw.githubusercontent.com/EasyTier/EasyTie..."))

---

## 总结

- **EasyTier 是一个轻量、易用、安全、去中心化的 SD-WAN 工具**，适合远程办公、跨网互通、游戏联机、IoT 互联等多种场景。
- 支持多种部署方式：**CLI、Docker、Docker Compose**，便于集成与扩展。
- 加密、安全、支持 NAT 穿透与智能路由，是构建虚拟网络的一种高效方式。

---

下面是为 **Ubuntu 20.04** 系统准备的 **Docker Compose 部署 EasyTier** 的完整教程，帮你快速搭建去中心化虚拟网络节点。

---

## 一、前期准备

### 1. 安装 Docker（如尚未安装）

首先，推荐使用 Docker 官方 apt 仓库进行安装，以确保稳定性与可更新性：  
参考 Docker 官方指南：

```bash
# 安装依赖
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release

# 添加 GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# 添加 Docker 仓库
echo \
 "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
 https://download.docker.com/linux/ubuntu \
 $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

```

安装完成后，可使用 `sudo docker run hello-world` 验证 Docker 是否正常运行。([Docker Documentation](https://docs.docker.com/engine/install/ubuntu/?utm_source=chatgpt.com "Install Docker Engine on Ubuntu"))

---

### 2. 安装 Docker Compose

推荐方式为直接从 GitHub 官方 Release 页面下载最新版：

```bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" \
 -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

```

这会安装 `docker-compose` （v1）工具，以便使用 `docker-compose up` 方式启动服务。([DigitalOcean](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04?utm_source=chatgpt.com "How to Install Docker Compose on Ubuntu (Step-by-Step Guide)"), [LinuxConfig](https://linuxconfig.org/how-to-install-docker-compose-on-ubuntu-20-04-focal-fossa-linux?utm_source=chatgpt.com "How to install docker-compose on Ubuntu 20.04 Focal Fossa Linux"))

---

## 二、配置 Docker Compose 部署 EasyTier

### 1. 创建项目目录与 `docker-compose.yml`

```bash
mkdir ~/easytier
cd ~/easytier

```

新建 `docker-compose.yml`：

```yaml
version: "3.8"

services:
  easytier:
    image: easytier/easytier:latest
    container_name: easytier
    network_mode: "host"
    privileged: true
    restart: unless-stopped
    command: >
      --network-name your_net_name
      --network-secret your_secret
      -p udp://public.easytier.cn:11010

```

说明：

- 使用 `host` 网络模式来允许 EasyTier 直接访问宿主机网络。
- `--network-name` 和 `--network-secret` 用于标识同一个 EasyTier 虚拟网络。
- `-p udp://public.easytier.cn:11010` 使用 EasyTier 提供的公共中继节点进行 NAT 穿透（适用于无公网 IP 场景）。([GitHub](https://raw.githubusercontent.com/EasyTier/EasyTier/main/README.md?utm_source=chatgpt.com "https://raw.githubusercontent.com/EasyTier/EasyTie..."), [Docs.rs](https://docs.rs/crate/easytier/latest/source/README_CN.md?utm_source=chatgpt.com "easytier 2.0.3 - Docs.rs"))

### 2. 启动服务

```bash
docker-compose up -d

```

后台启动 EasyTier 节点。

---

## 三、后续操作与验证

### 1. 查看 EasyTier 节点状态

容器运行后，进入容器或使用 CLI 工具查看当前节点连接情况（如容器内安装了 easytier-cli）：

```bash
docker exec -it easytier easytier-cli peer
docker exec -it easytier easytier-cli node
docker exec -it easytier easytier-cli route

```

如果没有 CLI，也可以进入容器运行：

```bash
docker exec -it easytier /bin/sh

```

并手动用 `easytier-cli` 查看。

---

### 2. 测试连通性

- 检查是否获得 EasyTier 虚拟 IP。
- 不同节点之间可以使用 `ping` 测试连接，例如：
    
    ```bash
    ping 10.x.x.y  # 已知另一个节点的虚拟 IP
    
    ```

---

## 四、完整流程总结

<table id="bkmrk-%E6%AD%A5%E9%AA%A4-%E6%93%8D%E4%BD%9C-1-%E5%AE%89%E8%A3%85-docker-%E5%BC%95%E6%93%8E"><thead><tr><th>步骤</th><th>操作</th></tr></thead><tbody><tr><td>1</td><td>安装 Docker 引擎</td></tr><tr><td>2</td><td>安装 Docker Compose</td></tr><tr><td>3</td><td>创建目录并写入 `docker-compose.yml`</td></tr><tr><td>4</td><td>设置好网络名称与密钥</td></tr><tr><td>5</td><td>启动 EasyTier 服务 `docker-compose up -d`</td></tr><tr><td>6</td><td>验证节点状态并测试连通性</td></tr></tbody></table>

---

若你希望我进一步帮你生成完整 Shell 自动化脚本（包括安装环境、生成配置、启动 Compose）或编写 systemd 服务实现开机自启，也可以告诉我，我可以帮你继续完善~~

希望这些步骤对你在 Ubuntu 20.04 上通过 Docker Compose 快速部署 EasyTier 有所帮助！

# HYPER-V虚拟机网卡因流量过高当机分析与解决方法

昨天2台有30万访问量的虚拟机网卡当机，执行ping命令发现有这个报错：

```  
ping: sendmsg: Operation not permitted  
```

结合“虚拟机网卡在流量高峰时才出现，重启后恢复”，问题大概率和 \*\*虚拟化平台的网卡/驱动/安全策略\*\* 有关，而不是简单的网络不通。常见原因有：

\---

\### 🔎 可能原因

1\. \*\*虚拟化平台的安全策略限制\*\*

 \* 比如 KVM、VMware、Hyper-V 的防火墙、vSwitch 安全规则触发，丢弃 ICMP 或阻止报文发送。  
 \* 当流量高峰时，虚拟交换机可能触发 \*\*端口安全（port security）\*\* 或 \*\*风暴控制（storm control）\*\*，直接禁止虚拟机继续发包。

2\. \*\*iptables/ebtables/SELinux/AppArmor 限制\*\*

 \* 在流量异常时，iptables 可能被防护脚本（比如 Fail2Ban、DDOS 防御）自动插入 DROP 规则，导致 ping 报错为 \*Operation not permitted\*。  
 \* `sendmsg: Operation not permitted` 一般是内核禁止应用层发包，而不是网络栈完全不可达。

3\. \*\*网卡驱动/队列过载\*\*

 \* virtio、e1000 等虚拟网卡驱动在高并发下容易队列塞满。  
 \* 如果 ring buffer 被填满，可能触发驱动错误，报文无法下发。

4\. \*\*ARP/邻居表溢出\*\*

 \* Linux 的 `neigh table` 在高并发时可能爆满，导致内核直接禁止发包。  
 \* 可以通过 `ip -s neigh` 或 `dmesg` 检查是否有 “neighbour table overflow”。

5\. \*\*内核资源耗尽 (socket buffer/netfilter conntrack)\*\*

 \* 高流量时，`net.ipv4.ip\_local\_port\_range`、`conntrack` 表、`net.core.somaxconn`、`netdev\_max\_backlog` 被用光，系统无法再发 ICMP 报文。  
 \* `sendmsg: Operation not permitted` 就是这种内核层拒绝。

\---

\### 🔧 建议排查步骤

1\. \*\*检查 dmesg / 内核日志\*\*

 ```bash  
 dmesg | grep -i denied  
 dmesg | grep -i neigh  
 ```

2\. \*\*确认防火墙\*\*

 ```bash  
 iptables -nvL  
 iptables -t raw -nvL  
 iptables -t mangle -nvL  
 ```

3\. \*\*查看 conntrack 是否溢出\*\*

 ```bash  
 cat /proc/sys/net/netfilter/nf\_conntrack\_count  
 cat /proc/sys/net/netfilter/nf\_conntrack\_max  
 ```

4\. \*\*检查邻居表是否爆满\*\*

 ```bash  
 ip -s neigh  
 ```

5\. \*\*检查虚拟机网卡驱动\*\*

 ```bash  
 ethtool -i eth0  
 ```

6\. \*\*提高内核资源参数（预防措施）\*\*  
 在 `/etc/sysctl.conf` 增加：

 ```conf  
 net.core.netdev\_max\_backlog = 8192  
 net.ipv4.neigh.default.gc\_thresh1 = 2048  
 net.ipv4.neigh.default.gc\_thresh2 = 4096  
 net.ipv4.neigh.default.gc\_thresh3 = 8192  
 net.netfilter.nf\_conntrack\_max = 262144  
 ```

 然后：

 ```bash  
 sysctl -p  
 ```

\---

⚠️ 你的情况 \*\*重启后恢复\*\*，说明资源是动态被耗尽/阻断的，不是永久配置问题。  
最可能的两个方向：

\* \*\*conntrack 表溢出\*\*（高峰时连接数过多，导致内核拒绝新连接/报文）。  
\* \*\*虚拟交换机风暴控制/限流\*\*（高峰时触发 vSwitch 安全机制）。

\---

要不要我帮你写一个 \*\*实时监控脚本\*\*（收集 conntrack、neigh table、带宽利用率、dmesg 关键字），这样下次再复现时，你能立刻知道是哪一块资源耗尽？

👌 收到，你这里的信息点挺关键：

1\. \*\*网卡驱动：hv\\\_netvsc\*\*

 \* 说明这台虚拟机是在 \*\*Hyper-V / Azure\*\* 上跑的。  
 \* `hv\_netvsc` 是 Hyper-V 的 paravirtualized 驱动，在高并发下偶尔会遇到队列溢出或转发 bug。  
 \* 一旦内部 ring buffer / channel 被占满，报文就会被丢弃，应用层收到的就是

 ```  
 ping: sendmsg: Operation not permitted  
 ```

 （这是 Linux 内核在无法下发数据时的报错，不是防火墙拒绝）。

2\. \*\*ARP/邻居表\*\*

 \* `ip -s neigh` 里没有看到 `FAILED`，大部分都是 `REACHABLE`，个别 `STALE` 正常。  
 \* 说明不是邻居表爆满。

3\. \*\*sysctl 配置\*\*

 \* 你已经把 `netdev\_max\_backlog`、`nf\_conntrack\_max`、`somaxconn` 等参数都调高了，说明连接跟 backlog 溢出的可能性已经大幅降低。  
 \* `nf\_conntrack\_max = 524288` 足够大，conntrack 不太会是瓶颈。

\---

\### 📌 综合分析

结合 \*\*虚拟机高峰时才触发\*\* + \*\*hv\\\_netvsc 驱动\*\*，大概率是：

\* \*\*Hyper-V vSwitch / hv\\\_netvsc 在高并发流量下丢包或阻止新报文\*\*。  
\* `sendmsg: Operation not permitted` 并不是权限问题，而是 \*\*驱动/内核层报文没法发出\*\*。  
\* 重启后恢复，是因为网卡队列 / vmbus 通道被清空，重新初始化了。

\---

\### 🔧 建议解决方案

1\. \*\*更新驱动 / 内核\*\*

 \* Ubuntu 20.04 的默认内核里，`hv\_netvsc` 有已知的稳定性 bug。  
 \* 建议升级到 \*\*5.15+ LTS 内核\*\*，或者在 Azure/Hyper-V 环境，直接用 \*\*最新的 cloud kernel\*\*。

2\. \*\*增加多队列\*\*

 \* 检查是否启用了多队列：

 ```bash  
 ethtool -l eth0  
 ```  
 \* 如果只有 1 队列，可以尝试增加：

 ```bash  
 ethtool -L eth0 combined 4  
 ```  
 \* 这样可以避免高并发时单队列被塞满。

3\. \*\*绕过 hv\\\_netvsc，用 SR-IOV / 合成网卡\*\*

 \* 如果平台支持，启用 \*\*SR-IOV\*\* 网卡直通，绕过 hv\\\_netvsc。  
 \* Azure 上是 \*\*Accelerated Networking\*\*，Hyper-V 上要在宿主机启用 SR-IOV。

4\. \*\*监控 vmbus 队列 / 网卡丢包情况\*\*

 \* 查看驱动统计：

 ```bash  
 ethtool -S eth0  
 ```

 看有没有 `dropped`, `errors`, `busy` 的计数。  
 \* 如果高峰时这些数暴增，就能确认是队列/驱动溢出。

\---

👉 总结：  
问题不是 Linux 配置不够，而是 \*\*Hyper-V 的 hv\\\_netvsc 驱动在高流量时掉坑\*\*。最有效的解决办法是 \*\*升级内核/驱动\*\* 或 \*\*改用 SR-IOV 网卡\*\*。

\---

要不要我帮你写一个小脚本，可以定时采集 `ethtool -S eth0` 和 `dmesg` 日志，把高峰期网卡丢包情况抓下来？这样你能确证是 \*\*hv\\\_netvsc driver bottleneck\*\*。

root@rulr-4:/var/log/sysstat# ethtool -S eth0  
NIC statistics:  
 tx\_scattered: 0  
 tx\_no\_memory: 0  
 tx\_no\_space: 0  
 tx\_too\_big: 0  
 tx\_busy: 0  
 tx\_send\_full: 1015  
 rx\_comp\_busy: 0  
 stop\_queue: 0  
 wake\_queue: 0  
 vf\_rx\_packets: 0  
 vf\_rx\_bytes: 0  
 vf\_tx\_packets: 0  
 vf\_tx\_bytes: 0  
 vf\_tx\_dropped: 0  
 tx\_queue\_0\_packets: 8786385  
 tx\_queue\_0\_bytes: 681809072  
 rx\_queue\_0\_packets: 10353578  
 rx\_queue\_0\_bytes: 783861988  
 tx\_queue\_1\_packets: 10558838  
 tx\_queue\_1\_bytes: 833217559  
 rx\_queue\_1\_packets: 10700441  
 rx\_queue\_1\_bytes: 807056969  
 tx\_queue\_2\_packets: 9822662  
 tx\_queue\_2\_bytes: 757879972  
 rx\_queue\_2\_packets: 11035513  
 rx\_queue\_2\_bytes: 835997833  
 tx\_queue\_3\_packets: 7852471  
 tx\_queue\_3\_bytes: 619014441

从你贴出的 \*\*`ethtool -S eth0`\*\* 输出看，有几个值得注意的指标：

```  
tx\_send\_full: 1015  
```

👉 这个计数器表示 \*\*发送队列已满（driver ring buffer full）\*\* 的次数，说明在流量高峰时，网卡的发送队列被打满，内核往驱动写包时被阻塞，导致出现你前面提到的 \*\*`ping: sendmsg: Operation not permitted`\*\* 问题。

其它计数器（如 `tx\_no\_memory`, `tx\_no\_space`, `rx\_comp\_busy`）都是 0，说明主要瓶颈出现在 \*\*发送队列拥塞\*\*。

\---

\### 可能原因

1\. \*\*虚拟化瓶颈\*\*

 \* 如果是虚拟机（KVM/VMware/Hyper-V 等），往往因为 vNIC 队列过小或虚拟交换机转发压力过大，容易在高流量时堆积。  
 \* 多队列（`tx\_queue\_x`）已经启用，但可能 \*\*分配的中断/CPU 核数不够\*\*，流量大时容易掉包。

2\. \*\*驱动/网卡队列太小\*\*

 \* 默认 ring buffer（`txqueuelen` 和 `ethtool -G` 设置的 ring size）过小，无法承受突发流量。

3\. \*\*宿主机/物理网卡瓶颈\*\*

 \* 如果虚拟机运行在物理机上，宿主机的物理网卡或 vSwitch 已经打满，也会反映到虚拟机侧。

\---

\### 建议排查 &amp; 优化

1\. \*\*查看当前队列深度\*\*

 ```bash  
 ethtool -g eth0  
 ```

 看 `TX` 和 `RX` ring buffer 默认值。很多虚拟网卡默认只有 256 或 512，很小。

2\. \*\*增大 ring buffer\*\*  
 比如调到 4096：

 ```bash  
 ethtool -G eth0 rx 4096 tx 4096  
 ```

 （注意部分 vNIC 可能不支持这么大，要看 `ethtool -g` 输出支持的 max 值）

3\. \*\*调整 txqueuelen\*\*

 ```bash  
 ifconfig eth0 txqueuelen 2000  
 ```

 或者用 ip 命令：

 ```bash  
 ip link set eth0 txqueuelen 2000  
 ```

4\. \*\*检查 CPU 中断绑定\*\*

 ```bash  
 cat /proc/interrupts | grep eth0  
 ```

 如果只落在一个 CPU 上，可以用 \*\*irqbalance\*\* 或手工 `smp\_affinity` 做中断多核分担。

5\. \*\*虚拟化层排查\*\*

 \* 如果是 KVM，确认 virtio-net 是否开了多队列（`mq=on`）。  
 \* VMware/Hyper-V 也要确认虚拟交换机性能参数，必要时开启 \*\*SR-IOV\*\* 或 \*\*DPDK\*\* 提升性能。

6\. \*\*监控宿主机带宽利用率\*\*  
 如果物理机本身已接近满速（比如 1Gbps 打满），虚拟机里当然会出现 send full。

\---

📌 总结：  
你的虚拟机 \*\*高峰时网络不通、`sendmsg: Operation not permitted`\*\*，结合 `tx\_send\_full: 1015`，几乎可以确认是 \*\*发送队列打满\*\*。  
首要优化手段：\*\*增大 ring buffer + txqueuelen\*\*，再确认虚拟化层是否支持多队列分流。

要不要我帮你写一份 \*\*Ubuntu20.04 下针对虚拟机网卡的优化脚本\*\*（自动调整 ring buffer、txqueuelen，并绑定多核中断）？

# tomcat&java异常排查思路

好的，在 Ubuntu 14.04 上排查 Tomcat 异常导致网页访问缓慢的问题，可以遵循以下步骤和命令。核心思路是：先定位是系统资源瓶颈还是应用内部问题，再深入 Java 应用内部。

\---

\### 排查思路流程图

```mermaid  
flowchart TD  
A\[Tomcat访问缓慢\] --&gt; B(系统级资源排查)  
A --&gt; C(应用级内部排查)

subgraph B\[第一步：系统资源排查\]  
 direction TB  
 B1\[CPU&lt;br&gt;top, vmstat\] --&gt; B2{资源异常？}  
 B3\[内存&lt;br&gt;free, vmstat\] --&gt; B2  
 B4\[磁盘I/O&lt;br&gt;iostat, iotop\] --&gt; B2  
 B5\[网络&lt;br&gt;netstat, ss\] --&gt; B2  
 B2 -- 是 --&gt; B6\[针对性地解决资源瓶颈\]  
end

subgraph C\[第二步：Java应用排查\]  
 direction TB  
 C1\[检查Tomcat日志&lt;br&gt;tail -f catalina.out\] --&gt; C2{发现异常错误？}  
 C2 -- 是 --&gt; C3\[根据错误日志解决\]  
 C2 -- 否 --&gt; C4\[检查线程状态&lt;br&gt;jstack\]  
 C4 --&gt; C5\[分析线程栈&lt;br&gt;查找阻塞、死锁、等待\]  
 C5 --&gt; C6\[发现可疑线程？\]  
 C6 -- 是 --&gt; C7\[结合代码分析问题\]  
end

B2 -- 否 --&gt; C  
C6 -- 否 --&gt; D\[第三步：性能分析\]  
D --&gt; D1\[生成Heap Dump&lt;br&gt;jmap\]  
D --&gt; D2\[分析内存使用&lt;br&gt;MAT, jhat\]  
D --&gt; D3\[找出内存泄漏或过度消耗\]  
```

\---

\### 第一步：系统级资源排查 (快速确认瓶颈)

首先排除服务器本身的资源瓶颈（CPU、内存、磁盘I/O）。

1\. \*\*综合性能查看 (`top` / `htop`)\*\*  
 ```bash  
 top  
 ```  
 \* \*\*看第一行 (load average):\*\* 1分钟、5分钟、15分钟的系统平均负载。如果值\*\*超过CPU核心数\*\*（例如4核机器负载长期&gt;4），说明系统负载过高。  
 \* \*\*看 %CPU:\*\* 检查 `%Cpu(s)` 这一行的 `%id`（空闲）是否很低，`%wa`（I/O等待）是否很高。高 `%wa` 表示磁盘是瓶颈。  
 \* \*\*看 %MEM:\*\* 向下看进程列表，按 `Shift + M` 按内存使用排序，找到 `java` 或 `tomcat` 进程，看其内存使用率是否异常高，接近分配的最大值。

2\. \*\*内存和Swap使用情况 (`free`)\*\*  
 ```bash  
 free -h  
 ```  
 \* 重点关注 `available` 列（Ubuntu 14.04 可能没有，就看 `free` 列）。如果 `free` 内存极少，并且 `swap` 的 `used` 在不断增加，说明物理内存不足，系统在使用交换分区，这会极大拖慢速度。

3\. \*\*磁盘I/O状态 (`iostat`, `iotop`)\*\*  
 ```bash  
 # 需要安装 sysstat 包: sudo apt-get install sysstat  
 iostat -xm 2

 # 需要安装 iotop 并需要root权限: sudo apt-get install iotop &amp;&amp; sudo iotop  
 ```  
 \* 查看 `%util` 列，如果持续接近100%，表示磁盘I/O非常繁忙。  
 \* 查看 `await` 列，表示I/O操作的平均等待时间（毫秒），如果很高（例如&gt;100ms），说明磁盘响应慢。

4\. \*\*网络连接检查 (`netstat`, `ss`)\*\*  
 ```bash  
 # 查看Tomcat连接数（ESTABLISHED状态）  
 netstat -natp | grep :8080 | grep ESTABLISHED | wc -l

 # 或者使用更快的 ss 命令  
 ss -t sport = :8080 state established | wc -l  
 ```  
 \* 连接数过多可能耗尽线程池资源。

\---

\### 第二步：Java应用内部排查 (核心步骤)

如果系统资源正常，问题很可能出在应用本身：GC、死锁、阻塞、内存泄漏等。

1\. \*\*查看Tomcat日志\*\*  
 ```bash  
 # 进入Tomcat的logs目录，查看最新的catalina.out和localhost日志  
 cd /path/to/tomcat/logs  
 tail -f catalina.out  
 grep -i "exception" catalina.out  
 ```  
 \* \*\*首先检查有无明显的异常堆栈信息\*\*，如 `OutOfMemoryError`, `NullPointerException`, 数据库连接超时等。

2\. \*\*查找Java进程PID\*\*  
 ```bash  
 # 通常Tomcat的进程名是java  
 ps -ef | grep java  
 # 或  
 jps -l  
 ```  
 \* 找到对应的Tomcat进程的PID（例如 `12345`）。

3\. \*\*检查GC情况 (`jstat`) - 非常有用\*\*  
 ```bash  
 # 需要JDK工具，假设PID为12345  
 jstat -gcutil 12345 2s  
 ```  
 \* \*\*输出列解读:\*\* `S0`, `S1` (幸存区使用率), `E` (伊甸园区使用率), `O` (老年代使用率), `M` (元空间使用率), `YGC` (年轻GC次数), `YGCT` (年轻GC时间), `FGC` (Full GC次数), `FGCT` (Full GC时间)  
 \* \*\*关键指标:\*\*  
 \* \*\*`FGC` (Full GC) 次数频繁\*\* 且 \*\*`FGCT` (Full GC时间) 很长\*\*：这是导致应用“卡顿”的最常见原因。如果 `O` (老年代) 使用率一直很高（如&gt;95%），很可能存在\*\*内存泄漏\*\*。

4\. \*\*查看线程栈 (`jstack`) - 排查死锁和阻塞\*\*  
 ```bash  
 # 生成当前线程的快照  
 jstack 12345 &gt; jstack.log

 # 或者连续抓取几次，对比分析  
 jstack 12345 &gt; jstack\_1.log  
 sleep 10  
 jstack 12345 &gt; jstack\_2.log  
 ```  
 \* \*\*分析 `jstack.log`:\*\*  
 \* 搜索 `deadlock` 看有无死锁信息。  
 \* 搜索 `blocked` 看大量线程阻塞在哪个锁上。  
 \* 看线程的\*\*状态\*\* (`RUNNABLE`, `WAITING`, `BLOCKED`) 和\*\*堆栈信息\*\*，找出大多数线程在做什么。例如，如果很多线程卡在数据库连接 `getConnection()` 上，可能是连接池不够用。

5\. \*\*生成堆转储文件 (`jmap`) - 用于分析内存泄漏\*\*  
 ```bash  
 # 生成Heap Dump文件（文件较大，注意磁盘空间）  
 jmap -dump:live,format=b,file=heap\_dump.hprof 12345  
 ```  
 \* 这个命令会对生产环境有一定影响（可能触发STW的Full GC），谨慎使用。  
 \* 生成的文件 `heap\_dump.hprof` 可以下载到本地，用 \*\*Eclipse MAT (Memory Analyzer Tool)\*\* 或 \*\*jhat\*\* 工具分析，查看是什么对象占用了大量内存。

\---

\### 第三步：Tomcat自身配置检查

1\. \*\*连接数配置\*\*  
 检查 `$CATALINA\_HOME/conf/server.xml` 中的连接器配置：  
 ```xml  
 &lt;Connector port="8080" protocol="HTTP/1.1"  
 connectionTimeout="20000"  
 maxThreads="200" &lt;!-- 最大线程数是否足够？ --&gt;  
 acceptCount="100" &lt;!-- 等待队列长度 --&gt;  
 ... /&gt;  
 ```  
 \* 如果并发请求数超过 `maxThreads`，多余的请求会在队列中等待，导致响应变慢。

\### 总结与行动指南

1\. \*\*`top`\*\* -&gt; 发现CPU或内存高。  
2\. \*\*`jstat -gcutil`\*\* -&gt; 确认是频繁Full GC导致。  
3\. \*\*`jstack`\*\* -&gt; 检查线程状态，看GC时线程在做什么，有无死锁。  
4\. \*\*`jmap` + MAT\*\* -&gt; 分析是什么对象占满了内存，定位内存泄漏的代码。  
5\. \*\*查看日志\*\* -&gt; 始终是第一步，寻找任何明显的错误线索。  
6\. \*\*检查配置\*\* -&gt; 确认 `maxThreads`、数据库连接池大小等配置是否合理。

根据上述步骤，你通常可以定位到是系统资源问题、GC问题、代码阻塞问题还是配置问题。

好的 👍 我帮你写一个 **适合 Ubuntu14.04 + Tomcat 的监控采集脚本**。  
脚本会每天定期执行，调用 `jstack` 或 `jstat` 收集 Tomcat 的 JVM 信息，并写入日志文件，每条日志前加上时间戳。

### 示例脚本 `collect_tomcat_jvm.sh`

```bash
#!/bin/bash
# 收集 Tomcat JVM 的 jstack/jstat 信息
# 保存到日志文件：/var/log/tomcat_jvm_monitor.log

# 配置项
LOG_DIR="/var/log"
LOG_FILE="$LOG_DIR/tomcat_jvm_monitor.log"
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64"   # 修改为你实际的 JDK 路径
TOMCAT_USER="tomcat"  # 修改为运行 tomcat 的用户
DATE=$(date +"%Y-%m-%d %H:%M:%S")

# 找到 Tomcat 的 Java 进程 PID
PID=$(ps -u $TOMCAT_USER -o pid,comm | grep java | awk '{print $1}')

if [ -z "$PID" ]; then
    echo "[$DATE] 未找到 Tomcat 进程" >> $LOG_FILE
    exit 1
fi

# 写入时间戳
echo "==================== [$DATE] ====================" >> $LOG_FILE

# 收集 jstat GC 信息
$JAVA_HOME/bin/jstat -gc $PID >> $LOG_FILE 2>&1

# 收集线程堆栈
$JAVA_HOME/bin/jstack -l $PID >> $LOG_FILE 2>&1

echo -e "\n" >> $LOG_FILE

```

---

### 使用方法

1. **保存脚本**  
    保存为 `/usr/local/bin/collect_tomcat_jvm.sh`，并赋予执行权限：
    
    ```bash
    sudo chmod +x /usr/local/bin/collect_tomcat_jvm.sh
    
    ```
2. **日志目录**  
    确认 `/var/log/` 有写权限，或者改为你自己的目录。
3. **定时任务**  
    使用 `cron` 每天定时执行，比如每天凌晨 2 点收集一次：
    
    ```bash
    crontab -e
    
    ```
    
    添加：
    
    ```bash
    0 2 * * * /usr/local/bin/collect_tomcat_jvm.sh
    
    ```
    
    如果你想更频繁（比如每 30 分钟收集一次），可以写：
    
    ```bash
    */30 * * * * /usr/local/bin/collect_tomcat_jvm.sh
    
    ```

---

👉 建议：

- `jstat` 适合收集 JVM GC/内存指标
- `jstack` 适合排查线程阻塞、死锁问题
- 日志文件会越来越大，可以结合 `logrotate` 自动切割。

---

要不要我帮你把这个脚本扩展成 **logrotate 配置**，避免日志无限增长？

好的 👍 我帮你把 `collect_tomcat_jvm.sh` 脚本对应的 **logrotate 配置文件**写出来，避免日志无限增长。

你现在的日志文件路径是：

```
/home/shuncom/tomcat_jvm_monitor.log

```

---

### logrotate 配置示例

新建文件 `/etc/logrotate.d/tomcat_jvm_monitor`：

```
/home/shuncom/tomcat_jvm_monitor.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0644 root root
    sharedscripts
    postrotate
        true
    endscript
}

```

以下对各行参数进行注释，但不能把行内注释写入脚本

```conf
/home/shuncom/tomcat_jvm_monitor.log {
    daily              # 每天轮转一次
    rotate 7           # 保留 7 天的日志，超过删除
    compress           # 启用压缩，老日志会变成 .gz
    delaycompress      # 延迟一天再压缩，避免影响最新日志查看
    missingok          # 如果日志不存在，不报错
    notifempty         # 如果日志为空，不进行轮转
    create 0644 root root  # 新日志的权限和属主（根据实际情况修改）
    sharedscripts
    postrotate
        # logrotate 每次切割日志后执行的操作
        # 这里不需要额外操作，因为脚本本身会继续写新日志
        true
    endscript
}

```

---

### 检查 logrotate 是否正常工作

手动执行一次（模拟日志轮转）：

```bash
sudo logrotate -f /etc/logrotate.d/tomcat_jvm_monitor

```

执行后，你应该会看到：

```
/home/shuncom/tomcat_jvm_monitor.log  →  /home/shuncom/tomcat_jvm_monitor.log.1

```

老日志会被压缩（如 `.1.gz`），新日志文件会重新创建。

---

⚠️ 注意事项：

1. 如果 `collect_tomcat_jvm.sh` 是用 `root` 运行的，`create 0644 root root` 就对了；  
    如果是 Tomcat 用户运行，改成 `create 0644 tomcat tomcat`。
2. 默认 logrotate 会每天运行一次，定时任务在 `/etc/cron.daily/logrotate`。
3. 如果日志量特别大，可以改成 `size 100M`（按大小轮转）。

---

要不要我帮你改成 **“按大小+按天混合轮转”**，比如每天轮转一次，或者日志超过 200MB 时立即切割？

👌 明白了，你要 **按天+按大小混合轮转**：

- 每天轮转一次；
- 如果日志超过 **100MB**，即使没到一天也立即切割。

logrotate 支持这种组合配置。

---

### 配置文件 `/etc/logrotate.d/tomcat_jvm_monitor`

```conf
/home/shuncom/tomcat_jvm_monitor.log {
    daily               # 每天轮转一次
    size 100M           # 日志超过 100MB 时立即切割
    rotate 7            # 保留 7 个历史文件
    compress            # 压缩历史日志
    delaycompress       # 延迟一天再压缩，避免影响最新日志查看
    missingok           # 如果日志不存在，不报错
    notifempty          # 如果日志为空，不轮转
    create 0644 root root   # 新日志文件权限和属主
    sharedscripts
    postrotate
        true
    endscript
}

```

---

### 验证

手动测试（强制执行一次）：

```bash
sudo logrotate -f /etc/logrotate.d/tomcat_jvm_monitor

```

日志会被切割为：

```
tomcat_jvm_monitor.log
tomcat_jvm_monitor.log.1
tomcat_jvm_monitor.log.2.gz
...

```

---

⚠️ 补充说明：

- `daily + size` 会实现 **二选一触发**，即每天轮转一次，或者超过 100MB 时立即切割。
- `rotate 7` 表示最多保留 7 个旧文件，超过会自动删除。
- 如果你想确保 **每天只切一次**（即使超过 100MB 也只在第二天切割），那要用 `maxsize` 而不是 `size`。

---

要不要我帮你写个对比表，解释 `size` 和 `maxsize` 的区别，方便你选择？

# Ubuntu 14.04 进Recovery Mode模式重置忘记的密码

在 **Ubuntu 14.04** 下如果忘记了 root（或用户）密码，可以通过进入 **单用户模式（Recovery Mode）** 来恢复，步骤如下：

---

## 方法一：通过 GRUB 进入单用户模式修改密码

1. **重启服务器/主机**  
    在启动时按下 `Shift` 键，调出 **GRUB 启动菜单**。
2. **选择恢复模式**  
    在菜单中选择类似：
    
    ```
    Ubuntu, with Linux ... (recovery mode)
    
    ```
    
    或者选中正常启动的内核条目后，按 **e** 编辑。
3. **修改启动参数**  
    找到以 `linux` 开头的那一行，结尾可能是：
    
    ```
    ro quiet splash
    
    ```
    
    把 `ro` 改成 `rw init=/bin/bash`，然后按 `Ctrl+X` 或 `F10` 启动。
4. **进入 root shell**  
    系统会直接进入 root shell，无需密码。
5. **修改密码**  
    执行：
    
    ```bash
    passwd 用户名
    
    ```
    
    如果要改 root 密码：
    
    ```bash
    passwd root
    
    ```
    
    然后输入两次新密码。
6. **重新挂载为读写（如需要）**  
    如果提示文件系统只读，可以先执行：
    
    ```bash
    mount -o remount,rw /
    
    ```
7. **重启**  
    修改完成后，执行：
    
    ```bash
    exec /sbin/init
    
    ```
    
    或者直接：
    
    ```bash
    reboot -f
    
    ```

---

## 方法二：用 LiveCD 修改密码

如果 GRUB 菜单被锁定或者不能进入单用户模式，可以：

1. 用 **Ubuntu LiveCD / U盘** 启动系统。
2. 挂载原系统分区，比如：
    
    ```bash
    sudo mount /dev/sda1 /mnt
    
    ```
3. 切换 chroot 环境：
    
    ```bash
    sudo chroot /mnt
    
    ```
4. 修改密码：
    
    ```bash
    passwd 用户名
    
    ```
5. 退出并重启：
    
    ```bash
    exit
    reboot
    
    ```

---

⚠️ 注意：

- 如果这台机器是生产服务器，操作前最好先 **快照/备份** 磁盘。
- 如果是云服务器（阿里云、AWS 之类），可能需要在控制台重置密码或用救援模式进入。

---

要不要我帮你写一个 **Ubuntu 14.04 专用的恢复密码命令清单**（适合你直接复制粘贴操作）？

# ubuntu设置swap交换缓存

你这台 **Ubuntu 14.04** 系统目前是 **没有启用交换分区/交换文件** 的，所以 `free -mh` 里显示 **交换：0B**，`sar -S` 也全是 0。要启用 swap（交换缓存），需要手动添加。

---

## 一、创建交换文件（推荐方法）

1. **创建一个 2G 的交换文件**（可以根据内存大小决定，通常设为内存的 1～2 倍，但不要超过硬盘实际空间）：
    
    ```bash
    sudo fallocate -l 2G /swapfile
    
    ```
    
    如果 `fallocate` 不支持，可以用 `dd`：
    
    ```bash
    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
    
    ```
2. **设置权限**
    
    ```bash
    sudo chmod 600 /swapfile
    
    ```
3. **格式化为交换分区**
    
    ```bash
    sudo mkswap /swapfile
    
    ```
4. **启用 swap**
    
    ```bash
    sudo swapon /swapfile
    
    ```
5. **检查是否生效**
    
    ```bash
    free -mh
    
    ```
    
    会显示有了 2G 的 Swap。  
    你再执行：
    
    ```bash
    sar -S 1 5
    
    ```
    
    就会有 kbswpfree 数值了。
6. **永久生效**  
    编辑 `/etc/fstab`，在文件最后添加一行：
    
    ```
    /swapfile   none    swap    sw    0   0
    
    ```

---

## 二、如果你有空闲磁盘分区

也可以用 `mkswap` 格式化一个分区（比如 `/dev/sdb2`），然后 `swapon /dev/sdb2`，并写入 `/etc/fstab`。

---

## 三、调整内核交换策略（可选）

Linux 默认 **尽量不用 swap**，你可以通过 `swappiness` 调整：

```bash
cat /proc/sys/vm/swappiness

```

默认是 **60**（数值越高越倾向使用 swap）。  
临时修改：

```bash
sudo sysctl vm.swappiness=10

```

永久修改：  
编辑 `/etc/sysctl.conf`，添加：

```
vm.swappiness=10

```

保存后执行：

```bash
sudo sysctl -p

```

---

🔧 建议：

- 你的内存才 1.8G，**一定要开 swap**，否则遇到大进程容易 OOM。
- 推荐 swapfile **2G** 左右，再配合 `vm.swappiness=10`，避免过度使用交换区导致系统卡顿。

---

要不要我帮你写一份 **Ubuntu 14.04 一键创建 + 启用 2G swap 的脚本**（直接复制运行即可）？

👌 现在效果很清楚了：

- 你已经启用了 **2 GiB swap**（`free -mh` 里显示出来了）。
- `sar -S` 也显示 swap 空间总量：
    
    ```
    kbswpfree  2096880   （大约 2GB 可用）
    kbswpused       268 （大约 268KB 被用）
    %swpused       0.01%
    
    ```
    
    → 说明 swap 已经生效，并且被轻微使用。

---

### 你目前的状态

- 内存总计：**1.8 GiB**，已用 ~727 MiB，可用 ~898 MiB。
- 缓冲/缓存 ~1 GiB，说明系统在尽可能利用内存做缓存。
- Swap 已经挂上，使用量几乎为 0，只在少数情况下被触发。
- `kbswpcad`（swap cache）显示有 **13% 命中率**，说明有部分页被交换出去后又被引用，但整体使用很轻微。

---

### 优化建议

1. **调整 swappiness（控制 swap 使用倾向）**  
    目前系统的默认值应该是 `60`。  
    你内存比较小（1.8 G），建议设低一些，避免过早 swap：
    
    ```bash
    echo 10 | sudo tee /proc/sys/vm/swappiness
    
    ```
    
    永久生效：在 `/etc/sysctl.conf` 里加：
    
    ```
    vm.swappiness=10
    
    ```
    
    然后：
    
    ```bash
    sudo sysctl -p
    
    ```
2. **设置 cache 压缩（可选）**  
    如果升级到较新内核，可以考虑用 `zram` 压缩 swap，性能更好。不过你现在是 5.4 内核，已经支持 zram，可以配置 `/dev/zram` 来当 swap。
3. **监控 swap 使用情况**
    
    
    - 查看 swap 使用情况：
        
        ```bash
        swapon -s
        
        ```
    - 结合内存监控：
        
        ```bash
        vmstat 1
        
        ```
        
        如果 `si/so`（swap in/out）经常不为 0，就说明 swap 压力大了。

---

⚡ 总结：

- 你已经 **成功启用了 2 G swap**。
- swap 正常工作，但几乎没用到，不会拖慢性能。
- 建议把 `vm.swappiness` 调到 **10** 左右，主要靠内存跑，只有在内存紧张时才用 swap。

要不要我给你写一个 **一键设置 swappiness + 开机启用 swap 检查的脚本**，方便你服务器自动化维护？

# 平台控制一会成功一会失败抓包分析实例

嗯，我看到用户上传了一个CSV文件，看起来是一个网络抓包记录。让我仔细看看这个文件内容，分析一下多次SYN重连的原因。

这个文件记录了TCP通信的数据包，源IP是222.222.42.187，目标IP是222.222.23.138，目标端口是6011。时间跨度大约是从15:02:56到15:06:29，大概3分半钟的时间。

我看到有很多TCP连接建立和关闭的过程。每个连接都遵循类似的模式：SYN → SYN-ACK → ACK（三次握手），然后有一些数据交换（PSH, ACK），最后是FIN → FIN-ACK → ACK（四次挥手）。

有趣的是，这些连接都非常短暂。我看到每个连接从建立到关闭的时间间隔很短，大多数只有几秒钟。比如第一个连接（端口52415）只持续了不到1秒，第二个连接（端口52420）持续了约3.5秒。

端口号在不断递增，从52415开始，一直到52531，这说明客户端在不断尝试新的连接，而不是复用现有连接。

我看到有些连接中有数据交换（PSH标志），数据长度是5字节或9字节，这可能是某种心跳或控制消息。有些连接中服务器会回复数据，有些则不会。

我觉得这种频繁的连接建立和关闭可能有几个原因。可能是应用程序设计如此，需要频繁建立短连接；也可能是某种协议要求，比如需要定期认证或状态同步；还有可能是客户端或服务器有问题，无法保持长连接。

从技术角度看，这种频繁的SYN连接会带来一些开销，包括TCP三次握手的时间延迟和资源消耗（如端口号、内存等）。但看起来这个模式很规律，不像是网络问题导致的意外重连。

我注意到时间戳（TSval/TSecr）也在变化，这表明TCP时间戳选项被启用，这有助于防止序列号回绕和更精确的RTT测量。

我觉得需要向用户解释这种SYN重连现象的可能原因，包括应用程序设计特性、协议要求、客户端实现方式等可能性。同时也可以从技术角度分析这种模式的优缺点。

好，我现在准备回答用户的问题，分析这个抓包记录中多次SYN重连的原因。

根据你提供的网络抓包记录（`tar-6011.csv`），可以看出这是一个典型的 **TCP 连接频繁建立和终止** 的流量记录。以下是对多次 **SYN 重连** 的原因分析：

<div class="ds-markdown" id="bkmrk-" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">---

</div>### 🔍 一、流量特征总结

<div class="ds-markdown" id="bkmrk-%E6%BA%90ip%EF%BC%9A222.222.42.187%EF%BC%88%E5%AE%A2" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- **源IP**：`222.222.42.187`（客户端）
- **目标IP**：`222.222.23.138`（服务端）
- **目标端口**：`6011`
- **协议**：TCP
- **行为**：频繁的 TCP 三次握手（SYN → SYN-ACK → ACK）和四次挥手（FIN → FIN-ACK → ACK）
- **数据长度**：多数数据包只有 5 或 9 字节的有效载荷（可能是心跳、控制消息或轻量级协议）

---

</div>### ⚠️ 二、SYN 重连的常见原因

#### 1. **短连接通信模式**

<div class="ds-markdown" id="bkmrk-%E6%AF%8F%E4%B8%AA-tcp-%E8%BF%9E%E6%8E%A5%E5%8F%AA%E7%94%A8%E4%BA%8E%E5%8F%91%E9%80%81%E6%9E%81%E5%B0%91%E9%87%8F%E7%9A%84%E6%95%B0%E6%8D%AE" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- 每个 TCP 连接只用于发送极少量的数据（如 5 字节的请求 + 9 字节的响应），随后立即关闭。
- 常见于：
    
    
    - **心跳检测**（keep-alive 或 health check）
    - **控制协议**（如某些工控协议、自定义控制通道）
    - **API 短连接调用**（如 HTTP/1.0 无 keep-alive）

</div>#### 2. **协议设计或客户端实现**

<div class="ds-markdown" id="bkmrk-%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%8F%AF%E8%83%BD%E8%A2%AB%E8%AE%BE%E8%AE%A1%E4%B8%BA%E6%AF%8F%E6%AC%A1%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82%E9%83%BD%E6%96%B0%E5%BB%BA%E8%BF%9E%E6%8E%A5" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- 客户端可能被设计为**每次发送请求都新建连接**，而不是复用连接（如 HTTP/1.1 的持久连接）。
- 可能是某种**自定义协议**或**老旧协议**，不支持长连接。

</div>#### 3. **服务端主动关闭连接**

<div class="ds-markdown" id="bkmrk-%E5%9C%A8%E6%9F%90%E4%BA%9B%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%8F%AF%E8%83%BD%E5%9C%A8%E5%AE%8C%E6%88%90%E5%93%8D%E5%BA%94%E5%90%8E%E7%AB%8B%E5%8D%B3" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- 在某些情况下，服务端可能在完成响应后立即发送 FIN 包，强制关闭连接，导致客户端必须重建连接。

</div>#### 4. **网络或防火墙策略**

<div class="ds-markdown" id="bkmrk-%E4%B8%AD%E9%97%B4%E8%AE%BE%E5%A4%87%EF%BC%88%E5%A6%82%E9%98%B2%E7%81%AB%E5%A2%99%E3%80%81%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%99%A8%EF%BC%89%E5%8F%AF%E8%83%BD%E4%BC%9A%E5%9B%A0" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- 中间设备（如防火墙、负载均衡器）可能会因为 idle timeout 设置过短而断开连接。
- 但在此流量中，连接是由客户端主动发起的 FIN 开始的，说明很可能是应用层行为。

---

</div>### 📊 三、具体流量模式分析

<div class="ds-markdown" id="bkmrk-%E6%AF%8F%E4%B8%AA%E2%80%9C%E4%BC%9A%E8%AF%9D%E2%80%9D%E5%A4%A7%E8%87%B4%E5%A6%82%E4%B8%8B%EF%BC%9A-syn%C2%A0%E2%86%92%C2%A0sy" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- 每个“会话”大致如下：
    
    
    - `SYN` → `SYN-ACK` → `ACK`（三次握手）
    - `PSH,ACK`（客户端发送 5 字节）
    - `ACK`（服务端确认）
    - 有时服务端会回复 `PSH,ACK`（9 字节）
    - `FIN,ACK` → `FIN,ACK` → `ACK`（四次挥手）
- 整个过程非常短暂，多数在 **3~10 秒** 内完成。

---

</div>### 🧠 四、可能的应用场景

<div class="ds-markdown" id="bkmrk-%E5%B7%A5%E4%B8%9A%E6%8E%A7%E5%88%B6%E7%B3%BB%E7%BB%9F%EF%BC%88%E5%A6%82-modbus%2Ftcp%E3%80%81" style="--ds-md-zoom: 1.143; font: 400 16px / 28px quote-cjk-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; color: rgb(15, 17, 21); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- **工业控制系统**（如 Modbus/TCP、Profinet 等）
- **数据库或监控系统的心跳机制**
- **自定义控制协议**（如设备状态查询）
- **负载均衡健康检查**
- **某些物联网设备通信**

</div>

# 运维工作中最常用的 `rabbitmqctl` 命令大全

好的，以下是运维工作中最常用的 `rabbitmqctl` 命令大全，涵盖了日常管理、监控和故障排查的各个方面。

\## 📊 RabbitMQ 常用运维命令大全

\### 一、节点与集群管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl status` | \*\*查看节点状态\*\*（最常用） | `rabbitmqctl status` |  
| `rabbitmqctl cluster\_status` | \*\*查看集群状态\*\* | `rabbitmqctl cluster\_status` |  
| `rabbitmqctl stop` | 停止 RabbitMQ 应用（保持 Erlang 运行） | `rabbitmqctl stop` |  
| `rabbitmqctl stop\_app` | 停止 RabbitMQ 应用 | `rabbitmqctl stop\_app` |  
| `rabbitmqctl start\_app` | 启动 RabbitMQ 应用 | `rabbitmqctl start\_app` |  
| `rabbitmqctl reset` | \*\*重置节点\*\*（清除所有数据） | `rabbitmqctl reset` |  
| `rabbitmqctl force\_reset` | 强制重置节点（当正常重置失败时） | `rabbitmqctl force\_reset` |  
| `rabbitmqctl forget\_cluster\_node &lt;node\_name&gt;` | 从集群中移除节点 | `rabbitmqctl forget\_cluster\_node rabbit@node2` |

\### 二、用户管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl list\_users` | \*\*列出所有用户\*\* | `rabbitmqctl list\_users` |  
| `rabbitmqctl add\_user &lt;username&gt; &lt;password&gt;` | \*\*添加用户\*\* | `rabbitmqctl add\_user admin password123` |  
| `rabbitmqctl delete\_user &lt;username&gt;` | \*\*删除用户\*\* | `rabbitmqctl delete\_user testuser` |  
| `rabbitmqctl change\_password &lt;username&gt; &lt;newpassword&gt;` | \*\*修改用户密码\*\* | `rabbitmqctl change\_password admin newpass123` |  
| `rabbitmqctl clear\_password &lt;username&gt;` | 清除用户密码（使其无法密码登录） | `rabbitmqctl clear\_password testuser` |  
| `rabbitmqctl set\_user\_tags &lt;username&gt; &lt;tag&gt; \[...\]` | \*\*设置用户角色\*\* | `rabbitmqctl set\_user\_tags admin administrator` |

\*\*常用角色标签：\*\*  
\- `administrator`：超级管理员  
\- `monitoring`：监控权限  
\- `policymaker`：策略管理权限  
\- `management`：管理界面访问权限  
\- `none`：无特殊权限

\### 三、虚拟主机（vhost）管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl list\_vhosts` | \*\*列出所有虚拟主机\*\* | `rabbitmqctl list\_vhosts` |  
| `rabbitmqctl add\_vhost &lt;vhost\_name&gt;` | \*\*添加虚拟主机\*\* | `rabbitmqctl add\_vhost /my\_vhost` |  
| `rabbitmqctl delete\_vhost &lt;vhost\_name&gt;` | \*\*删除虚拟主机\*\* | `rabbitmqctl delete\_vhost /my\_vhost` |  
| `rabbitmqctl list\_permissions \[-p &lt;vhost&gt;\]` | 列出权限 | `rabbitmqctl list\_permissions -p /` |  
| `rabbitmqctl set\_permissions \[-p &lt;vhost&gt;\] &lt;user&gt; &lt;conf&gt; &lt;write&gt; &lt;read&gt;` | \*\*设置用户权限\*\* | `rabbitmqctl set\_permissions -p / admin ".\*" ".\*" ".\*"` |  
| `rabbitmqctl clear\_permissions \[-p &lt;vhost&gt;\] &lt;user&gt;` | 清除用户权限 | `rabbitmqctl clear\_permissions -p / testuser` |

\### 四、队列与消息管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl list\_queues \[-p &lt;vhost&gt;\] \[&lt;queueinfoitem&gt; ...\]` | \*\*列出队列\*\* | `rabbitmqctl list\_queues name messages messages\_ready` |  
| `rabbitmqctl list\_exchanges \[-p &lt;vhost&gt;\]` | \*\*列出交换机\*\* | `rabbitmqctl list\_exchanges` |  
| `rabbitmqctl list\_bindings \[-p &lt;vhost&gt;\]` | \*\*列出绑定关系\*\* | `rabbitmqctl list\_bindings` |  
| `rabbitmqctl list\_connections` | \*\*列出客户端连接\*\* | `rabbitmqctl list\_connections` |  
| `rabbitmqctl list\_channels` | \*\*列出通道\*\* | `rabbitmqctl list\_channels` |  
| `rabbitmqctl list\_consumers \[-p &lt;vhost&gt;\]` | 列出消费者 | `rabbitmqctl list\_consumers` |  
| `rabbitmqctl purge\_queue \[-p &lt;vhost&gt;\] &lt;queue&gt;` | \*\*清空队列中的消息\*\* | `rabbitmqctl purge\_queue my\_queue` |  
| `rabbitmqctl delete\_queue \[-p &lt;vhost&gt;\] &lt;queue&gt;` | 删除队列 | `rabbitmqctl delete\_queue my\_queue` |

\### 五、策略管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl list\_policies \[-p &lt;vhost&gt;\]` | \*\*列出所有策略\*\* | `rabbitmqctl list\_policies` |  
| `rabbitmqctl set\_policy \[-p &lt;vhost&gt;\] \[--priority &lt;priority&gt;\] \[--apply-to &lt;apply-to&gt;\] &lt;name&gt; &lt;pattern&gt; &lt;definition&gt;` | \*\*设置策略\*\* | `rabbitmqctl set\_policy ha-all "^ha\\." '{"ha-mode":"all"}'` |  
| `rabbitmqctl clear\_policy \[-p &lt;vhost&gt;\] &lt;name&gt;` | 清除策略 | `rabbitmqctl clear\_policy ha-all` |

\### 六、监控与诊断

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmqctl environment` | 查看环境变量配置 | `rabbitmqctl environment` |  
| `rabbitmqctl report` | \*\*生成详细报告\*\*（用于故障诊断） | `rabbitmqctl report &gt; report.txt` |  
| `rabbitmqctl eval '&lt;expression&gt;'` | 执行 Erlang 表达式 | `rabbitmqctl eval 'node().'` |  
| `rabbitmqctl close\_connection &lt;connectionpid&gt; &lt;explanation&gt;` | 关闭指定连接 | `rabbitmqctl close\_connection "&lt;rabbit@node.1.1.1.1&gt;" "maintenance"` |

\### 七、插件管理

| 命令 | 说明 | 示例 |  
|------|------|------|  
| `rabbitmq-plugins list` | \*\*列出所有插件\*\* | `rabbitmq-plugins list` |  
| `rabbitmq-plugins enable &lt;plugin&gt; \[...\]` | \*\*启用插件\*\* | `rabbitmq-plugins enable rabbitmq\_management` |  
| `rabbitmq-plugins disable &lt;plugin&gt; \[...\]` | \*\*禁用插件\*\* | `rabbitmq-plugins disable rabbitmq\_management` |

\### 🎯 日常运维场景命令组合

\*\*1. 健康检查：\*\*  
```bash  
rabbitmqctl status  
rabbitmqctl list\_queues name messages messages\_ready messages\_unacknowledged  
rabbitmqctl list\_connections  
```

\*\*2. 排查消息堆积：\*\*  
```bash  
\# 查看所有队列的消息数量  
rabbitmqctl list\_queues name messages messages\_ready messages\_unacknowledged consumers

\# 查看具体队列的消费者情况  
rabbitmqctl list\_consumers | grep my\_queue  
```

\*\*3. 创建管理用户：\*\*  
```bash  
rabbitmqctl add\_user admin strongpassword  
rabbitmqctl set\_user\_tags admin administrator  
rabbitmqctl set\_permissions -p / admin ".\*" ".\*" ".\*"  
```

\*\*4. 紧急清空队列：\*\*  
```bash  
\# 先列出队列确认名称  
rabbitmqctl list\_queues name messages

\# 清空指定队列  
rabbitmqctl purge\_queue my\_queue  
```

\*\*5. 集群状态检查：\*\*  
```bash  
rabbitmqctl cluster\_status  
rabbitmqctl list\_queues name messages cluster\_node  
```

\### ⚠️ 注意事项

1\. \*\*权限要求\*\*：大多数命令需要管理员权限  
2\. \*\*Erlang Cookie\*\*：集群管理需要确保各节点有相同的 Erlang cookie  
3\. \*\*生产环境谨慎操作\*\*：`reset`、`purge\_queue`、`delete\_queue` 等命令会丢失数据  
4\. \*\*监控建议\*\*：定期监控内存、磁盘空间和队列积压情况

这些命令涵盖了 RabbitMQ 日常运维的绝大多数场景，建议根据实际需求灵活组合使用。

# ubuntu22.04 /dev/sda扩容后把剩余的全部空间分给/home

你现在的情况是：

- 物理磁盘 `/dev/sda` 总共 **700G**
- 已经分区：
    
    
    - `/dev/sda2` → `/boot` (2G)
    - `/dev/sda3` → LVM PV (298G)
- 你的 `/` 和 `/home` 都在 LVM 里（`ubuntu-vg` 卷组），目前 **卷组只有 298G** 可用。
- 剩下的 ~400G 还没分配给任何分区。

所以要把剩余空间加到 `/home`，步骤是：

---

### 🔧 操作步骤

#### 1. 检查现有磁盘分区情况

```bash
lsblk -f
sudo fdisk -l /dev/sda

```

你会看到 `/dev/sda3` 只用了 298G，剩下的 402G 是未分区状态。

---

#### 2. 给剩余空间新建一个分区（作为 LVM PV）

用 `gdisk` 或 `fdisk` 操作：

```bash
sudo fdisk /dev/sda

```

操作步骤：

- 输入 `n` 创建新分区（例如 `/dev/sda4`），使用剩余全部空间
- 分区类型改成 **Linux LVM**（代码 `8e`）
- 保存退出（`w`）

然后让内核识别新分区：

```bash
sudo partprobe /dev/sda

```

---

#### 3. 把新分区加入 LVM 卷组

```bash
sudo pvcreate /dev/sda4
sudo vgextend ubuntu-vg /dev/sda4

```

检查卷组是否扩展成功：

```bash
vgdisplay ubuntu-vg

```

你会看到 VG 的 Free PE 大幅增加（多了 400G）。

---

#### 4. 扩容 `/home` 的逻辑卷

你的 `/home` 是 `ubuntu-vg/lv-0`，扩容命令：

```bash
sudo lvextend -l +100%FREE /dev/ubuntu-vg/lv-0

```

这会把卷组里所有剩余空间全部加到 `/home`。

---

#### 5. 扩展文件系统

```bash
sudo resize2fs /dev/ubuntu-vg/lv-0

```

---

#### 6. 验证

```bash
df -h

```

你应该能看到 `/home` 已经从 220G 扩展到 ~620G 左右。

---

⚠️ **注意事项**

- 操作前建议先快照/备份重要数据，防止误操作。
- 如果 `/home` 文件系统不是 ext4 而是 xfs，用 `xfs_growfs` 来扩展：
    
    ```bash
    sudo xfs_growfs /home
    
    ```

---

要不要我帮你写一个完整的 **一键脚本**，自动检测 `/home` 的 LV，然后扩容到整个磁盘？

# ubuntu22.04升级docker&docker-compose

明白 ✅  
你现在的环境里：

- **Docker** 和 **docker-compose** 都是用 **解压 .deb 包 + 手动拷贝二进制** 的方式安装的（`docker_debs_2021_Rulr6.zip`）。
- 这种方式不会跟随系统自动升级，所以需要你手动卸载旧版本 → 安装 Docker 官方最新版本。

---

## 🔧 升级步骤（Ubuntu 22.04）

### 1. 卸载旧版本

先停掉并卸载老的 docker 组件：

```bash
sudo systemctl stop docker
sudo apt-get remove -y docker docker-engine docker.io containerd runc
sudo rm -f /usr/bin/docker-compose
sudo rm -rf /var/lib/docker

```

> ⚠️ `/var/lib/docker` 删除会清空所有容器和镜像，如果你要保留数据，可以先备份或跳过这一步。

---

### 2. 安装最新 Docker CE

添加官方仓库并安装：

```bash
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release

# 添加 Docker GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# 添加 Docker apt 源
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 安装最新 Docker
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

```

---

### 3. 启用并验证 Docker

```bash
sudo systemctl enable docker
sudo systemctl start docker
docker -v

```

应该显示最新版，例如：

```
Docker version 27.3.1, build xxxx

```

---

### 4. 安装新版 Docker Compose (v2)

新版 compose 已经是 Docker 官方插件，不需要单独装 `docker-compose` 1.x 了，命令变成：

```bash
docker compose version

```

例如：

```
Docker Compose version v2.29.7

```

如果你还想保留 `docker-compose` 命令（兼容老项目），可以做个软链接：

```bash
sudo ln -s /usr/libexec/docker/cli-plugins/docker-compose /usr/bin/docker-compose

```

---

## 总结

- 你的 **旧版 1.29.2 docker-compose** 太老 → 建议升级到 **Docker Compose v2**（插件版）。
- 以后直接用：
    
    ```bash
    docker compose up -d
    
    ```
- 不需要单独管理 compose 二进制。

---

要不要我帮你写一个 **一键升级脚本**（包含卸载旧版 + 安装最新版 Docker + 启用 compose v2 + 兼容 docker-compose 命令）？

# ubuntu24.04安装docker和docker-compose

好的 👍 我给你整理一份在 **Ubuntu 24.04** 上安装 **Docker** 和 **Docker Compose** 的完整步骤（官方推荐方式）。

---

## 1. 卸载旧版本（如果有）

```bash
sudo apt-get remove docker docker-engine docker.io containerd runc

```

---

## 2. 更新系统并安装依赖

```bash
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release

```

---

## 3. 添加 Docker 官方 GPG Key

```bash
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

```

---

## 4. 添加 Docker 官方源

```bash
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

```

---

## 5. 安装 Docker 最新版本

```bash
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

```

---

## 6. 启动并设置开机自启

```bash
sudo systemctl enable docker
sudo systemctl start docker

```

---

## 7. 验证安装

```bash
docker --version
docker compose version

```

你会看到类似输出：

```
Docker version 27.0.3, build xxxxxxx
Docker Compose version v2.29.2

```

---

## 8. （可选）非 root 用户使用 Docker

```bash
sudo groupadd docker   # 如果已经存在会提示
sudo usermod -aG docker $USER
newgrp docker

```

之后可以直接运行：

```bash
docker run hello-world

```

---

✅ 这样就完成了在 **Ubuntu 24.04** 上安装 **Docker + Docker Compose v2**（内置在 docker CLI 里，不再是单独的 python 版 `docker-compose`）。

要不要我再帮你写一个 **一键安装脚本**，你直接复制运行即可？

# ubuntu24.04安装mysql

在 **Ubuntu 24.04** 上安装 MySQL 的步骤和以前版本差不多，但有两个要点：

1. **Ubuntu 24.04 默认仓库里没有 MySQL**，它推荐 MariaDB。
2. 如果你要用 **官方 MySQL（Oracle 维护的版本）**，需要添加 MySQL 的官方 APT 源。

下面我给你两种方式：

---

## 🚀 方法一：安装 Ubuntu 自带的 MariaDB（兼容 MySQL，大部分场景够用）

```bash
sudo apt update
sudo apt install -y mariadb-server mariadb-client

```

安装完后：

```bash
sudo systemctl enable mariadb
sudo systemctl start mariadb

```

安全配置（设置 root 密码等）：

```bash
sudo mysql_secure_installation

```

---

## 🚀 方法二：安装 MySQL 官方版（推荐）

### 1. 下载并安装 MySQL APT 配置包

```bash
wget https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb

```

安装过程中会出现一个对话框，让你选择 MySQL 版本（一般选 **8.4 LTS**）。

### 2. 更新 apt

```bash
sudo apt update

```

### 3. 安装 MySQL

```bash
sudo apt install -y mysql-server

```

### 4. 启动并设置开机自启

```bash
sudo systemctl enable mysql
sudo systemctl start mysql

```

### 5. 验证版本

```bash
mysql --version

```

### 6. 初始化安全配置

```bash
sudo mysql_secure_installation

```

根据提示设置 root 密码、是否禁用匿名用户、是否删除 test 数据库等。

---

## 🚦 小提示

- 默认 root 登录方式是 `unix_socket`，如果要改成密码登录，可以这样：
    
    ```bash
    sudo mysql
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
    FLUSH PRIVILEGES;
    
    ```
- 如果需要远程连接，要修改 `/etc/mysql/mysql.conf.d/mysqld.cnf`，把：
    
    ```
    bind-address = 127.0.0.1
    
    ```
    
    改成：
    
    ```
    bind-address = 0.0.0.0
    
    ```
    
    然后重启：
    
    ```bash
    sudo systemctl restart mysql
    
    ```

---

要不要我帮你整理一个 **一键安装脚本**（选择 MySQL 还是 MariaDB），你复制运行即可？

# 堡垒机-tacPort容器化部署

# 容器化部署[​](https://docs.tp4a.com/deploy/containerized.html#%E5%AE%B9%E5%99%A8%E5%8C%96%E9%83%A8%E7%BD%B2)

## 准备工作[​](https://docs.tp4a.com/deploy/containerized.html#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C)

### 操作系统[​](https://docs.tp4a.com/deploy/containerized.html#%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F)

TacPort 支持以 docker 容器的方式进行部署，对应操作系统的要求，也就是对 docker 宿主机系统的要求。例如，您可以在 macOS 系统的 Docker Desktop 环境中部署容器化的 TacPort 节点。

### 初始化数据库[​](https://docs.tp4a.com/deploy/containerized.html#%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E6%8D%AE%E5%BA%93)

提示

如果您有数据库管理员账号及密码，可以跳过初始化数据库的步骤，在部署后由 TacPort 来为您创建并初始化数据库。

下面以 MySQL 8.x 为例说明如何为 TacPort 创建库及用户，并授予相应的访问权限。示例中的信息如下：

- 库名：`tacport`
- 用户：`tacport`
- 密码：`Pas5*w0rd`

<div class="language-shell vp-adaptive-theme line-numbers-mode" id="bkmrk-shell" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">shell</span></div>```
# 先以数据库管理员身份连接数据库
mysql -uroot -p
# 此时提示输入密码，请输入数据库root账号的密码
```

<div class="language-shell vp-adaptive-theme line-numbers-mode" id="bkmrk-123" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">2</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">3</span>  
</div></div>输入正确的密码后，进入MySQL交互式环境：

<div class="language-sql vp-adaptive-theme line-numbers-mode" id="bkmrk-sql" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">sql</span></div>```
-- 进入MySQL交互式环境后，执行以下SQL语句：
CREATE DATABASE tacport DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER `tacport`@`localhost` IDENTIFIED BY 'Pas5*w0rd';
GRANT ALL PRIVILEGES ON tacport.* TO `tacport`@`localhost`;
FLUSH PRIVILEGES;

-- 最后执行quit退出MySQL交互式环境
quit;


-- 要改所有主机访问，删除原来的本地用户（可选，如果你不需要限制 localhost 的账号）
DROP USER 'tacport'@'localhost';

-- 重新创建一个任意主机可访问的账号
CREATE USER 'tacport'@'%' IDENTIFIED BY 'Pas5*w0rd';

-- 授权数据库
GRANT ALL PRIVILEGES ON tacport.* TO 'tacport'@'%';

-- 刷新权限
FLUSH PRIVILEGES;

```

<div class="language-sql vp-adaptive-theme line-numbers-mode" id="bkmrk-12345678" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">2</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">3</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">4</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">5</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">6</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">7</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">8</span>  
</div></div>### 准备docker镜像[​](https://docs.tp4a.com/deploy/containerized.html#%E5%87%86%E5%A4%87docker%E9%95%9C%E5%83%8F)

提示

如果您能够直接访问 docker.hub 拉取镜像，那么可以跳过本小节，直接进入部署阶段。

因为一些周知的原因，国内难以从 docker.hub 拉取镜像，亦或者拉取镜像非常缓慢，另外就是可能您的环境不允许直接访问外部互联网。有鉴于此，我们提供了预制的 TacPort 节点服务的 docker 镜像文件。

请从 TP4A 的网站上（或者官方QQ群文件）下载docker镜像文件，然后在您本地服务器中导入。下面我们以 tacport v4.0.7-preview 版本为例进行说明。

下载得到的镜像文件名为 `tacport-server-4.0.7-preview-docker-img.tar.gz`，文件为使用 `docker save` 命令导出的并经过了 gnuzip 的压缩。要在您的环境中将镜像导入到 docker 运行环境，请执行：

<div class="vp-code-group vp-adaptive-theme" id="bkmrk-linuxmacos-shell" style="box-sizing: border-box; margin-top: 16px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="tabs" style="box-sizing: border-box; position: relative; display: flex; margin-right: 0px; margin-left: 0px; padding: 0px 12px; background-color: var(--vp-code-tab-bg); overflow: auto hidden; box-shadow: inset 0 -1px var(--vp-code-tab-divider); border-radius: 8px 8px 0px 0px;"><label data-title="Linux" for="tab-nL8BN_w" style="box-sizing: border-box; touch-action: manipulation; position: relative; display: inline-block; border-bottom: 1px solid transparent; padding: 0px 12px; line-height: 48px; font-size: 14px; font-weight: 500; color: var(--vp-code-tab-active-text-color); white-space: nowrap; cursor: pointer; transition: color 0.25s ease 0s;">Linux</label><label data-title="macOS" for="tab-9uLE1SS" style="box-sizing: border-box; touch-action: manipulation; position: relative; display: inline-block; border-bottom: 1px solid transparent; padding: 0px 12px; line-height: 48px; font-size: 14px; font-weight: 500; color: var(--vp-code-tab-text-color); white-space: nowrap; cursor: pointer; transition: color 0.25s ease 0s;">macOS</label></div><div class="blocks" style="box-sizing: border-box;"><div class="language-shell vp-adaptive-theme active line-numbers-mode" style="box-sizing: border-box; display: block; margin: 16px 0px; border-radius: 8px; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; padding-left: 32px;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">shell</span></div></div></div>```
gnuzip -c ./tacport-server-4.0.7-preview-docker-img.tar.gz | docker load

# 在较新的系统上，没有gnuzip，也可以使用gzip命令：
gzip -c -d ./tacport-server-4.0.7-preview-docker-img.tar.gz | docker load
```

<div class="vp-code-group vp-adaptive-theme" id="bkmrk-1234" style="box-sizing: border-box; margin-top: 16px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="blocks" style="box-sizing: border-box;"><div class="language-shell vp-adaptive-theme active line-numbers-mode" style="box-sizing: border-box; display: block; margin: 16px 0px; border-radius: 8px; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; padding-left: 32px;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">2</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">3</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">4</span>  
</div></div></div></div>注意

导入的镜像自带版本号为标签，方便后期进行版本管理。

提示

镜像名称会在很多地方引用，例如docker-compose的配置文件中。为避免每次升级后因镜像名称变动而重新修改这些引用镜像名称的地方，可以对导入的镜像设置一个通用的镜像标签，常见的做法是总是为最新的版本打上`latest`标签，例如：

<div class="language-shell vp-adaptive-theme line-numbers-mode" id="bkmrk-shell-1" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">shell</span></div>```
docker tag tp4a/tacport:4.0.7-preview tp4a/tacport:latest
```

<div class="language-shell vp-adaptive-theme line-numbers-mode" id="bkmrk-1" style="box-sizing: border-box; position: relative; margin: 16px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-radius: 8px; padding-left: 32px; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
</div></div>之后，您就可以在需要使用镜像名称的地方，总是使用`tp4a/tacport:latest`来引用最新版本的 TacPort 镜像了。

## 部署[​](https://docs.tp4a.com/deploy/containerized.html#%E9%83%A8%E7%BD%B2)

容器化部署有多种方式，例如 docker run，docker-compose，或者 K8s 等。我们这里以 docker-compose 为例进行部署说明。

注意

以下部署示例中，MySQL 和 Redis 均部署在运行 docker 的宿主机上，因此，在容器中访问宿主机上的服务时，宿主机可以通过 `host.docker.internal` 来访问。

如果您的环境并非如此，请在您的 docker-compose 配置文件中修改其服务地址。例如，访问其它容器中的服务，可以直接使用该服务所在的容器名作为主机地址。

在合适的目录中创建 `docker-compose.yaml` 文件，如果您已经有使用中的 docker-compose 配置文件，也可以在原配置文件基础上增加 TacPort 节点服务的内容。

### docker-compose 配置示例[​](https://docs.tp4a.com/deploy/containerized.html#docker-compose-%E9%85%8D%E7%BD%AE%E7%A4%BA%E4%BE%8B)

下面是一个运行 TacPort 服务的 docker-compose 配置文件示例：

<div class="vp-code-block-title" id="bkmrk-docker-compose.yaml-" style="box-sizing: border-box; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="vp-code-block-title-bar" style="box-sizing: border-box; position: relative; margin: 16px 0px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; font-size: 14px; font-weight: 500; color: var(--vp-code-tab-text-color); white-space: nowrap; transition: background-color 0.5s ease 0s; border-radius: 8px 8px 0px 0px; padding: 0px 12px; box-shadow: inset 0 -1px var(--vp-code-tab-divider);"><span class="vp-code-block-title-text" data-title="docker-compose.yaml" style="box-sizing: border-box; padding: 0px 12px; line-height: 48px;">docker-compose.yaml</span></div><div class="language-yaml vp-adaptive-theme line-numbers-mode" style="box-sizing: border-box; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; padding-left: 32px; margin: 0px !important 0px 16px 0px;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">yaml</span></div></div>```
services:
  tpnode1:
    image: tp4a/tacport:latest
    container_name: tpv4-node-1
    ports:
      - "7100:52100"
      - "7101:52101"
    environment:
      - TP_ALLOW_INIT=yes
      - TP_NODE_ID=1
      - TP_NODE_ADDR=tpv4.tp4a.com
      - TP_DB_ADDR=host.docker.internal:3306
      - TP_DB_USER=tacport
      - TP_DB_NAME=tacport
      - TP_DB_PASS=Pas5*w0rd
      - TP_SECRET_KEY=48V2%9+zmT
      - TP_REDIS_ADDR=host.docker.internal:6379
      - TP_NODE_SERVICE=api,ssh
      - TP_API_MAPPING_PORT=7100
      - TP_SSH_MAPPING_PORT=7101

    volumes:
      - /nfs-data/tacport-data:/opt/tacport/data
    deploy:
      resources:
        limits:
          cpus: "0.5"
          memory: 1024M
```

<div class="vp-code-block-title" id="bkmrk-12345678910111213141" style="box-sizing: border-box; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="language-yaml vp-adaptive-theme line-numbers-mode" style="box-sizing: border-box; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; padding-left: 32px; margin: 0px !important 0px 16px 0px;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">2</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">3</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">4</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">5</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">6</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">7</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">8</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">9</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">10</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">11</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">12</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">13</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">14</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">15</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">16</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">17</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">18</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">19</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">20</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">21</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">22</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">23</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">24</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">25</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">26</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">27</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">28</span>  
</div></div></div>下文将对此配置文件进行详细说明。

提示

您也可以使用 docker-compose 的 `env_file` 方式来提供环境变量。

警告

出于示例理解的考虑，这里的docker-compose配置文件中直接在环境变量中写入了密码/密钥等私密数据。实际应用时，应该使用docker-compose提供的`secrets`来保证安全性。

### docker-compose 配置详解[​](https://docs.tp4a.com/deploy/containerized.html#docker-compose-%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3)

需要重点关注通过环境变量方式对 TacPort 节点服务进行的配置。如果您对于配置项的作用不甚了解，可以查看[配置项::环境变量](https://docs.tp4a.com/deploy/configuration.html#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F)一文。

<div class="vp-code-block-title" id="bkmrk-docker-compose.yaml--1" style="box-sizing: border-box; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="vp-code-block-title-bar" style="box-sizing: border-box; position: relative; margin: 16px 0px 0px; background-color: var(--vp-code-block-bg); overflow-x: auto; font-size: 14px; font-weight: 500; color: var(--vp-code-tab-text-color); white-space: nowrap; transition: background-color 0.5s ease 0s; border-radius: 8px 8px 0px 0px; padding: 0px 12px; box-shadow: inset 0 -1px var(--vp-code-tab-divider);"><span class="vp-code-block-title-text" data-title="docker-compose.yaml" style="box-sizing: border-box; padding: 0px 12px; line-height: 48px;">docker-compose.yaml</span></div><div class="language-yaml vp-adaptive-theme line-numbers-mode" style="box-sizing: border-box; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; padding-left: 32px; margin: 0px !important 0px 16px 0px;"><button class="copy" style="box-sizing: border-box; touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; line-height: inherit; color: inherit; font-family: inherit; background-color: var(--vp-code-copy-code-bg); background-image: var(--vp-icon-copy); cursor: pointer; direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s ease 0s, background-color 0.25s ease 0s, opacity 0.25s ease 0s;" title="Copy Code"></button><span class="lang" style="box-sizing: border-box; position: absolute; top: 2px; right: 8px; z-index: 2; font-size: 12px; font-weight: 500; user-select: none; color: var(--vp-code-lang-color); transition: color 0.4s ease 0s, opacity 0.4s ease 0s;">yaml</span></div></div>```
services: # 定义要运行的服务
  tpnode1: # 服务的名称
    image: tp4a/tacport:latest # 创建此服务容器需要的镜像
    container_name: tpv4-node-1 # 创建的服务容器的名称
    ports: # 端口映射，冒号前为宿主机上的映射端口，后为容器内部的服务监听端口
      - "7100:52100" # 将默认的api服务端口 52100 映射到宿主机上的 7100 端口
      - "7101:52101" # 将默认的ssh协议转发服务端口 52101 映射到宿主机上的 7101 端口
    environment: # 通过环境变量的方式对 TacPort 节点服务进行配置
      - TP_ALLOW_INIT=yes # 首次部署时，允许执行初始化操作（初始化完成后请删除此项，或改为no，以增强安全性）
      - TP_NODE_ID=1 # 节点编号，多个节点实例需保证各个实例的节点编号唯一
      - TP_NODE_ADDR=tpv4.tp4a.com
      - TP_DB_ADDR=host.docker.internal:3306 # 容器内访问宿主机时可以用 host.docker.internal
      - TP_DB_USER=tacport # 可以省略，即，使用默认值
      - TP_DB_NAME=tacport # 可以省略，即，使用默认值
      - TP_DB_PASS=Pas5*w0rd # 建议使用 secrets 来管理私密数据
      - TP_SECRET_KEY=48V2%9+zmT # 建议使用 secrets 来管理私密数据
      - TP_REDIS_ADDR=host.docker.internal:6379
      - TP_NODE_SERVICE=api,ssh # 本节点提供API服务和SSH协议转发服务
      - TP_API_MAPPING_PORT=7100 # 需指明映射的宿主机的端口
      - TP_SSH_MAPPING_PORT=7101

    volumes: # 容器持久化存储，映射卷来存放配置/日志/审计录像文件等等。
      - ./data/tacport:/opt/tacport/data
    deploy: # 指定此容器的资源限制，请根据实际负载进行调整
      resources:
        limits:
          cpus: "0.5"
          memory: 1024M
```

<div class="vp-code-block-title" id="bkmrk-12345678910111213141-1" style="box-sizing: border-box; color: rgb(93, 93, 93); font-family: 'SF Pro Text', 'SF Pro Icons', 'PT Sans', Tahoma, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="language-yaml vp-adaptive-theme line-numbers-mode" style="box-sizing: border-box; position: relative; background-color: var(--vp-code-block-bg); overflow-x: auto; transition: background-color 0.5s ease 0s; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; padding-left: 32px; margin: 0px !important 0px 16px 0px;"><div aria-hidden="true" class="line-numbers-wrapper" style="box-sizing: border-box; position: absolute; top: 0px; bottom: 0px; left: 0px; z-index: 3; border-right: 1px solid var(--vp-code-block-divider-color); padding-top: 20px; width: 32px; text-align: center; font-family: var(--vp-font-family-mono); line-height: var(--vp-code-line-height); font-size: var(--vp-code-font-size); color: var(--vp-code-line-number-color); transition: border-color 0.5s ease 0s, color 0.5s ease 0s;"><span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">1</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">2</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">3</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">4</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">5</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">6</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">7</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">8</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">9</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">10</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">11</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">12</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">13</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">14</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">15</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">16</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">17</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">18</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">19</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">20</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">21</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">22</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">23</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">24</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">25</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">26</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">27</span>  
<span class="line-number" style="box-sizing: border-box; font-size: 11.2px; font-family: 'Operator Mono', Monaco, 'Lucida Console', Consolas, Courier, 'Courier New', monospace;">28</span>  
</div></div></div>### 启动服务[​](https://docs.tp4a.com/deploy/containerized.html#%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1)

执行 `docker compose up` 来启动服务，此时可以从控制台看到节点服务的日志输出，按 `Ctrl+C` 来结束服务。可以增加参数 `-d` 来将服务放到后台运行（比较控制台关闭时将服务也关闭了），像这样：`docker compose up -d`。

服务启动后，可以访问 `http://TP_容器宿主机IP:API服务映射端口` 来访问 TacPort 服务了，在本例中，地址为 `http://TP_容器宿主机IP:7100`。

如同常规部署一样，如果运行时检测到数据库尚未创建或初始化，需要访问初始化页面，参见 [常规配置：初始化](https://docs.tp4a.com/deploy/regular.html#%E5%88%9D%E5%A7%8B%E5%8C%96) 一节。在本例中，初始化页面地址为 `http://TP_容器宿主机IP:7100/init`。

## 服务日常管理[​](https://docs.tp4a.com/deploy/containerized.html#%E6%9C%8D%E5%8A%A1%E6%97%A5%E5%B8%B8%E7%AE%A1%E7%90%86)

#### 使用docker命令管理镜像[​](https://docs.tp4a.com/deploy/containerized.html#%E4%BD%BF%E7%94%A8docker%E5%91%BD%E4%BB%A4%E7%AE%A1%E7%90%86%E9%95%9C%E5%83%8F)

- 查看服务：`docker ps`
- 查看镜像：`docker images`
- 为镜像打tag: `docker tag 源镜像名[:TAG] 新镜像名[:TAG]`，例如：`docker tag tp4a/tacport:4.0.7 tp4a/tacport:latest`。需要注意的是，打完tag之后，原来的`镜像:tag`还是存在的，如果原来的标签不需要再使用了，可以将其删除。
- 删除镜像/标签：`docker rmi 镜像名[:TAG]`，一个镜像可以有多个标签，当最后一个标签被删除时，才会删除镜像本身。

#### 使用 docker-compose管理服务[​](https://docs.tp4a.com/deploy/containerized.html#%E4%BD%BF%E7%94%A8-docker-compose%E7%AE%A1%E7%90%86%E6%9C%8D%E5%8A%A1)

以下操作需在 docker-compose 配置文件所在目录下执行，或者命令行上用 `-f` 参数来指定配置文件。

- 启动服务：`docker compose up -d` 其中 `-d` 参数可将docker-compose进程放到后台执行
- 停止服务：`docker compose down`
- 重启服务：修改了配置文件后，重新执行 `docker-compose up -d` 即可
- 重启某个服务：`docker compose restart 服务名`，例如，重启第一个节点服务，可以 `docker compose restart tpnode1`
- 查看服务：`docker compose ps`
- 查看状态：`docker compose stats` 查看各容器实时负载状态，按`Ctrl-C`退出

# ubuntu24.04安装redis

在 **Ubuntu 24.04** 上安装 Redis 有两种常用方式：

---

## 🚀 方法一：直接用 apt 安装（最简单）

Ubuntu 24.04 自带 **redis-server** 包，直接安装即可。

```bash
sudo apt update
sudo apt install -y redis-server

```

安装后启动：

```bash
sudo systemctl enable redis-server
sudo systemctl start redis-server

```

验证：

```bash
redis-cli ping

```

返回：

```
PONG

```

说明 Redis 已正常运行。

---

## 🚀 方法二：用 Redis 官方仓库安装（获取最新版本）

Ubuntu 官方仓库版本可能较旧，如果要用 Redis 官方最新稳定版，可以添加官方 repo：

1. 安装依赖：

```bash
sudo apt update
sudo apt install -y curl lsb-release gpg

```

2. 添加 Redis 官方 GPG key 和仓库：

```bash
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb \
$(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

```

3. 安装 Redis：

```bash
sudo apt update
sudo apt install -y redis

```

---

## 🔧 可选配置

1. **允许远程访问**  
    编辑配置文件：
    
    ```bash
    sudo nano /etc/redis/redis.conf
    
    ```
    
    修改：
    
    ```
    bind 127.0.0.1 ::1
    
    ```
    
    为：
    
    ```
    bind 0.0.0.0
    
    ```
    
    同时确保：
    
    ```
    protected-mode no
    
    ```
    
    然后重启：
    
    ```bash
    sudo systemctl restart redis-server
    
    ```
2. **设置密码**  
    在 `redis.conf` 里添加：
    
    ```
    requirepass 你的密码
    
    ```
3. **验证版本**
    
    ```bash
    redis-server --version
    
    ```

---

✅ 总结：

- **简单场景**：`apt install redis-server` 就能用。
- **要新版本**：用 Redis 官方仓库。
- **生产环境**：建议绑定 0.0.0.0 + 设置密码 + 防火墙控制访问。

---

要不要我帮你整理一个 **Ubuntu 24.04 一键安装 + 配置 Redis（含远程访问和密码）脚本**？

# ubuntu系统跳转或透明转发实现的几种方式

非常好的问题 👍  
你现在的需求可以理解为：

> 所有访问 **47.236.101.216:6011** 的流量，都透明地转发（或跳转）到 **47.241.11.4:6011**。

在 Ubuntu 14.04（内核 3.x 时代）下，可以有几种成熟的实现方式，取决于你希望“转发”的层次（L3/L4）与透明度。  
下面我给你推荐几种方案，按推荐优先级从高到低排列👇

---

## ✅ **方案一：使用 iptables DNAT（推荐）**

如果两台服务器在公网都可访问，最简洁、最稳定的方式就是 **iptables DNAT 转发**。

### 1️⃣ 编辑转发规则：

```bash
# 启用IP转发
echo 1 > /proc/sys/net/ipv4/ip_forward
sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

# 清空旧规则（可选）
iptables -t nat -F

# 设置DNAT转发规则：将所有访问本机6011端口的流量转发到另一台服务器
iptables -t nat -A PREROUTING -p tcp --dport 6011 -j DNAT --to-destination 47.241.11.4:6011

# SNAT确保返回流量走回原服务器
iptables -t nat -A POSTROUTING -p tcp -d 47.241.11.4 --dport 6011 -j SNAT --to-source 47.236.101.216

```

### 2️⃣ 保存规则

Ubuntu 14.04 默认没 `iptables-persistent`，可以手动保存：

```bash
apt-get install iptables-persistent -y
service netfilter-persistent save

```

或使用：

```bash
iptables-save > /etc/iptables.rules

```

然后在 `/etc/network/interfaces` 的 `post-up` 中添加：

```bash
post-up iptables-restore < /etc/iptables.rules

```

---

## ✅ **方案二：用 `socat` 实现 TCP 端口中继**

若不想修改系统防火墙（或仅临时转发），`socat` 是轻量好用的方案。

### 1️⃣ 安装

```bash
apt-get install socat -y

```

### 2️⃣ 启动转发

```bash
socat TCP-LISTEN:6011,fork TCP:47.241.11.4:6011

```

这会将所有到 `6011` 的连接直接中继到 `47.241.11.4:6011`。

### 3️⃣ 后台运行（长期服务）

可以写入 `/etc/rc.local`：

```bash
/usr/bin/socat TCP-LISTEN:6011,fork TCP:47.241.11.4:6011 &

```

或者用 `screen` / `nohup`：

```bash
nohup socat TCP-LISTEN:6011,fork TCP:47.241.11.4:6011 >/var/log/socat_6011.log 2>&1 &

```

---

## ✅ **方案三：使用 Nginx TCP 反向代理（更可控）**

如果你已有 Nginx，可以用它转发 TCP 流量。

1️⃣ 安装 stream 模块版本：

```bash
apt-get install nginx-extras -y

```

2️⃣ 编辑 `/etc/nginx/nginx.conf` 增加：

```nginx
stream {
    server {
        listen 6011;
        proxy_pass 47.241.11.4:6011;
    }
}

```

3️⃣ 重新加载配置：

```bash
nginx -t && service nginx restart

```

---

## ⚙️ 对比建议

<table id="bkmrk-%E6%96%B9%E6%A1%88-%E6%80%A7%E8%83%BD-%E6%8C%81%E4%B9%85%E6%80%A7-%E8%B0%83%E8%AF%95%E4%BE%BF%E5%88%A9%E6%80%A7-%E6%8E%A8%E8%8D%90%E5%9C%BA%E6%99%AF"><thead><tr><th>方案</th><th>性能</th><th>持久性</th><th>调试便利性</th><th>推荐场景</th></tr></thead><tbody><tr><td>**iptables DNAT**</td><td>⭐⭐⭐⭐</td><td>永久有效</td><td>略复杂</td><td>系统级透明转发（生产推荐）</td></tr><tr><td>**socat**</td><td>⭐⭐</td><td>临时转发方便</td><td>简单易改</td><td>测试或过渡用</td></tr><tr><td>**nginx stream**</td><td>⭐⭐⭐</td><td>稳定可控</td><td>日志支持好</td><td>若已有 Nginx 环境</td></tr></tbody></table>

---

## 🔍 验证

在另一主机上测试：

```bash
telnet 47.236.101.216 6011

```

或：

```bash
curl http://47.236.101.216:6011

```

然后查看 47.241.11.4 是否收到请求。

---

我建议你优先用 **iptables DNAT**，因为它在内核层转发，性能最好、透明度最高、适合生产服务器。  
如果你告诉我：

- 两台服务器是否在同一内网或仅公网互通，
- 是否需要保持源 IP（即被转发请求的真实来源 IP），

我可以进一步给出优化方案（比如保留原始客户端 IP 的 DNAT + SNAT 精细配置）。

是否希望我帮你写一版完整可执行、带日志与开机自启的 iptables 方案脚本？

# 在 Kylin Linux Advanced Server V11 (Swan25) 上安装浏览器

在 Kylin Linux Advanced Server V11 (Swan25) 上安装浏览器，你可以根据硬件架构和网络情况选择不同的方法。虽然搜索结果中没有直接针对V11版本的指南，但以下方案综合了多个可靠来源，并已为你梳理了操作要点。

\### 🖥️ 硬件架构判断

在开始安装前，\*\*务必先确认你电脑的CPU架构\*\*，这直接决定了你应该下载哪个版本的浏览器。你可以通过以下命令查看：

```bash  
lscpu  
```  
在输出信息中，找到 \*\*“Architecture”\*\* 字段。常见的架构有：  
\- \*\*x86\_64\*\*：通常指英特尔或AMD的CPU。  
\- \*\*aarch64\*\* 或 \*\*arm64\*\*：指ARM架构的CPU，例如飞腾。  
\- \*\*mips64\*\* 或 \*\*loongarch64\*\*：指龙芯等国产CPU。

\### 🌐 主流浏览器安装方案

确认架构后，你可以参考下面的表格选择适合的浏览器和安装方式。

| 浏览器 | 推荐安装方法 | 适用架构 | 关键命令/步骤 | 注意事项 |  
| :--- | :--- | :--- | :--- | :--- |  
| \*\*Chromium\*\* &lt;br&gt;(Chrome开源版) | \*\*在线安装 (APT)\*\* | x86\_64, aarch64 | 1. `sudo apt update`&lt;br&gt;2. `sudo apt install chromium-browser chromium-browser-l10n` | 确保软件源已更新。如果找不到包，可能需要配置额外的软件源。 |  
| \*\*Google Chrome\*\* | \*\*离线安装 (RPM)\*\* | x86\_64 | 1. 从官网下载`.rpm`包。&lt;br&gt;2. 安装依赖：`sudo yum -y install vulkan-loader.x86\_64`。&lt;br&gt;3. 安装包：`sudo rpm -ivh google-chrome-stable\_current\_x86\_64.rpm`。&lt;br&gt;4. \*\*重要\*\*：编辑`/opt/google/chrome/google-chrome`文件，在指定行后添加`--user-data-dir --no-sandbox`参数。 | 安装过程可能需要解决其他依赖问题。Root用户运行时必须添加`--no-sandbox`参数。 |  
| \*\*龙芯浏览器\*\* &lt;br&gt;(Loongnix Browser) | \*\*在线/离线安装\*\* | \*\*mips64, loongarch64\*\* &lt;br&gt;(龙芯CPU专用) | 1. \*\*在线\*\*：`sudo apt install lbrowser`。&lt;br&gt;2. \*\*离线\*\*：从龙芯开源社区下载对应架构的安装包，使用`dpkg`或`rpm`命令安装。 | 这是为龙芯架构优化的浏览器，其他架构无法使用。 |  
<span style="color: rgb(224, 62, 45);">**| \*\*Firefox\*\* | \*\*通过Snap安装\*\* | 多架构 | 1. 先安装Snap：&lt;br&gt; `sudo apt-get install snapd`&lt;br&gt; `sudo systemctl enable --now snapd`。&lt;br&gt;2. 安装Firefox：`sudo snap install firefox`。 | 此方法可以安装最新版本。如果遇到文件选择对话框问题，需安装`xdg-desktop-portal-gtk`等组件。 |**</span>

\### 🔧 安装通用步骤与故障排查

\- \*\*更新软件源\*\*：在安装任何软件前，首先运行 `sudo apt update`（适用于DEB包）或 `sudo yum makecache`（适用于RPM包）来更新软件源列表，这能确保系统获取到最新的软件信息。  
\- \*\*搜索可用软件包\*\*：如果不确定软件包的确切名称，可以使用命令 `apt-cache search chromium` 或 `yum list available | grep -i browser` 进行搜索。  
\- \*\*解决Flash插件问题\*\*：如果使用Firefox时提示缺少Flash插件，可以通过命令 `sudo apt-get install -y kylin-flash-plugin` 来安装。  
\- \*\*处理密钥环密码弹窗\*\*：安装Chrome后若反复弹出“输入密钥环密码”的窗口，可以尝试通过命令 `sudo mv /usr/bin/gnome-keyring-daemon /usr/bin/gnome-keyring-daemon.bak` 并重启系统来解决。

希望这些步骤能帮助你在Kylin Linux V11上顺利安装浏览器。如果你能告知你电脑的具体CPU架构，我可以为你提供更精确的安装建议。

# Wubuntu（Windows Ubuntu）系统安装远程桌面 的完整方案

下面我给你一套 **Wubuntu（Windows Ubuntu）系统安装远程桌面** 的完整方案。Wubuntu 基于 Ubuntu，所以远程桌面方式与 Ubuntu 基本一致。

你可以选择两种方式：

1. **XRDP（最简单，支持 Windows 远程桌面 mstsc）**
2. **VNC（适合图形速度要求高）**

下面推荐你使用 **XRDP**，因为你可以直接用 Windows 的“远程桌面连接”访问。

---

# ✅ 方法一：安装 XRDP（最推荐）

## 1. 安装 XRDP 服务

```bash
sudo apt update
sudo apt install xrdp -y

```

## 2. 启动并设置开机自启

```bash
sudo systemctl enable xrdp --now

```

## 3. 添加你的用户到 ssl-cert 组（否则无法登录）

```bash
sudo adduser $USER ssl-cert

```

如果你想指定某个用户，例如 `shuncom`：

```bash
sudo adduser shuncom ssl-cert

```

## 4. 查看 XRDP 是否正在运行

```bash
sudo systemctl status xrdp

```

显示 active (running) 即成功。

---

# ⚙️ 可选优化：将 Xorg 设置为默认（避免黑屏）

编辑配置文件：

```bash
sudo vi /etc/xrdp/startwm.sh

```

将最后的这两行：

```
test -x /etc/X11/Xsession && exec /etc/X11/Xsession
exec /bin/sh /etc/X11/Xsession

```

替换成：

```
startxfce4

```

如果你没有安装桌面环境，则执行：

### ⭐ 安装 XFCE 桌面（轻量稳定推荐）

```bash
sudo apt install xfce4 -y

```

---

# 🖥️ 5. 从 Windows 连接

按键：`Win + R`  
输入：

```
mstsc

```

然后输入：

```
wubuntu 的 IP 地址

```

例如：

```
192.168.1.50

```

输入你 Wubuntu 的用户名 + 密码即可登录。

---

# 🔥 XRDP 常见问题解决

### ❗ 登录后黑屏 → 桌面环境未安装

执行：

```bash
sudo apt install xfce4 -y

```

### ❗ 登录被踢出 → 本地已登录图形界面

Ubuntu 不允许同账号同时图形登录  
解决：使用新建用户：

```bash
sudo adduser rdpuser

```

然后用 `rdpuser` 登录 XRDP。

---

# ⭐ 方法二：安装 VNC（如果你更喜欢自托管、性能更好）

可以使用 TigerVNC：

```bash
sudo apt install tigervnc-standalone-server tigervnc-common -y

```

启动：

```bash
vncserver

```

然后从其他电脑使用 **VNC Viewer** 连接即可。

---

# 📌 如果你告诉我你的 Wubuntu 的版本和桌面环境（KDE、GNOME、XFCE），我可以给你量身定制最兼容的 XRDP 配置。

Wubuntu 本质是 **Ubuntu + Windows 混合桌面（KDE）**，所以中文输入法安装方式与 Ubuntu 完全相同。  
下面我给你 **最稳定、兼容性最高的中文输入法方案（fcitx5）**，支持拼音、双拼、五笔。

---

# ✅ 推荐：安装 **Fcitx5 中文输入法（Ubuntu 标准方案）**

## 1️⃣ 安装 Fcitx5 及中文模块

```bash
sudo apt update
sudo apt install -y fcitx5 fcitx5-config-qt fcitx5-chinese-addons fcitx5-frontend-qt5 fcitx5-frontend-gtk3 fcitx5-frontend-gtk4

```

安装中文词库（拼音/五笔）：

```bash
sudo apt install -y fcitx5-pinyin fcitx5-pinyin-zhwiki

```

---

# 2️⃣ 设置系统输入法环境变量（关键）

执行：

```bash
im-config -n fcitx5

```

如果你想手动设置环境变量（某些 Wubuntu 版本需要），在 `~/.pam_environment` 加：

```bash
GTK_IM_MODULE=fcitx5
QT_IM_MODULE=fcitx5
XMODIFIERS="@im=fcitx5"

```

或者用命令添加：

```bash
echo 'GTK_IM_MODULE=fcitx5' >> ~/.pam_environment
echo 'QT_IM_MODULE=fcitx5' >> ~/.pam_environment
echo 'XMODIFIERS="@im=fcitx5"' >> ~/.pam_environment

```

---

# 3️⃣ 重启 Wubuntu

```bash
reboot

```

---

# 4️⃣ 初始化 Fcitx5，添加中文输入法

重启后，任务栏右下角会出现 **小键盘图标**。

如果没有，你可以手动打开：

```bash
fcitx5-configtool

```

在“输入法”中：

1. 点击「+」
2. 搜索：**Pinyin（拼音）**
3. 或 **Wubi（五笔）**
4. 添加即可

---

# 🎉 现在你可以在应用中按：

```
Ctrl + Space

```

在 **英文 / 中文输入法** 之间切换。

---

# ❗ 如果你用的是 KDE Plasma（Wubuntu 默认）

你也可以从 KDE 系统设置进入：

**系统设置 → 区域与语言 → 输入法 → 启用 Fcitx5**

---

# 🔥 常见问题处理

### 1. 输入法不显示？

```bash
fcitx5

```

如果能启动，请设置为开机启动。

### 2. 应用里无法输入中文（如 Chrome、微信等）？

确保你装了所有 Qt/Gtk 前端：

```bash
sudo apt install -y fcitx5-frontend-gtk3 fcitx5-frontend-gtk4 fcitx5-frontend-qt5

```

---

# 📌 如果你告诉我：

- 你用的是 **Wubuntu KDE** 还是 Wubuntu GNOME？
- 是否使用 WeChat、QQ、Chrome、VSCode 等应用？

我可以帮你进一步做 **自动化输入法配置脚本**，一键搞定。

# 通过命令行手动添加多个子 IP

可以的！  
在 **Windows Server 2016** 中，即使网卡是 **DHCP 自动获取 IP**，你仍然可以 **通过命令行手动添加多个子 IP（Secondary IP / 新的 IP Alias）**。

✔ 支持 DHCP 主 IP + 静态子 IP  
✔ 命令行可添加  
✔ 删除也可以用命令行

下面是最实用的命令方法。

---

# ✅ 方法 1：使用 `netsh` 命令添加子 IP（最兼容最稳）

格式：

```cmd
netsh interface ip add address "网卡名称" 子IP 子掩码

```

例如：

给 DHCP 网卡添加 **10.2.2.100/24**：

```cmd
netsh interface ip add address "Ethernet" 10.2.2.100 255.255.255.0

```

查看网卡名称：

```cmd
netsh interface ipv4 show interfaces

```

删除子 IP：

```cmd
netsh interface ip delete address "Ethernet" 10.2.2.100 255.255.255.0

```

---

# ✅ 方法 2：使用 PowerShell（Windows Server 2012+ 推荐）

PowerShell 直接支持为 DHCP 网卡添加静态子 IP：

## 📌 添加子 IP

```powershell
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 10.2.2.100 -PrefixLength 24 -AddressFamily IPv4

```

如果你有多张网卡，可查看别名：

```powershell
Get-NetAdapter

```

## 📌 删除子 IP

```powershell
Remove-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 10.2.2.100 -Confirm:$false

```

---

# 🔥 常见问题解答

## ❓1. 主 IP 是 DHCP，会不会被覆盖？

不会。

DHCP 主 IP 属于：

```
NetIPAddress(Type = Unicast)

```

你添加的子 IP 属于：

```
NetIPAddress(Type = Unicast, but manual)

```

两者共存，不冲突。

---

## ❓2. 添加多个子 IP，可以吗？

可以，添加多个直接重复执行命令即可。

---

## ❓3. 重启后会生效吗？

✔ 子 IP **永久生效**，重启服务器不会丢失  
（除非你修改网卡绑定或卸载 NIC）

---

## ❓4. DHCP 改变主 IP 会不会影响子 IP？

不会影响，子 IP 始终保持静态。

---

# 🧪 如果你愿意，我可以为你生成：

✔ 完整的「一键添加多个子 IP」脚本（CMD 或 PowerShell）  
✔ 自动识别网卡 + 添加子 IP  
✔ 生产环境专用版本（带日志、验证、错误检查）

只要你提供：

- 网卡名称或段（如 "Ethernet"）
- 需要添加的子 IP 列表

我立即生成。

# 在ubuntu系统上为rtx 3090独立显卡安装驱动

### AI推荐方法

看到你的GeForce RTX 3090显卡已经识别出了多个可用驱动，其中还标记了推荐版本，这为后续安装打下了很好的基础。在Ubuntu系统上安装驱动程序有多种方法，我来为你详细梳理一下。

\### 🔍 了解驱动安装的几种方式

首先，你可以根据下面的表格了解几种主要的安装方式及其特点：

| 安装方法 | 适用场景 | 优点 | 注意事项 |  
| :--- | :--- | :--- | :--- |  
| \*\*`ubuntu-drivers`自动安装\*\* | 新手用户，希望安装系统推荐的稳定驱动 | 自动处理依赖和版本选择，最省心 | 版本可能不是最新的 |  
| \*\*`apt`安装指定版本\*\* | 需要特定版本驱动，或更新版本的驱动 | 灵活性高，可自主选择 | 需自行确保版本兼容性 |  
| \*\*PPA仓库安装\*\* | 追求最新的驱动程序 | 版本通常比官方仓库更新 | 可能存在稳定性风险 |  
| \*\*手动编译安装\*\* | 硬件非常新或有特殊定制需求 | 可应对无现成安装包的情况 | 过程复杂，需自行解决依赖和冲突 |

\### 🛠️ 驱动安装步骤详解

\*\*使用 `ubuntu-drivers` 自动安装（推荐）\*\*

这是最简单的方法，尤其适合新手。系统会自动为你安装标记为"recommended"的驱动（在你的情况中是 `nvidia-driver-580`）。

1\. \*\*安装推荐驱动\*\*：打开终端，直接运行以下命令：  
 ```bash  
 sudo ubuntu-drivers autoinstall  
 ```  
 这个命令会自动安装所有需要的依赖，并设置好推荐驱动。

2\. \*\*重启系统\*\*：安装完成后，\*\*务必重启你的电脑\*\*，以使新驱动生效。  
 ```bash  
 sudo reboot  
 ```

\*\*使用 `apt` 安装指定版本\*\*

如果你想安装一个特定的版本（比如出于兼容性考虑），可以这样做：

1\. \*\*更新软件包列表\*\*：  
 ```bash  
 sudo apt update  
 ```

2\. \*\*安装指定的驱动版本\*\*：从 `ubuntu-drivers devices` 列出的驱动中选择一个，例如安装推荐的580版本：  
 ```bash  
 sudo apt install nvidia-driver-580  
 ```  
 你也可以选择其他列出的版本，如 `nvidia-driver-535` 或 `nvidia-driver-470`。

3\. \*\*重启系统\*\*：  
 ```bash  
 sudo reboot  
 ```

\*\*通过PPA安装新版驱动\*\*

如果你想尝试更新的驱动版本，可以添加Graphic Drivers团队的PPA仓库。

1\. \*\*添加PPA仓库并更新\*\*：  
 ```bash  
 sudo add-apt-repository ppa:graphics-drivers/ppa  
 sudo apt update  
 ```

2\. \*\*安装驱动\*\*：之后，你就可以使用 `apt` 命令安装该PPA提供的新版驱动了。  
 ```bash  
 sudo apt install nvidia-driver-xxx # 将xxx替换为PPA中提供的具体版本号  
 ```

3\. \*\*重启系统\*\*：  
 ```bash  
 sudo reboot  
 ```

\### 🔧 安装后的验证与故障排查

\- \*\*验证驱动加载\*\*：安装并重启后，你可以通过以下命令检查NVIDIA驱动是否正常运行：  
 ```bash  
 nvidia-smi  
 ```  
 这个命令会显示你的GPU信息以及驱动版本，如果正常显示，则说明驱动安装成功。

\- \*\*解决常见问题\*\*：  
\- \*\*如果驱动未生效\*\*：可以尝试手动加载驱动模块：  
 ```bash  
 sudo modprobe nvidia  
 ```  
\- \*\*安装依赖\*\*：在某些情况下，如果选择手动安装或遇到问题，可能需要先安装编译工具和内核头文件：  
 ```bash  
 sudo apt install build-essential linux-headers-$(uname -r)  
 ```

\### 💎 总结

对于大多数用户，我首推使用 `sudo ubuntu-drivers autoinstall` 命令让系统自动完成所有工作，这是最稳妥的方案。如果你的RTX 3090显卡在使用中遇到任何特定问题，或者你对驱动版本有特殊要求，欢迎提供更多细节，我可以再帮你进一步分析。

希望这些步骤能帮助你顺利在Ubuntu上安装好NVIDIA显卡驱动！

### 其它操作方法

ubuntu系统下显卡3090配置

1\. 更新系统

sudo apt update &amp;&amp; sudo apt upgrade -y

确保系统软件包是最新的。  
2\. 禁用 Nouveau 驱动

Ubuntu 自带 Nouveau 开源驱动，可能会与 NVIDIA 官方驱动冲突，因此需要禁用它。  
检查 Nouveau 是否启用

lsmod | grep nouveau

如果有输出，说明 Nouveau 已加载，需要禁用。  
禁用 Nouveau

sudo bash -c "echo -e 'blacklist nouveau\\noptions nouveau modeset=0' &gt; /etc/modprobe.d/blacklist-nouveau.conf"

sudo update-initramfs -u

重启系统

sudo reboot

3\. 安装 NVIDIA 驱动  
方法 1：使用 Ubuntu 官方驱动

 检测可用的 NVIDIA 驱动版本

ubuntu-drivers devices

示例输出：

vendor : NVIDIA Corporation  
model : NVIDIA GeForce RTX 3090  
driver : nvidia-driver-535 - recommended  
driver : nvidia-driver-525

其中 nvidia-driver-535 是推荐驱动。

安装推荐驱动

sudo apt install -y nvidia-driver-535

如果输出推荐不同的版本（如 nvidia-driver-525），请替换命令中的版本号。

安装完成后，重启系统

sudo reboot

# GitHub 已经禁止密码方式拉取代码解决方法

GitHub **已经禁止密码方式拉取代码**（2021 年就关闭了）。  
现在只能使用：

✅ **PAT（Personal Access Token）个人访问令牌**  
或  
✅ **SSH Key**

你出现的报错：

```
Password authentication is not supported for Git operations.

```

说明你输入 GitHub 密码 → **不支持**。

下面给你最简单、100% 可成功的解决方法。

---

# ✅ 方案 1：使用 GitHub Token（最简单）

## **步骤 1：在 GitHub 创建 Token**

1. 登录 GitHub
2. 打开：  
    [https://github.com/settings/tokens](https://github.com/settings/tokens)
3. 点击 **Generate new token → Classic**
4. 勾选权限（够用即可）：
    
    
    - ✔ repo
    - ✔ read:user
5. 生成后复制 Token（只显示一次）

---

## **步骤 2：用 Token 替代密码**

你的命令保持不变：

```bash
git clone https://github.com/o2ospring/mywebsite.git

```

提示输入用户名时填：

```
pxlu@qq.com

```

提示输入密码时，不要填 GitHub 密码，填：

```
<你的 Token>

```

✔ 成功克隆

---

# ❤️ 推荐方案 2：使用 SSH Key（最永久可靠）

## **步骤 1：生成 SSH Key**

```bash
ssh-keygen -t ed25519 -C "pxlu@qq.com"

```

一路回车即可。

生成文件：

```
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub

```

---

## **步骤 2：添加公钥到 GitHub**

```bash
cat ~/.ssh/id_ed25519.pub

```

复制内容，打开：

[https://github.com/settings/keys](https://github.com/settings/keys)

→ New SSH key  
→ 粘贴保存。

---

## **步骤 3：用 SSH 克隆仓库**

你的仓库地址：

```bash
git clone git@github.com:o2ospring/mywebsite.git

```

✔ 无需用户名/密码  
✔ 永远免登录  
✔ 服务器上最推荐使用 SSH

---

# 🔧 如果你愿意，我可以帮你：

- 生成你的 SSH 连接命令
- 生成 .ssh/config（自动识别多个 Key）
- 生成 Token
- 排查 GitHub 权限问题

只需要告诉我你偏向 **Token** 还是 **SSH key**。

# Ubuntu 18.04 下测试硬盘 IOPS 上限的工具及hyper对iosp限制的优化

在 **Ubuntu 18.04** 下测试硬盘 **IOPS 上限**，最标准、准确的工具是：

# ✅ **首选：fio（Linux 官方推荐 I/O 压测工具）**

Ubuntu18.04 自带 apt 包，可直接安装：

```bash
sudo apt-get update
sudo apt-get install fio

```

---

# 🚀 **一条命令测试硬盘 IOPS 上限（随机 4K）**

## **1）随机写 IOPS 上限（4K）**

```bash
fio --name=4k-randwrite --filename=/data/testfile --size=2G \
--bs=4k --rw=randwrite --iodepth=32 --numjobs=4 --direct=1 --time_based \
--runtime=30 --group_reporting

```

## **2）随机读 IOPS 上限（4K）**

```bash
fio --name=4k-randread --filename=/data/testfile --size=2G \
--bs=4k --rw=randread --iodepth=32 --numjobs=4 --direct=1 --time_based \
--runtime=30 --group_reporting

```

## 参数说明（关键）：

- `--bs=4k` → 4K 块，测数据库最常用 IOPS
- `--rw=randread` 或 `randwrite` → 随机读/写
- `--numjobs=4` → 相当于 4 个线程（可根据 CPU 增加）
- `--iodepth=32` → 队列深度
- `--direct=1` → 绕过缓存，真实测试磁盘
- `--runtime=30` → 运行 30 秒
- `--group_reporting` → 整合输出

---

# 🔥 **测 IOPS 上限的最强命令（压榨到极限）**

适用于 NVMe / RAID / SSD：

```bash
# 随机读 4k极限测试
fio --name=max-iops --filename=/home/testfile --size=4G \
--rw=randread --bs=4k --numjobs=8 --iodepth=64 --direct=1 \
--time_based --runtime=60 --ioengine=libaio --group_reporting

# 随机读 减少并发，更适合HDD
fio --name=test --filename=/home/testfile --size=4G \
    --rw=randread --bs=64k --numjobs=4 --iodepth=16 \
    --direct=1 --runtime=60 --ioengine=libaio --group_reporting

# 随机写 更接近 MySQL / 日志型业务
fio --name=real-write \
--filename=/home/testfile \
--size=10G \
--rw=randwrite \
--bs=4k \
--numjobs=4 \
--iodepth=16 \
--direct=1 \
--time_based \
--runtime=60 \
--ioengine=libaio \
--group_reporting
```

适合直接看你的硬盘理论上限。

---

# ✨ 输出中重点看：

- **IOPS**（最关键）
- `lat (usec)` 平均延迟
- `clat` 99%、99.99% 延迟
- `bw`（带宽）

---

# 🧪 **附：连续写（顺序写）带宽测试**

顺序写：

```bash
fio --name=seqwrite --filename=/data/testfile --size=4G \
--bs=1M --rw=write --direct=1 --runtime=30 --group_reporting

```

顺序读：

```bash
fio --name=seqread --filename=/data/testfile --size=4G \
--bs=1M --rw=read --direct=1 --runtime=30 --group_reporting

```

---

# ❗注意事项

- 测试前必须确保目录里有足够空间
- 测试会产生文件（测试后删除即可）
- 如果是生产服务器，请避免测试期间影响业务
- `--direct=1` 必须开启，否则测试的是缓存不是磁盘

---

# 如果你愿意

你可以把 fio 输出发给我，我能帮你分析：

✔ IOPS 是否正常  
✔ 是否被 CPU / RAID / 控制器限制  
✔ 能否进一步优化 IO 性能（比如调整 iodepth/numjobs）

随时发我即可。

下面根据你的 **fio 测试结果、Hyper-V 架构特点、虚拟机磁盘类型** 等，分析是否存在瓶颈、是否能“解除 Hyper-V 限制”，以及实际可优化点。

---

# ✅ **结论：你看到的 IOPS ≈ 17k，属于 Hyper-V 虚拟化下的典型瓶颈，确实可以继续优化，但无法完全突破虚拟层的结构限制。**

IOPS 低的主要原因不是 fio 命令，而是：

1. **虚拟磁盘类型（VHD/VHDX）造成的性能损耗**
2. **Hyper-V 控制器结构（虚拟化开销）**
3. **未启用 write-back cache / 未使用直通磁盘**
4. **宿主机物理磁盘性能也被虚拟化稀释**

下面按实际能提升的方向给你完整方案。

---

# 📉 **你的 fio IOPS（17K）说明的问题**

```
read: IOPS=17.4k, BW=67.8MiB/s
99% 延迟 > 10ms，最大甚至 >1sec
磁盘利用率 util = 100%

```

这说明：

- **宿主机磁盘排队严重（in\_queue=9393724）**
- **虚拟磁盘队列深度被 Hyper-V 限制**
- **I/O 上下文切换高、虚拟化开销大**

即使你的宿主机 NVMe 能 300K～600K IOPS，虚机里一般只能看到 **1万～3万 IOPS**（典型值）。

你现在 17k 已经属于“典型 Hyper-V IOPS 区间”，不是异常。

---

# 🧨 **能否解除 Hyper-V 对 IOPS 的限制？**

⚠ 坦率说："完全解除" 不可能。

但能通过多项优化 **从 17K 提升到 50K～100K IOPS（甚至更高）**，取决于宿主 NVMe 性能。

---

# 🚀 **Hyper-V 提升 IOPS 的实用优化方案（按提升幅度排序）**

---

## **① 使用直通磁盘（Pass-through Disk）—— IOPS 提升幅度最大（2～10 倍）**

如果你把虚拟机磁盘改成：

✔ 直通物理磁盘  
✔ 直接映射 NVMe

你能直接看到宿主磁盘的全部性能。

### **配置方法：**

1. Hyper-V 管理器 → 选择虚拟机 → 设置
2. SCSI 控制器 → 添加磁盘
3. 选择 **物理硬盘（Pass-Through Disk）**

**IOPS 能从 17k → 150k～300k+**

---

## **② 使用固定大小 VHDX（Fixed VHDX）而不是动态磁盘（Dynamic VHDX）**

动态扩展 VHDX 性能最差，会导致：

- 额外的元数据更新
- 写放大
- 队列阻塞

📈 固定大小 VHDX（Fixed-size VHDX）性能可提升 **1.5～3倍**

---

## **③ 启用 Write-Back Cache（主机与虚机都要打开）**

在虚机存储控制器选择：

✔ “启用磁盘写缓存（Enable write caching）”

在 Windows 宿主机磁盘属性：

✔ “启用设备上的写缓存”  
✔ “关闭 Windows 写缓存刷新策略”（慎用，有掉电风险）

📈 可提升 20–100% IOPS

---

## **④ 使用 SCSI 控制器（不要用 IDE）**

你 fio 跑到 17k 说明可能已经是 SCSI。

如果你现在是 IDE → 立即换 SCSI：

✔ IDE = 单队列  
✔ SCSI (VHBA) = 多队列并支持更大队列深度

---

## **⑤ 使用多虚拟磁盘 + 多 fio jobs**

Hyper-V 单 VHDX 队列深度有限。

如果你：

- 增加 4 个 VHDX
- 每个还有自己的队列
- fio 分别测试

IOPS 能翻倍甚至更多。

---

## **⑥ 宿主机 BIOS 打开 NVMe 的 ASPM + 高性能模式**

BIOS 设置：

- NVMe ASPM → off 或 L0s/L1 优化
- PCIe Active State Power Management → off
- CPU C-states → off
- CPU performance → high performance

---

## **⑦ 虚拟机 OS 端优化 fio 参数**

你用：

```
iodepth=64
numjobs=8

```

可以进一步调：

```
--iodepth=128 --numjobs=16 --ioengine=io_uring

```

如果内核支持 (`uname -r` 5.4+ 才有 io\_uring)

**io\_uring IOPS 能比 libaio 高 20～200%**

---

# 🧪 **确认宿主机 NVMe 性能（关键）**

你虚拟机里 17k IOPS 很可能是因为：

- 宿主机本身的磁盘性能一般（如 SATA SSD 只能 50k TPS）

👉 请在 Windows 宿主机测一下：

```
diskspd -c3G -d30 -r -w0 -b4K -o32 -t4 C:\test.dat

```

告诉我 host 的 IOPS，我能更准确判断瓶颈。

---

# 📌 **你的结果说明什么？**

- 高延迟（&gt;30ms）
- util = 100% 表示 I/O 阻塞
- Hyper-V 队列被打满
- 在你的虚拟化配置下，17k 已经是正常水平

要真正突破到 100k+ IOPS，需要：

✔ 固定 VHDX / 直通磁盘  
✔ Write-Back cache  
✔ io\_uring  
✔ 多磁盘并行

---

# 📥 如果你愿意，我可以：

- 帮你写一个优化 Hyper-V 虚拟机磁盘性能的 **完整调优脚本（含 PowerShell+Linux）**
- 帮你设计 fio 的 **最大 IOPS 测试方案**
- 帮你分析宿主机硬盘瓶颈

只需要你补充：

1. 宿主机硬盘型号（例如 NVMe/SATA？）
2. VHDX 类型（Fixed / Dynamic？）
3. 存储方式（本地磁盘 / SAN / iSCSI？）

我可以立即给你个精准的优化建议。

# 使用 logrotate 自动切割Openvpn日志

下面给你一套 **在生产环境可直接使用、不会中断 OpenVPN 服务、无日志丢失风险** 的安全日志分割方案，适用于 Ubuntu/Debian/CentOS 等绝大多数系统。

---

# ✅ **一、最推荐方式：使用 logrotate 自动切割（安全、不重启、不丢日志）**

OpenVPN 默认使用 `syslog` 或直接写文件，都可以用 logrotate 来切割。

## **1. 创建 OpenVPN 日志轮转配置**

创建文件：

```bash
sudo nano /etc/logrotate.d/openvpn

```

写入以下内容（可直接复制）：

```bash
/var/log/openvpn/openvpn.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 640 root adm
    postrotate
        # 让 OpenVPN 重新打开日志文件 (不会重启 VPN 服务)
        if pidof openvpn >/dev/null; then
            kill -USR1 $(pidof openvpn)
        fi
    endscript
}

```

---

## 🔍 **配置说明**

<table id="bkmrk-%E9%85%8D%E7%BD%AE%E9%A1%B9-%E5%90%AB%E4%B9%89-daily-%E6%AF%8F%E5%A4%A9%E5%88%86%E5%89%B2%E4%B8%80%E6%AC%A1-"><thead><tr><th>配置项</th><th>含义</th></tr></thead><tbody><tr><td>`daily`</td><td>每天分割一次</td></tr><tr><td>`rotate 7`</td><td>保留 7 天日志</td></tr><tr><td>`compress`</td><td>Gzip 压缩旧日志</td></tr><tr><td>`delaycompress`</td><td>延迟一天再压缩，避免影响写入</td></tr><tr><td>`kill -USR1 $(pidof openvpn)`</td><td>**让 OpenVPN 重新打开日志文件（不会重启）**</td></tr></tbody></table>

> `USR1` 是 OpenVPN 官方推荐的 **安全 reopen-log** 信号，不会影响连接。

---

## **2. 手动立即触发一次切割**

这样可以测试是否生效：

```bash
sudo logrotate -f /etc/logrotate.d/openvpn

```

切割后你会看到：

```
/var/log/openvpn/openvpn.log
/var/log/openvpn/openvpn.log.1.gz

```

---

# ✅ **二、确认 OpenVPN 的 log 文件是否允许切割**

检查 OpenVPN 配置：

```bash
grep -E "log|status" /etc/openvpn/*.conf

```

你应看到：

```
log /var/log/openvpn/openvpn.log

```

如果配置里启用了 `log-append`，也没问题：

```
log-append /var/log/openvpn/openvpn.log

```

logrotate 依旧能安全切割。

---

# ⚠️ **三、不要使用 rm /truncate 强制清空（会导致服务日志失效）**

以下危险操作千万不要做：

```bash
echo "" > /var/log/openvpn/openvpn.log
rm openvpn.log

```

因为：

- OpenVPN 仍会写入旧的文件描述符
- 会导致日志不再更新
- 有时会令 debug 无法进行

除非你同时发 `USR1` 信号让其 reopen。

---

# 🔧 **四、如果你想立即安全清空日志**

**先切割，再通知 OpenVPN 重新打开文件：**

```bash
sudo mv /var/log/openvpn/openvpn.log /var/log/openvpn/openvpn.log.bak
sudo kill -USR1 $(pidof openvpn)

```

此后 `openvpn.log` 会重新生成（0 字节），100% 安全。

---

# 🧾 **五、如果日志增长异常快，该检查什么？**

常见原因：

<table id="bkmrk-%E5%8E%9F%E5%9B%A0-%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95-%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%A2%91%E7%B9%81%E9%87%8D%E8%BF%9E-%E6%A3%80%E6%9F%A5%E7%BD%91%E7%BB%9C"><thead><tr><th>原因</th><th>解决办法</th></tr></thead><tbody><tr><td>客户端频繁重连</td><td>检查网络、证书有效期</td></tr><tr><td>配置开启了 `verb 6~9` 超详细日志</td><td>调低：`verb 3`</td></tr><tr><td>DoS 攻击/暴力扫描</td><td>设置防火墙、fail2ban</td></tr><tr><td>报错：TLS Error / Authenticate failed</td><td>检查证书、时间同步</td></tr></tbody></table>

建议查看最新 20 行日志：

```bash
tail -f /var/log/openvpn/openvpn.log

```

---

# 🎯 如需，我可以帮你：

✔ 优化你的 OpenVPN 配置  
✔ 根据现网日志判断是否被攻击  
✔ 配置 fail2ban 保护 OpenVPN  
✔ 优化日志级别（verb）减少磁盘写入

如果你愿意，把你当前的 `openvpn.conf` 或 `server.conf` 发我，我可以帮你进一步做最优配置。

你现在遇到的报错原因非常明确：

# ❌ **OpenVPN 2.6 已经移除了 log-rotate 选项**

你的错误日志：

```
Options error: Unrecognized option ... log-rotate (2.6.14)

```

这就是 OpenVPN 2.6 版本（Ubuntu 24.04 默认）不再支持 `log-rotate` 的结果。

---

# 🎯 **根因：你加了 log-rotate 0，但 OpenVPN 2.6 不支持该指令**

因此：

✔ openvpn 启动失败  
✔ 客户端无法连接  
✔ 日志不断被 truncate（因为进程一直在崩溃重启）

---

# ✅ **正确方式（OpenVPN 2.6）**

OpenVPN **2.6 已经废弃所有日志轮转选项**，包括：

- `log-rotate`
- `mute-replay-warnings`
- 部分 `suppress-timestamps` 行为变化

OpenVPN 官方建议：

# ✔ **日志轮转只能靠 logrotate，不再由 OpenVPN 自己处理**

---

# ⭐ 正确配置（最终版）

请把 `/etc/openvpn/server.conf` 中删除该行：

```
log-rotate 0

```

并保留：

```
log-append /var/log/openvpn/openvpn.log

```

这是 OpenVPN 2.6 唯一推荐的日志配置。

---

# ⭐ 正确的 logrotate（最终版）

`/etc/logrotate.d/openvpn` 保持：

```
/var/log/openvpn/openvpn.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 640 root adm
    sharedscripts
    postrotate
        if pidof openvpn >/dev/null; then
            kill -HUP $(pidof openvpn)
        fi
    endscript
}

```

---

# ⭐ 重启 OpenVPN（你使用的是 SysV init，不是 systemd）

执行：

```
/etc/init.d/openvpn restart
实际使用
systemctl restart openvpn
```

确认是否成功：

```
ps aux | grep openvpn
tail -f /var/log/openvpn/openvpn.log

```

你应该看到：

```
Initialization Sequence Completed

```

表示服务已恢复。

---

# ⭐ 现在检查客户端连接

一旦服务启动正常，OpenVPN 客户端应该可以重新连接。

---

# 🔍 如果你愿意，我可以帮你进一步优化

你可发我：

```
cat /etc/openvpn/server.conf

```

我可以帮你清理所有不兼容 2.6 的旧参数（常见问题）。

# XRDP（RDP）远程里 Rime 无法输出汉字问题处理

你这个问题其实很典型：**本地桌面正常，XRDP（RDP）远程里 Rime 报错并失效**，日志已经给了非常关键的线索，我给你讲清楚根因和可行修复方案。

---

## 一、日志含义（问题根源）

你日志里的核心报错是：

```
missing input schema: luna_pinyin
missing input schema: luna_pinyin_simp
missing input schema: luna_pinyin_fluency
...

```

这不是 Rime 程序崩了，而是：

> **RDP 会话中的 Rime 找不到用户目录下的输入方案(schema)文件**

在 Linux 下，Rime 的方案文件需要存在于：

```
~/.config/ibus/rime/

```

或

```
~/.local/share/rime/

```

### 为什么**本地正常，RDP异常？**

SRDP / xRDP / RDP 的特性是：

- RDP 会启动一个 **独立的会话环境**
- 有时会使用：
    
    
    - 不完整的 `$HOME`
    - 错误的 `$XDG_CONFIG_HOME`
    - 或直接指向 `/tmp` 的临时目录

于是：

✅ 本地桌面：读取的是  
`/home/youruser/.config/ibus/rime`

❌ RDP 会话：读取的是类似  
`/tmp` 或 `/root` 下的空目录 → 方案缺失

这也是为什么你的日志在：

```
/tmp/rime.ibus.ERROR

```

---

## 二、如何验证（你可以自己快速确认）

进入 RDP 会话的终端，执行：

```bash
echo $HOME
echo $XDG_CONFIG_HOME
env | grep RIME

```

再对比本地会话。

然后检查方案是否存在：

```bash
ls ~/.config/ibus/rime/
ls ~/.local/share/rime/

```

如果 RDP 会话下目录是空的，就坐实了问题。

---

## 三、修复方案（推荐最稳的一种）

### ✅ 方案1：强制复制 Rime 配置到 RDP 能识别的位置（最直接）

在 **本地正常的终端**执行：

```bash
mkdir -p ~/.local/share/rime
cp -r ~/.config/ibus/rime/* ~/.local/share/rime/
实测是将默认账号下的rime配置拷贝到XRDP远程桌面登录账号下：
cp -r /home/shuncom/.config/ibus/rime/* /root/.config/ibus/rime/
```

然后在 **RDP 会话中重新部署 Rime**：

```bash
ibus restart
rime_deployer --build

```

---

### ✅ 方案2：给 Rime 指定固定的用户目录（最彻底）

编辑或新建：

```bash
nano ~/.profile

```

追加：

```
export RIME_USER_DATA_DIR=$HOME/.config/ibus/rime

```

保存后，在 RDP 会话里执行：

```bash
source ~/.profile
ibus restart

```

这样 RDP 和本地都会强制用同一个 Rime 目录。

---

### ✅ 方案3（如果你用的是 xRDP）

在：

```
~/.xsession

```

里加入：

```bash
export XIM_PROGRAM=ibus
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export RIME_USER_DATA_DIR=$HOME/.config/ibus/rime
ibus-daemon -drx

```

---

## 四、为什么显示 missing schema

Rime 默认会加载这些方案：

```
luna_pinyin
luna_pinyin_simp
terra_pinyin
bopomofo

```

它们存在于：

```
/usr/share/rime-data/

```

RDP 会话因为路径异常，**根本没扫到这个目录**，所以全报 missing。

---

## 五、最省事的最终推荐你执行一次（一步到位）

你可以直接运行：

```bash
mkdir -p ~/.local/share/rime
cp -r /usr/share/rime-data/* ~/.local/share/rime/
cp -r ~/.config/ibus/rime/* ~/.local/share/rime/

echo 'export RIME_USER_DATA_DIR=$HOME/.local/share/rime' >> ~/.profile

ibus restart

```

然后 **完全退出 RDP 再重新连接**。

---

## 六、简单总结一句

你的问题不是 Rime坏了，而是：

> SRDP / RDP 会话没有正确指向 Rime 的用户目录和系统 schema 目录，导致所有输入方案无法加载。

---

如果你告诉我你用的是：

- `xRDP`
- 还是 `GNOME 远程桌面`
- 还是 `freerdp`

我可以给你一套**最匹配的专用修复配置**。

# Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？

<div class="entry-head" id="bkmrk-" style="box-sizing: border-box; margin-bottom: 20px; color: rgb(26, 26, 26); font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Microsoft YaHei', 'Source Han Sans SC', 'Noto Sans CJK SC', 'WenQuanYi Micro Hei', sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"></div>Kali Linux是一款专为渗透测试和安全研究设计的操作系统，其中VNC（Virtual Network Computing）功能可以帮助用户远程控制计算机，本文将详细介绍Kali Linux下VNC的配置方法。

[![Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091813176455189357463.jpg "Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？")](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091813176455189357463.jpg)

## <span class="ez-toc-section" id="bkmrk--2" style="box-sizing: border-box;"></span>安装VNC服务器

在Kali Linux中,我们可以使用以下命令安装VNC服务器：

```
sudo apt-get update
sudo apt-get install tightvncserver
```

## <span class="ez-toc-section" id="bkmrk--3" style="box-sizing: border-box;"></span>配置VNC服务器

安装完成后，需要配置VNC服务器,运行以下命令启动VNC服务器：

```
vncserver
```

系统会提示输入密码，该密码将用于登录VNC服务器，系统会生成一个随机会话名称，例如`1`。

## <span class="ez-toc-section" id="bkmrk--4" style="box-sizing: border-box;"></span>修改VNC配置文件

为了更好地配置VNC服务器，我们需要修改`/etc/vnc/x0vnc.conf`文件,使用以下命令打开该文件：

```
sudo nano /etc/vnc/x0vnc.conf
```

在文件中,找到以下行：

```
# vncpassword = "vncserverpassword"
```

将其修改为：

[![Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091813176455189365609.jpg "Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？")](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091813176455189365609.jpg)

```
vncpassword = "your_password"
```

保存并关闭文件。

## <span class="ez-toc-section" id="bkmrk--6" style="box-sizing: border-box;"></span>启动VNC服务

我们需要启动VNC服务，以便远程连接,使用以下命令启动VNC服务：

```
sudo systemctl start vncserver@:1.service
```

## <span class="ez-toc-section" id="bkmrk--7" style="box-sizing: border-box;"></span>设置VNC服务开机自启

为了使VNC服务在系统启动时自动运行，我们需要将其设置为开机自启,使用以下命令：

```
sudo systemctl enable vncserver@:1.service
```

## <span class="ez-toc-section" id="bkmrk--8" style="box-sizing: border-box;"></span>连接VNC客户端

我们可以使用VNC客户端连接到VNC服务器，在Windows系统中，可以使用TightVNC客户端；在macOS系统中，可以使用RealVNC客户端,以下是连接步骤：

<div class="entry-content text-justify" id="bkmrk-%E6%89%93%E5%BC%80vnc%E5%AE%A2%E6%88%B7%E7%AB%AF%E3%80%82-%E8%BE%93%E5%85%A5vnc%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84i" style="box-sizing: border-box; text-align: justify; counter-reset: footnotes 0; color: rgb(26, 26, 26); font-size: 16px; line-height: 1.8; margin-bottom: 30px; position: relative; font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Microsoft YaHei', 'Source Han Sans SC', 'Noto Sans CJK SC', 'WenQuanYi Micro Hei', sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1. 打开VNC客户端。
2. 输入VNC服务器的IP地址和端口号（默认为`localhost:1`）。
3. 输入VNC密码，然后点击“连接”。

</div>## <span class="ez-toc-section" id="bkmrk--9" style="box-sizing: border-box;"></span>FAQs

**Q1：如何查看VNC服务器的IP地址？**

A1：在Kali Linux中,可以使用以下命令查看IP地址：

[![Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091814176455189487588.jpg "Kali Linux VNC配置过程中遇到了哪些常见问题及解决方法？")](https://kos-kfy.uploads.cn/wp-content/uploads/2025/12/20251201091814176455189487588.jpg)

```
ifconfig
```

或者

```
ip addr show
```

**Q2：如何更改VNC服务器的端口号？**

A2：要更改VNC服务器的端口号，需要修改`/etc/vnc/x0vnc.conf`文件中的以下行：

```
# vncport = 5901
```

将其修改为所需的端口号，

```
vncport = 5902
```

然后重新启动VNC服务即可。

在 KVM 虚拟机中，Kali Linux 2025.4 桌面分辨率低，通常是因为\*\*没有安装或正确配置虚拟机增强工具（virtio驱动和 SPICE Guest Tools）\*\*。这导致系统无法动态适应虚拟机窗口大小，只能使用基本的默认分辨率。

\### 🔧 解决方案：安装并配置 SPICE 增强工具

这是解决问题的根本方法，它将提供高性能的驱动和动态分辨率调整功能。

\*\*操作步骤：\*\*

1\. \*\*在 Kali 虚拟机内操作\*\*，确保软件源是最新的：  
 ```bash  
 sudo apt update  
 sudo apt upgrade -y  
 ```

2\. \*\*安装必需的软件包\*\*，包括 SPICE 客户端所需的驱动和 X11 工具：  
 ```bash  
 sudo apt install -y spice-vdagent spice-webdavd xserver-xorg-video-qxl  
 ```  
 \* `spice-vdagent`：核心组件，负责\*\*动态分辨率调整\*\*、剪贴板共享和鼠标集成。  
 \* `spice-webdavd`：用于文件夹共享（可选，但建议安装）。  
 \* `xserver-xorg-video-qxl`：优化的显示驱动，提供更好的图形性能。

3\. \*\*启用并启动 `spice-vdagent` 服务\*\*，使其开机自动运行：  
 ```bash  
 sudo systemctl enable --now spice-vdagent  
 ```

4\. \*\*立即重启 Kali 虚拟机\*\*：  
 ```bash  
 sudo reboot  
 ```

\### 💡 验证与备用方案

重启后，尝试拖动虚拟机窗口边框改变大小，分辨率应该能自动适应了。

如果上述方案未能完全解决问题，你可以\*\*手动设置一个更高分辨率\*\*作为备用方案：  
1\. 在 Kali 桌面环境中，打开终端。  
2\. 使用 `xrandr` 命令查看可用的显示模式和当前设置：  
 ```bash  
 xrandr  
 ```  
3\. 从列出的模式中（如 `1920x1080`），选择一个并设置：  
 ```bash  
 xrandr --output Virtual-1 --mode 1920x1080  
 ```  
 （请将 `Virtual-1` 替换为 `xrandr` 命令输出中你的实际接口名，通常是 `Virtual-1` 或 `VGA-1`。）

\### ⚠️ 重要前提：宿主机配置检查

确保你的 \*\*KVM 虚拟管理器（如 virt-manager）\*\* 配置正确：  
1\. 虚拟机的\*\*显示协议\*\*应设置为 \*\*`spice`\*\*（而不是古老的 `VNC`）。  
2\. 虚拟机的\*\*显卡模型\*\*应设置为 \*\*`virtio`\*\* 或 \*\*`qxl`\*\*。  
3\. 在虚拟机的 `XML` 配置中，应有 `&lt;channel&gt;` 设备用于 `spice-vdagent` 通信。

\### 📝 问题排查流程总结

如果安装增强工具后问题依旧，可以按以下流程排查：

```mermaid  
flowchart TD  
 A\[KVM Kali虚拟机分辨率低\] --&gt; B{是否已安装&lt;br&gt;SPICE Guest Tools?};  
 B -- 否 --&gt; C\[安装 spice-vdagent&lt;br&gt;等软件包并重启\];  
 C --&gt; D\[问题解决?\];  
 B -- 是 --&gt; E\[检查KVM虚拟机配置\];  
 E --&gt; F\[显示协议是否为 SPICE?\];  
 F -- 否 --&gt; G\[改为 SPICE 协议\];  
 F -- 是 --&gt; H\[显卡是否为 virtio/qxl?\];  
 H -- 否 --&gt; I\[改为 virtio 或 qxl\];  
 H -- 是 --&gt; J\[尝试手动设置&lt;br&gt;xrandr 分辨率\];  
 D -- 是 --&gt; K\[✅ 完成\];  
 D -- 否 --&gt; E;  
 G --&gt; D;  
 I --&gt; D;  
 J --&gt; D;  
```

&gt; \*\*提示\*\*：手动 `xrandr` 设置是临时的，重启后可能失效。安装 `spice-vdagent` 是实现自动调整和最佳体验的关键。

请按照上述步骤操作，特别是安装增强工具的部分。如果操作后分辨率问题仍未解决，或者你在 `xrandr` 输出中看不到高分辨率选项，可以告诉我具体情况，我们再进一步分析。

# Kali Linux 最新版安装中文拼音输入法

在 **Kali Linux 最新版（当前基于 Debian 12 / Bookworm）** 上，**最稳定、官方推荐的中文拼音输入法方案是：IBus + Rime**。  
该方案兼容性好、维护活跃，适合桌面与远程场景。

下面给你 **标准、可复用的完整安装流程**。

---

## 一、方案选择说明（先给结论）

**推荐方案**：`ibus + ibus-rime`  
不推荐旧版 `fcitx / fcitx4`（已淘汰）

---

## 二、安装 IBus + Rime（一步一步来）

### 1️⃣ 更新系统索引

```bash
sudo apt update

```

---

### 2️⃣ 安装中文字体（非常重要，否则候选框乱码）

```bash
sudo apt install -y fonts-noto-cjk

```

---

### 3️⃣ 安装 IBus 框架 + Rime 拼音

```bash
sudo apt install -y ibus ibus-rime

```

---

### 4️⃣ 设置 IBus 为默认输入法框架

```bash
im-config -n ibus

```

执行后 **必须重新登录桌面**（注销 / 重启都可以）

---

## 三、启用中文拼音（GUI 操作）

重新登录后：

1. 打开 **Settings / Settings Manager**
2. 进入 **Keyboard → Input Sources**
3. 点击 **+**
4. 选择：
    
    
    - `Chinese`
    - `Chinese (Rime)`

确认后关闭设置。

---

## 四、切换与使用方式

- **切换输入法**：  
    `Ctrl + Space`
- **中 / 英切换**：  
    `Shift`
- **拼音模式**：  
    全拼 / 双拼（可在 Rime 配置）

---

## 五、验证是否生效

打开终端或浏览器，输入：

```text
nihao

```

应出现中文候选词。

---

## 六、常见问题与解决（很关键）

### 1️⃣ 没有候选框 / 无法切换

执行：

```bash
ibus restart

```

或手动启动：

```bash
ibus-daemon -drx

```

---

### 2️⃣ SSH / XRDP / RDP 下无法输入中文

这是 **输入法环境变量未加载** 的典型问题：

```bash
echo -e 'export GTK_IM_MODULE=ibus\nexport QT_IM_MODULE=ibus\nexport XMODIFIERS=@im=ibus' >> ~/.xprofile

```

注销后重新登录。

---

### 3️⃣ 候选框乱码 / 方块字

说明字体未装好：

```bash
sudo apt install -y fonts-noto-cjk
fc-cache -fv

```

---

## 七、（可选）Rime 高级配置路径

Rime 用户配置目录：

```bash
~/.config/ibus/rime/

```

常见文件：

- `default.custom.yaml`
- `luna_pinyin.custom.yaml`

可用于：

- 关闭简繁切换
- 调整候选数
- 启用双拼

---

## 八、如果你有以下特殊场景，我可以进一步定制

你是资深运维，常见的进一步需求包括：

- Kali + **XRDP / xRDP + Windows**
- Kali + **SRDP / RDP / NoMachine**
- Kali 作为 **渗透测试工作站**（终端优先）
- 纯命令行环境（TTY）输入中文

你只需要告诉我一句：  
👉 **是本地桌面用，还是远程桌面用？**

我可以直接给你最优配置方案。

# 在 Ubuntu 14.04 环境下，将 整块分区 /dev/vdc1 转换为 VHD 文件

在 **Ubuntu 14.04** 环境下，将 **整块分区 `/dev/vdc1`** 转换为 **VHD 文件** 并保存到  
`/home/shuncom/`，**推荐使用 `qemu-img`**，而不是直接 `dd`，因为：

- VHD 支持稀疏文件（体积更小）
- 兼容 **Hyper-V**
- 可控、可校验、可恢复

下面给你一套 **生产可用、一步到位** 的做法。

---

## 一、前置强烈建议（重要）

### 1️⃣ 确保分区未被写入

```bash
mount | grep vdc1

```

- **理想状态**：未挂载
- 如果已挂载，务必只读卸载：

```bash
umount /dev/vdc1

```

---

### 2️⃣ 确认分区信息

```bash
lsblk -f /dev/vdc

```

---

## 二、安装必需工具（Ubuntu 14.04 可用）

```bash
apt-get update
apt-get install -y qemu-utils

```

确认：

```bash
qemu-img --version

```

---

## 三、推荐方案（最优）：直接转为 VHD

### 命令（标准、安全）

```bash
qemu-img convert \
  -p \
  -f raw \
  -O vpc \
  /dev/vdc1 \
  /home/shuncom/vdc1.vhd

```

说明：

<table id="bkmrk-%E5%8F%82%E6%95%B0-%E5%90%AB%E4%B9%89--p-%E6%98%BE%E7%A4%BA%E8%BF%9B%E5%BA%A6--f-raw"><thead><tr><th>参数</th><th>含义</th></tr></thead><tbody><tr><td>`-p`</td><td>显示进度</td></tr><tr><td>`-f raw`</td><td>输入是裸设备</td></tr><tr><td>`-O vpc`</td><td>输出为 VHD（Hyper-V）</td></tr><tr><td>`/dev/vdc1`</td><td>整个分区</td></tr><tr><td>`vdc1.vhd`</td><td>目标文件</td></tr></tbody></table>

---

## 四、如果你想“更稳妥”（推荐生产）

### 方案：raw → vhd（两步）

#### 1️⃣ 先做 raw 镜像

```bash
dd if=/dev/vdc1 \
   of=/home/shuncom/vdc1.raw \
   bs=64M \
   status=progress

```

#### 2️⃣ 转换为 VHD

```bash
qemu-img convert -p -f raw -O vpc \
  /home/shuncom/vdc1.raw \
  /home/shuncom/vdc1.vhd

```

完成后可删除 raw：

```bash
rm -f /home/shuncom/vdc1.raw

```

---

## 五、校验与确认（必须做）

### 1️⃣ 查看 VHD 信息

```bash
qemu-img info /home/shuncom/vdc1.vhd

```

应看到：

```text
file format: vpc
virtual size: XXXG

```

---

### 2️⃣ （可选）做 hash 校验

```bash
sha256sum /home/shuncom/vdc1.vhd > /home/shuncom/vdc1.vhd.sha256

```

---

## 六、常见问题与坑

### ❗ XFS / ext4 都支持

- ext4：完全没问题
- XFS：**必须卸载或只读**

---

### ❗ 空间不足

VHD 虽然是稀疏文件，但仍需：

- 至少 **10–20% 剩余空间**
- 用 `df -h` 提前确认

---

### ❗ 分区 vs 整盘区别

你现在导出的是：

```text
/dev/vdc1   ← 分区（常用，正确）

```

如果你想要 **MBR/GPT + 全盘**，应使用：

```bash
/dev/vdc

```

---

## 七、导入 Hyper-V 的注意事项（提前提醒）

- 建议用 **Generation 1**（尤其是老系统 / BIOS）
- Linux 内核 3.x（14.04）兼容性更好
- 启动失败时检查：
    
    
    - grub
    - fstab 中 UUID

---

## 八、我可以继续帮你

如果你后续是要：

- 导入 Hyper-V 无法启动
- 要合并系统盘 + 数据盘
- 转 VHDX / QCOW2 / VMDK
- 写自动化脚本

直接告诉我你的 **最终目标平台**，我可以直接给你下一步命令级方案。

# 使用 ffmpeg 判断一个文件是否为 MP3 格式

在 Linux（如 Ubuntu）中，使用 `ffmpeg` 判断一个文件是否为 **MP3 格式**，可以通过以下几种可靠方式实现。核心思路是：**让 ffmpeg 探测文件的容器格式和音频编码，并检查是否匹配 MP3**。

---

### ✅ 推荐方法：使用 `ffprobe`（FFmpeg 自带的探测工具）

> `ffprobe` 是专门用于分析媒体文件元数据的工具，比直接用 `ffmpeg` 更轻量、更准确。

#### 🔹 命令 1：检查容器格式是否为 `mp3`

```bash
ffprobe -v quiet -show_entries format=format_name -of csv=p=0 input.mp3

```

- 如果输出是 `mp3`，说明容器格式是 MP3。
- 示例： ```bash
    $ ffprobe -v quiet -show_entries format=format_name -of csv=p=0 song.mp3
    mp3
    
    ```

#### 🔹 命令 2：检查音频编码是否为 MP3（更本质）

```bash
ffprobe -v quiet -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 input.mp3

```

- 如果输出是 `mp3`，说明音频编码是 MP3（即使文件扩展名不是 .mp3）。
- 注意：有些 MP3 文件可能被封装在其他容器中（罕见），但绝大多数 MP3 文件的 `codec_name` 就是 `mp3`。

---

### ✅ 组合判断（推荐用于脚本）

```bash
#!/bin/bash
file="your_file"

# 获取编码器名称（第一个音频流）
codec=$(ffprobe -v quiet -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 "$file" 2>/dev/null)

if [ "$codec" = "mp3" ]; then
    echo "✅ 是 MP3 格式"
else
    echo "❌ 不是 MP3 格式（实际编码：$codec）"
fi

```

> 💡 为什么优先看 `codec_name` 而不是 `format_name`？  
> 因为 MP3 本质上是一种**音频编码格式**。虽然通常用 `.mp3` 作为文件扩展名（对应 `format_name=mp3`），但技术上 MP3 音频也可能出现在其他容器中（如 AVI、MKV），不过极少见。反过来，`.mp3` 文件几乎总是使用 `mp3` 编码。

---

### ⚠️ 不推荐：仅靠文件扩展名

```bash
# ❌ 不可靠！
[[ "$file" == *.mp3 ]] && echo "可能是 MP3"

```

- 用户可随意改扩展名，无法保证内容真实格式。

---

### 🔍 补充：查看完整信息（调试用）

```bash
ffprobe -v quiet -show_format -show_streams input.mp3

```

输出中关注：

- `format_name=mp3`
- `codec_name=mp3`（在 `[STREAM]` 段落中）

---

### 🛠️ 如果系统没有 `ffprobe`？

虽然你已安装 `ffmpeg`，但某些精简版可能未包含 `ffprobe`。此时可用 `ffmpeg` 代替（效率较低）：

```bash
# 通过解析 ffmpeg 的输入信息判断
if ffmpeg -v quiet -i input.mp3 -f null - 2>&1 | grep -q 'Audio: mp3'; then
    echo "✅ 是 MP3"
else
    echo "❌ 不是 MP3"
fi

```

> 原理：`ffmpeg -i file` 会打印流信息，其中包含 `Audio: mp3, ...` 字样。

---

### ✅ 总结

<table id="bkmrk-%E6%96%B9%E6%B3%95-%E5%91%BD%E4%BB%A4-%E4%BC%98%E7%82%B9-%E9%A6%96%E9%80%89-ffprobe-"><thead><tr><th>方法</th><th>命令</th><th>优点</th></tr></thead><tbody><tr><td>**首选**</td><td>`ffprobe -show_entries stream=codec_name ...`</td><td>准确、快速、专为探测设计</td></tr><tr><td>备用</td><td>`ffmpeg -i file 2&gt;&amp;1</td><td>grep 'Audio: mp3'`</td></tr><tr><td>避免</td><td>仅检查 `.mp3` 扩展名</td><td>不可靠</td></tr></tbody></table>

建议在脚本中使用 `ffprobe` + `codec_name` 方式判断，最权威。

# 安全清理容器的日志文件/var/lib/docker/containers

从你的 `du -h -d1` 输出可以看出：

- 总共 **82GB** 的空间被 `/var/lib/docker/containers` 占用。
- 其中一个容器目录（ID 为 `b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6`）就占用了 **82GB**，几乎全部空间。

这极大概率是该容器的 **日志文件（`b731e62...-json.log`）过大**导致的。

---

## 🔍 第一步：确认是日志文件

进入该目录，查看具体文件：

```bash
ls -lh /var/lib/docker/containers/b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6/

```

你应该会看到类似：

```
-rw-r----- 1 root root 82G ... b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6-json.log

```

---

## ✅ 安全清理方案（根据容器状态选择）

### 🟢 情况一：该容器**仍在运行**，但你不能停它

> 比如是生产服务，需要保持运行。

**使用 `truncate` 清空日志而不中断容器：**

```bash
truncate -s 0 /var/lib/docker/containers/b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6/b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6-json.log

```

✅ 空间立即释放，容器继续正常运行。

---

### 🔴 情况二：该容器**已停止或不再需要**

> 比如是测试容器、僵尸容器。

**彻底删除容器（推荐）：**

1. 先查容器名或确认状态：
    
    ```bash
    docker ps -a | grep b731e62
    
    ```
2. 如果确认无用，删除它：
    
    ```bash
    docker rm b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6
    
    ```
    
    > 如果还在运行，先 `docker stop <id>` 再删。

✅ 删除后，整个目录（包括 82GB 日志）会自动被 Docker 清理。

---

### 🛡️ 长期预防：配置日志轮转

避免未来再次出现 82GB 日志！

编辑 `/etc/docker/daemon.json`（若不存在则创建）：

```json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}

```

然后重启 Docker：

```bash
systemctl restart docker

```

> ⚠️ 注意：此设置**仅对新启动的容器生效**。已有容器需重建（`docker-compose up -d` 或 `docker run` 重新创建）才能应用。

---

## 🔎 补充建议

- 查看这个容器是什么：
    
    ```bash
    docker inspect b731e62e9a85 --format='{{.Name}} {{.Config.Image}}'
    
    ```
    
    看是否是关键服务（如数据库、日志收集器等）。有些应用（如未配置的日志服务、调试容器）会疯狂输出日志。
- 如果是 `fluentd`、`logstash`、`promtail` 或自研程序，检查其日志级别（是否误设为 `DEBUG`）。

---

## ✅ 总结操作（推荐顺序）

1. **先清空日志释放空间**（无论容器是否运行）：
    
    ```bash
    truncate -s 0 /var/lib/docker/containers/b731e62e9a85206148e66cceb202722362f04f444fc9c4aa205866aaf373abe6/*-json.log
    
    ```
2. **决定是否保留该容器**：
    
    
    - 不需要 → `docker rm`
    - 需要 → 未来重建时应用日志限制
3. **配置全局日志轮转**，防止复发。

---

如果你告诉我这个容器是什么（运行什么服务），我可以给出更具体的建议（比如是否安全删除、如何调低日志级别等）。

# 把 Docker 的运行数据目录从系统盘（/）迁移到 /home

你的目的本质上是：**把 Docker 的运行数据目录从系统盘（/）迁移到 /home 所在的大容量磁盘（/dev/sdb1）**，以避免后续容器、日志、overlay2 占满根分区。

需要先明确一个关键点，然后给出**正确、规范、可回滚**的迁移方案。

---

## 一、先澄清一个常见误区（很重要）

你现在关注的是：

```
/var/lib/docker/containers

```

但 **Docker 实际占用空间的核心并不在 containers 目录，而是在：**

- `/var/lib/docker/overlay2` ← **真正吃空间**
- `/var/lib/docker/volumes`
- `/var/lib/docker/image`

你当前看到：

```
containers 目录 < 1MB
overlay2 挂载在 /var/lib/docker/overlay2/*

```

说明：

- 现在只是“刚开始”，但 **只迁移 containers 没有任何实质意义**
- 正确做法是：**整体迁移 /var/lib/docker**

---

## 二、推荐方案（生产最常用）：修改 Docker data-root

这是**最干净、最安全、官方推荐**的方式。

### 1️⃣ 停止 Docker 服务

```bash
systemctl stop docker
systemctl stop containerd

```

确认没有残留进程：

```bash
ps -ef | grep -E 'docker|containerd' | grep -v grep

```

---

### 2️⃣ 在 /home/shuncom 下准备新目录

```bash
mkdir -p /home/shuncom/docker

```

权限建议保持 root：

```bash
chown -R root:root /home/shuncom/docker

```

---

### 3️⃣ 使用 rsync 迁移原有数据（强烈推荐）

> **不要用 cp -a**，overlay2 + hardlink 场景下 rsync 更安全

```bash
rsync -aHAX --numeric-ids /var/lib/docker/ /home/shuncom/docker/

```

参数说明（你是老运维，这里直说重点）：

- `-H`：保持 hard links（overlay2 必须）
- `-A -X`：ACL / xattr
- `--numeric-ids`：避免 UID/GID 映射问题

---

### 4️⃣ 备份原目录（不要直接删）

```bash
mv /var/lib/docker /var/lib/docker.bak

```

---

### 5️⃣ 配置 Docker 使用新目录

编辑（没有就新建）：

```bash
vim /etc/docker/daemon.json

```

内容如下：

```json
{
  "data-root": "/home/shuncom/docker"
}

```

**注意：JSON 严格格式，不能多逗号**

---

### 6️⃣ 启动 Docker

```bash
systemctl daemon-reexec
systemctl start docker

```

检查状态：

```bash
systemctl status docker

```

---

### 7️⃣ 验证是否生效（非常关键）

```bash
docker info | grep -i "Docker Root Dir"

```

应显示：

```
Docker Root Dir: /home/shuncom/docker

```

再确认 overlay2：

```bash
mount | grep overlay | head

```

路径应指向 `/home/shuncom/docker/overlay2`

---

### 8️⃣ 一切正常后，清理旧数据

**建议观察 1–2 天后再删**

```bash
rm -rf /var/lib/docker.bak

```

---

## 三、备选方案（不推荐但可用）：bind mount

如果你**不想改 Docker 配置文件**，也可以用挂载方式：

```bash
mount --bind /home/shuncom/docker /var/lib/docker

```

并写入 `/etc/fstab`：

```fstab
/home/shuncom/docker  /var/lib/docker  none  bind  0  0

```

⚠️ **缺点**：

- 可维护性差
- 后期排障容易踩坑
- 不利于 Docker 升级

**不建议长期使用**

---

## 四、针对你当前环境的补充建议（基于你贴的 df）

- `/` 只有 98G，但 overlay2 已经在用
- `/home` 有 300G，非常适合 Docker data-root
- 后续建议：
    
    ```bash
    docker system df
    docker system prune -a
    
    ```
    
    定期清理无用镜像、层

---

## 五、总结一句话方案

> **不要只迁移 `/var/lib/docker/containers`，而是整体迁移 `/var/lib/docker`，并通过 `data-root` 正式切换到 `/home/shuncom/docker`。**

如果你愿意，下一步我可以帮你：

- 校验 rsync 后的 overlay2 完整性
- 规划 volumes 单独挂载
- 评估是否需要开启 `log-driver` 限制容器日志膨胀

# ubuntu提升stack size（栈空间）

你当前的 **stack size（栈大小）** 是：

```
stack size (kbytes, -s) 8192

```

即 **8192 KB = 8 MB**，这是 Linux 系统的**默认用户栈大小限制**。

---

## ✅ 如何将 `stack size` 配置为最高（或更大）？

### 🔹 方法 1：临时提升（当前会话有效）

```bash
# 设置为无限制（unlimited）
ulimit -s unlimited

# 或设置为具体值（例如 65536 KB = 64 MB）
ulimit -s 65536

```

> ✅ 立即生效，但**仅对当前 shell 及其子进程有效**，退出后失效。

---

### 🔹 方法 2：永久生效（系统级配置）

#### 步骤 1：编辑 `/etc/security/limits.conf`

```bash
sudo nano /etc/security/limits.conf

```

添加以下行（替换 `username` 为实际用户，如 `shuncom` 或用 `*` 表示所有用户）：

```conf
# 用户级设置（推荐指定用户）
shuncom    soft    stack    unlimited
shuncom    hard    stack    unlimited

# 或全局设置（谨慎！）
*          soft    stack    unlimited
*          hard    stack    unlimited

```

> 📌 `soft` = 当前生效值，`hard` = 允许用户自行提升的最大值  
> `unlimited` 表示无限制（实际受虚拟内存总量约束）

#### 步骤 2：确保 PAM 启用 limits 模块

检查 `/etc/pam.d/common-session` 是否包含：

```conf
session required pam_limits.so

```

Ubuntu 默认已启用，通常无需修改。

#### 步骤 3：**重新登录用户**

- 修改 `limits.conf` 后必须**完全退出并重新登录**（或重启）才能生效。
- 不能只开新终端！

---

### 🔹 方法 3：针对 systemd 服务（如 PHP-FPM、Nginx）

如果你的应用是通过 `systemd` 启动的（如 `php8.3-fpm.service`），需单独配置：

```bash
sudo systemctl edit php8.3-fpm

```

添加：

```ini
[Service]
LimitSTACK=infinity

```

然后重载并重启：

```bash
sudo systemctl daemon-reload
sudo systemctl restart php8.3-fpm

```

> ✅ 这样可确保服务进程获得更大的栈空间。

---

## ⚠️ 重要注意事项

### 1. **`unlimited` 并非真正无限**

- 实际受限于： 
    - 系统可用虚拟内存（`ulimit -v`）
    - 内核参数 `vm.max_map_count`
    - 物理内存 + swap
- 在 64 位系统上，通常可安全设为 `unlimited`

### 2. **盲目增大栈可能掩盖程序 bug**

- 栈溢出（stack overflow）通常是**递归过深**或**局部变量过大**（如 `char buf[10*1024*1024]`）导致
- **正确做法**： 
    - 将大数组改为堆分配（`malloc` / `new`）
    - 优化递归为迭代
    - 使用工具（如 Valgrind）检测内存问题

### 3. **某些程序硬编码栈大小**

- 如 Java、Python 等虚拟机语言有自己的栈管理，不受 `ulimit -s` 影响
- 需在其内部配置（如 JVM 的 `-Xss`）

---

## 🔍 验证是否生效

```bash
# 重新登录后执行
ulimit -s

```

应输出：

```
unlimited

```

或你设置的具体值（如 `65536`）。

---

## 🛠️ 推荐配置（平衡安全与需求）

<table id="bkmrk-%E5%9C%BA%E6%99%AF-%E5%BB%BA%E8%AE%AE%E5%80%BC-%E6%99%AE%E9%80%9A%E5%BA%94%E7%94%A8-%E4%BF%9D%E6%8C%81%E9%BB%98%E8%AE%A4-819"><thead><tr><th>场景</th><th>建议值</th></tr></thead><tbody><tr><td>普通应用</td><td>保持默认 `8192`（8MB）</td></tr><tr><td>深度递归/大型局部变量</td><td>`65536`（64MB）</td></tr><tr><td>科学计算/HPC</td><td>`unlimited`（需监控内存）</td></tr></tbody></table>

> 💡 **除非明确需要，否则不要设为 `unlimited`**。优先修复代码中的栈使用问题。

---

## ✅ 总结

<table id="bkmrk-%E7%9B%AE%E6%A0%87-%E5%91%BD%E4%BB%A4%2F%E9%85%8D%E7%BD%AE-%E4%B8%B4%E6%97%B6%E6%8F%90%E5%8D%87-ulimit"><thead><tr><th>目标</th><th>命令/配置</th></tr></thead><tbody><tr><td>临时提升</td><td>`ulimit -s unlimited`</td></tr><tr><td>永久用户级</td><td>`/etc/security/limits.conf` + 重新登录</td></tr><tr><td>systemd 服务</td><td>`systemctl edit <service>` + `LimitSTACK=infinity`</td></tr></tbody></table>

执行后，你的程序将能使用更大的栈空间，避免因栈溢出导致的崩溃（如 `Segmentation fault` 或 `stack smashing detected`）。

# 在 Ubuntu 24.04 系统上配置 Samba（SMB）共享

在 **Ubuntu 24.04** 系统上配置 Samba（SMB）共享，将本地目录 `/home/shuncom/teleport_backup` 共享为 `\\192.168.30.54\teleport_backup`，请按以下步骤操作：

---

## ✅ 第一步：安装 Samba

```bash
sudo apt update
sudo apt install samba -y

```

---

## ✅ 第二步：创建或确认共享目录，并设置权限

```bash
# 确保目录存在
sudo mkdir -p /home/shuncom/teleport_backup

# 设置属主（建议使用 shuncom 用户）
sudo chown -R shuncom:shuncom /home/shuncom/teleport_backup

# 设置适当权限（根据需求调整）
sudo chmod -R 755 /home/shuncom/teleport_backup
# 或更宽松（如需写入）：
# sudo chmod -R 775 /home/shuncom/teleport_backup

```

> ⚠️ 注意：Samba 默认不共享用户家目录下的子目录（因安全限制），但通过正确配置可实现。

---

## ✅ 第三步：为 Samba 添加用户（必须与系统用户对应）

```bash
# 假设你要用 "shuncom" 用户访问共享
sudo smbpasswd -a shuncom

```

> 🔐 系统会提示你设置一个 **Samba 密码**（可以和登录密码不同）。  
> 此用户必须是系统已存在的用户（`id shuncom` 应有输出）。

启用该用户（默认已启用）：

```bash
sudo smbpasswd -e shuncom

```

---

## ✅ 第四步：配置 Samba 共享（编辑 smb.conf）

备份原配置：

```bash
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

```

编辑配置文件：

```bash
sudo nano /etc/samba/smb.conf

```

在文件末尾添加以下内容：

```ini
[teleport_backup]
   comment = Teleport Backup Share
   path = /home/shuncom/teleport_backup
   browseable = yes
   writable = yes
   guest ok = no
   valid users = shuncom
   create mask = 0644
   directory mask = 0755
   force user = shuncom

```

### 参数说明：

<table id="bkmrk-%E9%85%8D%E7%BD%AE%E9%A1%B9-%E4%BD%9C%E7%94%A8-browseable-%3D-"><thead><tr><th>配置项</th><th>作用</th></tr></thead><tbody><tr><td>`browseable = yes`</td><td>网络邻居中可见</td></tr><tr><td>`writable = yes`</td><td>允许写入</td></tr><tr><td>`guest ok = no`</td><td>禁用匿名访问</td></tr><tr><td>`valid users = shuncom`</td><td>仅允许 shuncom 用户访问</td></tr><tr><td>`force user = shuncom`</td><td>所有操作以 shuncom 身份执行，避免权限混乱</td></tr></tbody></table>

> 💡 如果你希望多个用户访问，可将 `valid users = user1,user2`，并为每个用户执行 `smbpasswd -a`。

---

## ✅ 第五步：重启 Samba 服务

```bash
sudo systemctl restart smbd nmbd
sudo systemctl enable smbd nmbd  # 开机自启

```

---

## ✅ 第六步：配置防火墙（如果启用 UFW）

Ubuntu 24.04 默认可能启用 `ufw`，需放行 SMB 端口：

```bash
sudo ufw allow samba
# 或手动开放端口：
# sudo ufw allow 139/tcp
# sudo ufw allow 445/tcp

```

查看状态：

```bash
sudo ufw status

```

---

## ✅ 第七步：从 Windows 访问测试

在 Windows 文件资源管理器地址栏输入：

```
\\192.168.30.54\teleport_backup

```

- 弹出登录框时，输入： 
    - **用户名**：`shuncom`
    - **密码**：你在 `smbpasswd -a shuncom` 时设置的 Samba 密码

> 📌 注意：不要用 Windows 的“记住凭据”，避免后续混淆。

---

## 🔍 故障排查

### 1. 无法访问？检查服务状态：

```bash
sudo systemctl status smbd

```

### 2. 权限被拒绝？

- 确认 `/home/shuncom` 目录对其他用户有 **执行权限（x）**： ```bash
    chmod o+x /home/shuncom
    
    ```
    
    > 否则 Samba 无法进入家目录。

### 3. 测试本地连接：

```bash
smbclient -L localhost -U shuncom

```

### 4. 查看日志：

```bash
sudo tail -f /var/log/samba/log.smbd

```

---

## ✅ 最终效果

- Windows 访问：`\\192.168.30.54\teleport_backup`
- 需输入用户名 `shuncom` + Samba 密码
- 可读写，文件属主为 `shuncom`

---

> 💡 **安全建议**：
> 
> - 不要设置 `guest ok = yes`（除非内网可信）
> - 定期更新系统：`sudo apt upgrade`
> - 如仅限内网使用，可绑定 IP（在 `smb.conf` 的 `[global]` 中加 `interfaces = 192.168.30.54/24`）

完成以上步骤，你的 SMB 共享即可正常使用！

你已确认 **445/139 端口通**，Samba 服务运行正常，共享配置也基本正确，但 **Windows 无法访问 `\\192.168.30.54\teleport_backup`**。

结合你提供的信息（尤其是 `/home/shuncom` 的权限），**根本原因极可能是：**

> ❌ **`/home/shuncom` 目录权限为 `drwxr-x--x`（即 `751`），其他用户（包括 Samba 进程）没有进入（execute）权限！**

虽然 `teleport_backup` 子目录是 `755`，但 **Linux 要求父目录必须有 `x` 权限才能访问子目录**。Samba 默认以 `nobody` 或指定用户身份访问，若无权限进入 `/home/shuncom`，就会被拒绝。

---

## ✅ 解决方案

### 🔧 步骤 1：修复家目录权限（关键！）

```bash
# 给其他用户添加 "进入" 权限（x）
sudo chmod o+x /home/shuncom

```

验证：

```bash
ls -ld /home/shuncom
# 应显示：drwxr-x--x → 变为 drwxr-x--x（最后一位 x 表示 others 有 execute 权限）
# 实际权限应为 751 → 但至少 others 有 x（即权限位最后一位是 1, 3, 5, 7）

```

> 💡 更安全的做法（推荐）：
> 
> ```bash
> # 只允许特定组访问（如 sambashare）
> sudo groupadd sambashare
> sudo usermod -aG sambashare shuncom
> sudo chgrp sambashare /home/shuncom
> sudo chmod 750 /home/shuncom  # owner:rwx, group:rx, others: none
> 
> ```
> 
> 但简单场景下 `chmod o+x` 足够（内网环境）。

---

### 🔧 步骤 2：确认 Samba 用户已正确设置

```bash
# 查看 Samba 用户列表
sudo pdbedit -L

# 确保 shuncom 在其中
# 如果没有，重新添加：
sudo smbpasswd -a shuncom   # 设置密码
sudo smbpasswd -e shuncom   # 启用

```

> ⚠️ 注意：**Samba 密码 ≠ 系统登录密码**，必须单独设置！

---

### 🔧 步骤 3：检查 Samba 配置是否生效

你的 `smb.conf` 末尾配置基本正确，但建议在 `[global]` 段落中显式指定安全模式（Ubuntu 24.04 默认可能较严格）：

编辑 `/etc/samba/smb.conf`，在 `[global]` 部分添加或确认以下行：

```ini
[global]
   workgroup = WORKGROUP
   server string = Samba Server %v
   netbios name = ubuntu24-00
   security = user
   map to guest = never
   dns proxy = no
   log file = /var/log/samba/log.%m
   max log size = 1000
   server min protocol = NT1  # ← 仅当 Windows 是 Win10/11 且无法连接时临时加（不推荐长期用）

```

> 🚫 **不要随意加 `server min protocol = NT1`**，除非你确定是协议问题（Win10 默认禁用 SMB1）。  
> 先尝试不加，优先解决权限问题。

---

### 🔧 步骤 4：重启 Samba 服务

```bash
sudo systemctl restart smbd nmbd

```

---

### 🔧 步骤 5：从 Windows 测试（关键操作）

1. **先清除 Windows 凭据缓存**（非常重要！）：
    
    
    - 按 `Win + R`，输入 `cmd` 打开命令提示符
    - 执行： ```cmd
        net use * /delete /y
        
        ```
    - 或手动删除：  
        **控制面板 &gt; 凭据管理器 &gt; Windows 凭据 &gt; 删除与 192.168.30.54 相关的条目**
2. **使用完整凭据访问**：
    
    
    - 在文件资源管理器输入： ```
        \\192.168.30.54\teleport_backup
        
        ```
    - 弹出登录框时： 
        - **用户名**：`shuncom`（或 `.\shuncom`）
        - **密码**：你在 `smbpasswd -a shuncom` 时设置的 **Samba 密码**
        - 勾选“记住凭据”（可选）

---

### 🔍 附加排查命令（在 Ubuntu 上）

```bash
# 1. 本地测试共享是否可见
smbclient -L //localhost -U shuncom

# 2. 查看实时日志（另开终端）
sudo tail -f /var/log/samba/log.smbd

# 3. 检查 SELinux/AppArmor（Ubuntu 默认无 SELinux，但有 AppArmor）
sudo aa-status | grep samba  # 通常不影响，可忽略

```

---

## ✅ 总结：最可能的原因和修复

<table id="bkmrk-%E9%97%AE%E9%A2%98-%E4%BF%AE%E5%A4%8D-%2Fhome%2Fshuncom-"><thead><tr><th>问题</th><th>修复</th></tr></thead><tbody><tr><td>**`/home/shuncom` 权限缺少 `o+x`**</td><td>`sudo chmod o+x /home/shuncom`</td></tr><tr><td>**未设置 Samba 密码**</td><td>`sudo smbpasswd -a shuncom`</td></tr><tr><td>**Windows 缓存了错误凭据**</td><td>`net use * /delete` 清除</td></tr></tbody></table>

> 💡 **90% 的类似问题都是因为家目录权限不足导致的！**

执行 `chmod o+x /home/shuncom` 后，Windows 应能正常访问共享。如果仍有问题，请贴出 `tail -f /var/log/samba/log.smbd` 在 Windows 尝试连接时的**具体错误日志**。

# ubuntu14.04离线安装telnet

在 **Ubuntu 14.04** 上离线安装 Telnet（服务端），本质是：

> 在有网络的机器下载 `.deb` 依赖包 → 拷贝到目标机 → 本地安装

下面给你标准可执行流程。

---

# 一、需要安装的核心包

Ubuntu 14.04 Telnet 服务端通常需要：

- `telnetd`
- `xinetd`
- 可能的依赖包（libwrap0 等）

---

# 二、有网机器上下载离线包

假设一台同版本 Ubuntu 14.04 可联网。

## 1️⃣ 建立下载目录

```bash
mkdir telnet_offline
cd telnet_offline

```

## 2️⃣ 下载所有依赖（关键命令）

```bash
apt-get install --download-only telnetd xinetd

```

下载后的包默认在：

```
/var/cache/apt/archives/

```

拷贝出来：

```bash
cp /var/cache/apt/archives/*.deb ~/telnet_offline/

```

---

## 更干净的方法（推荐）

```bash
apt-get download telnetd xinetd
apt-cache depends telnetd xinetd

```

然后把依赖包也逐个下载：

```bash
apt-get download 包名

```

但 14.04 比较老，直接用 `--download-only` 更简单。

---

# 三、拷贝到目标服务器

通过：

- scp
- U 盘
- 挂载 ISO
- 内网共享

把所有 `.deb` 文件放到：

```
/root/telnet_offline/

```

---

# 四、目标机离线安装

进入目录：

```bash
cd /root/telnet_offline

```

安装：

```bash
dpkg -i *.deb

```

如果提示依赖问题：

```bash
dpkg -i *.deb
apt-get -f install

```

⚠ 注意：

`apt-get -f install` 不能联网，否则会报错。  
在完全离线情况下，只要依赖包齐全，dpkg 即可成功。

---

# 五、启动服务

```bash
service xinetd restart

```

确认端口：

```bash
netstat -utpln | grep 23

```

---

# 六、极端离线场景（无同版本机器）

如果没有 Ubuntu 14.04 联网环境，可以：

### 方案 A：使用 Ubuntu 14.04 官方 ISO

挂载 ISO：

```bash
mount -o loop ubuntu-14.04.iso /mnt

```

然后：

```bash
dpkg -i /mnt/pool/main/t/telnetd/*.deb

```

---

### 方案 B：直接手动下载（旧源地址）

Ubuntu 14.04 属于 old-releases：

```
http://old-releases.ubuntu.com/ubuntu/pool/main/t/telnetd/

```

用浏览器下载对应版本的：

- telnetd\_xxx.deb
- xinetd\_xxx.deb
- libwrap0\_xxx.deb

---

# 七、最小依赖组合（经验值）

Ubuntu 14.04 通常只需：

```
libwrap0
xinetd
telnetd

```

安装顺序：

```bash
dpkg -i libwrap0*.deb
dpkg -i xinetd*.deb
dpkg -i telnetd*.deb

```

---

# 八、提醒（重要）

Telnet：

- 明文密码
- 无加密
- 公网绝对禁止
- 只能作为临时救援

你现在的真实目的应该是：

> 在重启 ssh 之前准备一个备用通道

在这种情况下：

✔ 安装  
✔ 测试可登录  
✔ 重启 ssh  
✔ ssh 正常后立即删除 telnet

---

# 更安全替代方案（强烈建议）

与其装 Telnet，不如：

```bash
/usr/sbin/sshd -p 2222

```

启动第二个 SSH 实例

这比 Telnet 安全 100 倍。

---

你的安装是成功的，但 **telnet 服务并没有被 xinetd 启用**，所以没有监听 23 端口。

从你输出看：

- xinetd 已运行
- telnetd 已安装
- 但 `xinetd` 没有加载 telnet 服务配置
- 所以 23 不监听

这是 Ubuntu 14.04 的一个典型问题：  
`telnetd` 安装后不会自动在 `/etc/xinetd.d/` 生成 telnet 配置。

---

# 一、确认 telnet 配置文件是否存在

执行：

```bash
ls -l /etc/xinetd.d/

```

看是否有：

```
telnet

```

如果没有这个文件 → 必须手动创建。

---

# 二、手动创建 telnet xinetd 配置

创建文件：

```bash
vi /etc/xinetd.d/telnet

```

写入以下内容：

```text
service telnet
{
    disable         = no
    flags           = REUSE
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/sbin/in.telnetd
    log_on_failure  += USERID
}

```

保存退出。

---

# 三、确认 in.telnetd 存在

执行：

```bash
ls -l /usr/sbin/in.telnetd

```

如果不存在，说明 telnetd 包异常。

正常应该存在。

---

# 四、重启 xinetd

```bash
service xinetd restart

```

然后检查：

```bash
netstat -utpln | grep :23

```

正常应看到：

```
0.0.0.0:23 LISTEN

```

⚠ 注意：

xinetd 是“按需监听”，有时只显示在：

```bash
ss -lntp | grep 23

```

---

# 五、如果还是没有 23 端口

再检查 xinetd 是否读取配置：

```bash
grep telnet /etc/services

```

应该有：

```
telnet 23/tcp

```

然后查看日志：

```bash
tail -n 20 /var/log/syslog

```

看 xinetd 是否报错。

---

# 六、最常见原因

90% 情况是：

> `/etc/xinetd.d/telnet` 不存在  
> 或者 `disable = yes`

---

# 七、确认 xinetd 是否真的加载了 telnet

执行：

```bash
xinetd -d

```

会进入 debug 模式。

如果 telnet 服务被加载，会看到：

```
Service telnet added

```

---

# 八、补充说明

你现在 netstat 输出里没有 23：

说明：

✔ telnetd 安装成功  
✔ xinetd 在运行  
❌ 但 telnet 服务未启用

---

# 九、重要提醒（你当前的场景）

你装 telnet 是为了：

> 给 ssh 重启前做备用通道

在这种情况下，我仍然建议：

## 比 telnet 更安全的方式

直接启动一个备用 ssh：

```bash
/usr/sbin/sshd -p 2222

```

然后测试：

```bash
ssh root@IP -p 2222

```

成功后再 restart 原 ssh。

这比 telnet 安全太多。

---

# 现在请你执行

```bash
ls /etc/xinetd.d/

```

把输出发我。

我可以 10 秒内判断问题点。

# HTTP POST 请求超时（Read timed out）引起系统高负载排查

从你提供的错误日志截图来看，核心问题是：

> **HTTP POST 请求超时（Read timed out）**  
> 目标地址：`http://192.168.95.80:6060/api/api/token`  
> 异常类型：`ResourceAccessException` → 嵌套异常是 `SocketTimeoutException: Read timed out`

---

## 🔍 问题详解

### 1. 发生了什么？

你的 Java 应用（通过 `RestTemplate`）尝试向内部服务 `http://192.168.95.80:6060/api/api/token` 发起一个 **POST 请求**，但在等待对方响应时**超过了设定的读取超时时间**，于是抛出异常。

简单说：

> “我发了请求，但对方一直没回我数据，我等不及了，就报错了。”

---

### 2. 常见原因

#### ✅ 网络或目标服务不可达/慢

- 目标服务 `192.168.95.80:6060` 可能： 
    - 没有启动
    - 崩溃了 / 卡死了
    - 正在处理耗时操作（如查数据库、调用第三方接口）
    - 防火墙/安全组拦截了请求（虽然能连上端口，但无响应）

#### ✅ 超时设置太短

- `RestTemplate` 默认的读取超时可能是几秒，如果目标服务需要更长时间处理，就会触发超时。
- 特别是在获取 token 的场景中，可能涉及加密、鉴权、日志记录等耗时操作。

#### ✅ 服务端资源不足

- CPU、内存、线程池满、GC 停顿等都可能导致服务端无法及时响应。

#### ✅ 中间件或代理层问题

- 如果中间有 Nginx、网关、负载均衡器，它们也可能导致延迟或断开。

---

### 3. 关键线索

- 错误发生在 `SuqianHttpManager.postLightData(...)` → 说明是你自己封装的 HTTP 客户端方法。
- 调用链中有 `RetryAnnotationAwareRetryOperationsInterceptor` → 说明你可能配置了重试机制，但即使重试也失败了。
- RedisMessageListenerContainer 出现在栈顶 → 可能是异步监听器触发的请求，比如收到某个消息后去刷新 token，结果失败。

---

## 🛠️ 解决方案建议

### ✔️ 临时缓解：增加超时时间

如果你确定目标服务只是“慢”，不是“挂”，可以延长 RestTemplate 的超时设置：

```java
// 示例：自定义 RestTemplate 设置超时
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);   // 连接超时 5 秒
factory.setReadTimeout(30000);     // 读取超时 30 秒（根据业务调整）
restTemplate.setRequestFactory(factory);

```

或者使用 `HttpComponentsClientHttpRequestFactory`（推荐用于生产环境）：

```java
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(30000);
RestTemplate restTemplate = new RestTemplate(factory);

```

> 💡 注意：不要盲目设得太大，否则会导致线程阻塞过久，影响系统吞吐量。

---

### ✔️ 根本解决：排查目标服务

登录到 `192.168.95.80` 服务器，检查：

1. 服务是否运行？
    
    ```bash
    netstat -tlnp | grep 6060
    # 或
    ss -tlnp | grep 6060
    
    ```
2. 查看该服务的日志（尤其是 `/api/api/token` 接口相关日志），看是否有报错、卡顿、死锁。
3. 手动测试接口：
    
    ```bash
    curl -X POST http://192.168.95.80:6060/api/api/token \
         -H "Content-Type: application/json" \
         -d '{"your": "data"}' \
         --connect-timeout 5 \
         --max-time 30
    
    ```
    
    观察是否也能复现超时。
4. 检查系统资源：
    
    ```bash
    top
    free -h
    iostat -x 1
    
    ```

---

### ✔️ 架构优化建议

- **加熔断降级**：使用 Hystrix、Sentinel 等，在多次超时后自动降级，避免拖垮主流程。
- **异步化 + 队列**：如果是非实时必须的 token 刷新，可放入消息队列异步处理。
- **缓存 token**：避免每次都要远程获取 token，本地缓存有效期的 token。
- **健康检查 + 监控告警**：对 `192.168.95.80:6060` 做定期探活，提前发现故障。

---

## 📌 总结一句话

> 你的应用调用内网 token 服务时因对方响应太慢或未响应而超时，需要么调大超时时间应急，要么深入排查目标服务为何变慢或不可用。

---

✅ 下一步行动建议：

1. 先手动 `curl` 测试目标接口能否通、多久返回。
2. 查看目标服务日志和系统负载。
3. 根据情况决定是改超时参数，还是修复服务端性能问题。
4. 长期考虑加入缓存、熔断、监控等健壮性设计。

如需进一步分析，请提供：

- 目标服务的语言/框架（Spring Boot? Node.js?）
- 当前 RestTemplate 的超时配置
- 是否集群部署？有没有负载均衡？

我可以帮你定制具体解决方案。

# ubunru14.04统计所有连接IP及所属国家的脚本

在 Ubuntu 14.04 这种较老的系统上，要实现“IP 地址后显示国家”，最轻量且无需联网查询（速度快、不依赖外部 API）的方法是使用 **`geoip-bin`** 和 **`geoip-database`**。

这将使用本地的 GeoIP 数据库进行匹配。

### 第一步：安装 GeoIP 工具库

请执行以下命令安装必要的包：

```bash
sudo apt-get update
sudo apt-get install -y geoip-bin geoip-database

```

*注：`geoip-bin` 包含 `geoiplookup` 命令，`geoip-database` 包含免费的 GeoIP Country 数据库。*

### 第二步：修改脚本 `/home/shuncom/iplist.sh`

我们需要修改脚本，在统计出 IP 和数量后，循环读取每一行，查询国家代码并追加到后面。

请使用 `nano` 编辑脚本：

```bash
nano /home/shuncom/iplist.sh

```

**将脚本内容完全替换为以下内容：**

```bash
#!/bin/bash

# 定义变量
LOG_DIR="/home/shuncom/iplist"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
LOG_FILE="${LOG_DIR}/iplist-${TIMESTAMP}.log"
GEOIP_CMD="geoiplookup"

# 确保日志目录存在
if [ ! -d "$LOG_DIR" ]; then
    mkdir -p "$LOG_DIR"
fi

# 检查 geoiplookup 是否安装
if ! command -v $GEOIP_CMD &> /dev/null; then
    echo "Error: geoip-bin not installed. Please run: sudo apt-get install geoip-bin"
    # 即使没安装也生成日志，但标记错误
    echo "=== IP Connection Statistics (GeoIP Missing) ===" > "$LOG_FILE"
    echo "Generated at: $(date)" >> "$LOG_FILE"
    echo "ERROR: geoiplookup command not found." >> "$LOG_FILE"
    exit 1
fi

# 生成临时文件用于处理
TEMP_FILE=$(mktemp)

# 1. 获取网络连接统计
# 使用 netstat 或 ss
if command -v netstat &> /dev/null; then
    netstat -ntu 2>/dev/null | awk '{print $5}' | cut -d: -f1 | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | sort -nr > "$TEMP_FILE"
elif command -v ss &> /dev/null; then
    ss -ntu 2>/dev/null | awk '{print $5}' | cut -d: -f1 | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | sort | uniq -c | sort -nr > "$TEMP_FILE"
else
    echo "Error: Neither netstat nor ss found." > "$LOG_FILE"
    exit 1
fi

# 2. 写入日志头部
echo "=== IP Connection Statistics with Country ===" > "$LOG_FILE"
echo "Generated at: $(date)" >> "$LOG_FILE"
echo "Format: Count IP_ADDRESS (COUNTRY_CODE)" >> "$LOG_FILE"
echo "-------------------------------" >> "$LOG_FILE"

# 3. 逐行读取统计结果，查询国家并写入日志
# 读取格式：空格+数量 空格+IP
while read -r count ip; do
    # 跳过空行
    if [ -z "$ip" ]; then continue; fi
    
    # 查询国家 (geoiplookup 返回类似: GeoIP Country Edition: US, United States)
    # 我们只提取国家代码 (US, CN, etc.)
    country_info=$($GEOIP_CMD "$ip" 2>/dev/null)
    
    # 解析国家代码 (提取冒号后的第一个单词，通常是代码)
    # 示例输出: GeoIP Country Edition: US, United States
    if ; then
        country_code=$(echo "$country_info" | sed 's/.*GeoIP Country Edition: \([A-Z][A-Z]\).*/\1/')
        country_name=$(echo "$country_info" | sed 's/.*GeoIP Country Edition: [A-Z][A-Z], \(.*\)/\1/')
        output_line="$(printf '%6s' $count) $ip ($country_code - $country_name)"
    else
        # 如果查询失败或本地回环
        output_line="$(printf '%6s' $count) $ip (Unknown - Local/Private)"
    fi
    
    echo "$output_line" >> "$LOG_FILE"
done < "$TEMP_FILE"

# 清理临时文件
rm -f "$TEMP_FILE"

echo "Log created: $LOG_FILE"

```

**保存并退出** (`Ctrl+O`, `Enter`, `Ctrl+X`)。

### 第三步：赋予执行权限并测试

```bash
chmod +x /home/shuncom/iplist.sh

```

**手动运行一次测试效果：**

```bash
/home/shuncom/iplist.sh

```

然后查看生成的最新日志文件：

```bash
ls -lt /home/shuncom/iplist/ | head -n 2
# 假设最新文件是 iplist-2026-03-12_18-00-01.log (请替换为你实际的文件名)
cat /home/shuncom/iplist/iplist-*.log | tail -n 20

```

### 预期输出效果

修改后的日志文件内容将类似于：

```text
=== IP Connection Statistics with Country ===
Generated at: Thu Mar 12 18:00:01 ICT 2026
Format: Count IP_ADDRESS (COUNTRY_CODE - Country Name)
-------------------------------
   369 127.0.0.1 (Unknown - Local/Private)
   108 119.59.98.80 (CN - China)
    49 45.140.17.97 (NL - Netherlands)
    30 45.140.17.26 (NL - Netherlands)
     5 49.237.44.244 (TH - Thailand)
     5 223.24.143.8 (CN - China)
     ...

```

### 常见问题与优化

1. **关于速度**：  
    `geoiplookup` 是本地查询，速度非常快。但如果你的并发 IP 数量成千上万，`while` 循环可能会让脚本运行时间从几秒增加到几十秒。对于一般的服务器监控（几百个 IP），这个方法是完全可行的。
2. **关于 127.0.0.1**：  
    本地回环地址通常查不到国家，脚本中做了特殊处理显示为 `Local/Private`。
3. **定时任务无需修改**：  
    你之前设置的 `*/10 19-22 * * *` 依然有效，它会自动调用这个新版本的脚本。
4. **数据库更新**：  
    Ubuntu 14.04 自带的 GeoIP 数据库可能比较旧（截至 2014-2015 年）。如果需要非常精准的最新归属地，需要手动下载最新的 `.dat` 文件替换 `/usr/share/geoip/GeoIP.dat`，但在老系统上，自带库通常足够用于区分主要国家。

# Red Hat Enterprise Linux 9.7 离线部署docker 及 docker compose

下面给出在 **Red Hat Enterprise Linux 9.7** 上的完整操作流程，包括：

1. 关闭 SELinux
2. 离线安装 **Docker**
3. 离线安装 **Docker Compose**

整个流程适合 **完全离线环境**（无法访问互联网）。

---

# 一、关闭 SELinux

## 1 临时关闭（立即生效）

```bash
setenforce 0

```

检查状态：

```bash
getenforce

```

如果返回：

```
Permissive

```

说明已临时关闭。

---

## 2 永久关闭

编辑配置文件：

```bash
vi /etc/selinux/config

```

修改：

```ini
SELINUX=disabled

```

原始可能是：

```ini
SELINUX=enforcing

```

保存后 **重启系统**：

```bash
reboot

```

验证：

```bash
getenforce

```

输出应为：

```
Disabled

```

---

# 二、准备 Docker 离线安装包

需要从一台 **可以联网的 Linux 机器** 下载以下 RPM：

Docker 在 RHEL9 需要这些包：

```
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
docker-compose-plugin

```

官方下载地址：

```
https://download.docker.com/linux/centos/9/x86_64/stable/Packages/

```

说明：

虽然是 CentOS 路径，但 **RHEL9 完全兼容**。

---

## 推荐下载版本（稳定）

例如：

```
containerd.io
docker-ce
docker-ce-cli
docker-buildx-plugin
docker-compose-plugin

```

下载示例：

```bash
wget https://download.docker.com/linux/centos/9/x86_64/stable/Packages/containerd.io-*.rpm
wget https://download.docker.com/linux/centos/9/x86_64/stable/Packages/docker-ce-*.rpm
wget https://download.docker.com/linux/centos/9/x86_64/stable/Packages/docker-ce-cli-*.rpm
wget https://download.docker.com/linux/centos/9/x86_64/stable/Packages/docker-buildx-plugin-*.rpm
wget https://download.docker.com/linux/centos/9/x86_64/stable/Packages/docker-compose-plugin-*.rpm

```

然后打包：

```bash
tar czvf docker-rhel9.tar.gz *.rpm

```

拷贝到离线服务器。

---

# 三、离线安装 Docker

进入 RPM 目录：

```bash
cd /root/docker-rpm

```

安装：

```bash
dnf install -y *.rpm

```

如果没有 dnf repo：

```bash
rpm -ivh *.rpm

```

推荐：

```bash
dnf install *.rpm

```

dnf 会自动处理依赖。

---

# 四、启动 Docker

安装完成后：

启动：

```bash
systemctl start docker

```

设置开机启动：

```bash
systemctl enable docker

```

查看状态：

```bash
systemctl status docker

```

---

# 五、验证 Docker

```bash
docker version

```

示例输出：

```
Client: Docker Engine
Server: Docker Engine

```

测试：

```bash
docker info

```

---

# 六、Docker Compose

RHEL9 推荐使用 **docker compose plugin**。

验证：

```bash
docker compose version

```

如果正常：

```
Docker Compose version v2.x

```

使用方法：

```bash
docker compose up -d

```

注意：

RHEL9 不再推荐旧命令：

```
docker-compose

```

而是：

```
docker compose

```

---

# 七、常见离线依赖问题

RHEL9 可能缺少：

```
libcgroup
iptables
container-selinux

```

如果安装报错，需要额外下载：

```
container-selinux

```

下载地址：

```
https://rpmfind.net/linux/rpm2html/search.php?query=container-selinux

```

---

# 八、生产环境建议配置

编辑：

```bash
vi /etc/docker/daemon.json

```

推荐：

```json
{
 "log-driver": "json-file",
 "log-opts": {
   "max-size": "100m",
   "max-file": "3"
 },
 "storage-driver": "overlay2"
}

```

重启：

```bash
systemctl restart docker

```

---

# 九、开放 Docker API（如果需要）

Docker 默认端口：

```
2375
2376

```

如果使用 **firewalld**：

```bash
firewall-cmd --permanent --add-port=2375/tcp
firewall-cmd --reload

```

---

# 十、验证容器运行

导入测试镜像（离线）：

```bash
docker load -i nginx.tar

```

运行：

```bash
docker run -d -p 80:80 nginx

```

查看：

```bash
docker ps

```

---

# 十一、运维经验（RHEL9 + Docker）

生产服务器建议：

1️⃣ 关闭 SELinux 或设为 permissive  
2️⃣ 使用 **overlay2 存储驱动**  
3️⃣ `/var/lib/docker` 单独磁盘  
4️⃣ 开启日志轮转  
5️⃣ 防火墙限制 docker API

---

💡 如果需要，我可以再给你一份 **“RHEL9 Docker 完整离线安装包结构”**（企业运维常用），特点：

- **只需一个 tar 包**
- 解压后一条命令安装
- 自动解决所有依赖
- 支持 **docker + compose + buildx**

很多数据中心离线部署都是这种方式。

# ubuntu18.04安装ntp，并配置内网校时，含离线安装NTP方法

在你的场景里，本质是做一个**NTP层级结构**：

- **server-1**：能访问互联网 → 作为上游时间源（NTP Server）
- **server-2**：不能访问互联网 → 从 server-1 同步时间（NTP Client）

下面一步步配置 👇

---

# 一、在 server-1 上配置（作为 NTP 服务器）

### 1. 安装 NTP 服务

```bash
sudo apt update
sudo apt install ntp

```

---

### 2. 修改配置文件

编辑：

```bash
sudo vim /etc/ntp.conf

```

确保有公网时间源（默认一般已经有）：

```bash
server 0.ubuntu.pool.ntp.org iburst
server 1.ubuntu.pool.ntp.org iburst
server 2.ubuntu.pool.ntp.org iburst

```

---

### 3. 允许内网 client（server-2）访问

在配置中添加（很关键）：

```bash
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

```

👉 根据你的内网网段修改，比如：

- `192.168.0.0`
- `10.0.0.0`

---

### 4. 重启服务

```bash
sudo systemctl restart ntp

```

---

### 5. 检查是否正常同步公网

```bash
ntpq -p

```

看到类似：

```
*ntp1.aliyun.com

```

说明已经同步成功 ✅

---

### 6. 确认监听端口

```bash
sudo netstat -lnup | grep 123

```

---

# 二、在 server-2 上配置（作为客户端）

---

### 1. 安装 NTP

```bash
sudo apt update
sudo apt install ntp

```

---

### 2. 修改配置文件

```bash
sudo vim /etc/ntp.conf

```

👉 注释掉所有公网 server：

```bash
# server 0.ubuntu.pool.ntp.org iburst
# server 1.ubuntu.pool.ntp.org iburst

```

👉 添加 server-1：

```bash
server 192.168.1.10 iburst

```

（这里填 server-1 的IP）

---

### 3. 重启服务

```bash
sudo systemctl restart ntp

```

---

### 4. 验证同步状态

```bash
ntpq -p

```

看到：

```
*192.168.1.10

```

说明已经成功同步 🎉

---

# 三、可选优化（推荐）

### 1. 强制 server-1 作为时间源（避免无外网时漂移）

在 server-1 的 `/etc/ntp.conf` 添加：

```bash
local stratum 10

```

---

### 2. 防火墙放行（如果有防火墙）

server-1 需要开放 UDP 123：

```bash
sudo ufw allow 123/udp

```

---

### 3. 手动立即校时（客户端）

server-2 可以执行一次：

```bash
sudo ntpdate 192.168.1.10

```

---

# 四、整体结构图

```
互联网 NTP
     ↓
 server-1（有公网）
     ↓
 server-2（内网）

```

---

# 五、常见问题排查

### ❌ server-2 不同步？

检查：

1. 能否 ping 通 server-1
2. 防火墙是否放行 123/UDP
3. server-1 是否在监听
4. `ntpq -p` 是否有 `reach` 值（非0）

---

在 Ubuntu 18.04 上离线安装 `ntp`，核心思路是：**在有网的机器上下载 `.deb` 安装包及其所有依赖包，拷贝到无网机器上，然后使用 `dpkg` 进行批量安装。**

由于 `ntp` 依赖于 `libopts25` 等库，直接拷贝一个 `ntp.deb` 通常会报错缺失依赖。最稳妥的方法是下载整个依赖树。

以下是具体操作步骤：

### 准备工作

- **机器 A**：有互联网权限的服务器（或任何一台能上网的 Ubuntu 18.04/相似环境电脑）。
- **机器 B**：目标服务器（无网，需要安装 ntp）。
- **传输工具**：U 盘、或者通过 `scp` 从机器 A 传到机器 B。

---

### 第一步：在机器 A（有网）上下载安装包及依赖

我们需要利用 `apt-get download` 配合 `--print-deps` 来一次性下载所有需要的包。

1. **创建存放目录**
    
    ```bash
    mkdir ~/ntp-offline-packages
    cd ~/ntp-offline-packages
    
    ```
2. **下载 ntp 及其所有依赖包**  
    执行以下命令。这条命令会解析 `ntp` 的所有依赖，并下载当前系统架构（通常是 amd64）对应的所有 `.deb` 文件。
    
    ```bash
    # 获取依赖列表并下载
    apt-get download $(apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances ntp | grep "^\w")
    
    ```
    
    *解释：*
    
    
    - `apt-cache depends ...`：列出 ntp 的所有深层依赖。
    - `grep "^\w"`：过滤出有效的包名。
    - `apt-get download`：只下载 `.deb` 包到当前目录，不进行安装（所以不会报依赖错误）。
3. **确认下载结果**  
    查看目录下是否有多个 `.deb` 文件：
    
    ```bash
    ls -lh *.deb
    
    ```
    
    您应该能看到类似 `ntp_*.deb`, `libopts25_*.deb`, `libedit2_*.deb` 等文件（具体依赖数量视系统环境而定，通常 5-10 个左右）。
4. **打包文件**  
    为了方便传输，打个包：
    
    ```bash
    cd ..
    tar -czvf ntp-offline.tar.gz ntp-offline-packages
    
    ```

---

### 第二步：传输到机器 B（无网）

将 `ntp-offline.tar.gz` 复制到机器 B。

- **方法 1 (U 盘)**：拷贝到 U 盘，插到机器 B 上挂载复制。
- **方法 2 (SCP)**：如果机器 B 虽然没外网但能和机器 A 内网互通： ```bash
    # 在机器 A 上执行
    scp ntp-offline.tar.gz user@server2-ip:/home/user/
    
    ```

---

### 第三步：在机器 B（无网）上安装

1. **解压文件**
    
    ```bash
    tar -xzvf ntp-offline.tar.gz
    cd ntp-offline-packages
    
    ```
2. **批量安装所有 .deb 包**  
    使用 `dpkg -i` 配合通配符 `*.deb` 一次性安装当前目录下所有的包。`dpkg` 会自动处理同一批次内的依赖关系。
    
    ```bash
    sudo dpkg -i *.deb
    
    ```
3. **验证安装结果**
    
    
    - **检查状态**：
        
        ```bash
        dpkg -l | grep ntp
        
        ```
        
        如果状态显示 `ii`，说明安装成功。
    - **检查服务**：
        
        ```bash
        systemctl status ntp
        
        ```
        
        如果看到 `active (running)`，说明服务已启动。
    - **如果报错 "dependency problems"**:  
        如果在第 2 步中仍然报错缺少某个特定的包（极少见，除非机器 A 和 B 的系统版本差异巨大，比如一个是 18.04.1 一个是 18.04.6 且基础库不同），您需要记下缺少的包名，回到机器 A，单独下载那个包：
        
        ```bash
        # 在机器 A 上
        apt-get download <缺失的包名>
        
        ```
        
        然后再次拷贝到机器 B 运行 `sudo dpkg -i *.deb`。

---

### 第四步：配置时间同步

安装完成后，您就可以按照之前的方案配置 `/etc/ntp.conf` 了：

1. **编辑配置**：
    
    ```bash
    sudo vim /etc/ntp.conf
    
    ```
    
    
    - 注释掉所有 `pool` 或 `server` 指向外网的行。
    - 添加指向服务器 1 的内网 IP： ```conf
        server 192.168.x.x iburst  # 替换为服务器 1 的 IP
        
        ```
2. **重启服务**：
    
    ```bash
    sudo systemctl restart ntp
    
    ```
3. **验证同步**：
    
    ```bash
    ntpq -p
    
    ```
    
    看到服务器 1 的 IP 前面出现 `*` 号即表示成功。

### 💡 备选方案：如果无法下载依赖

如果机器 A 和机器 B 的小版本号差异太大导致依赖包不匹配，或者您觉得下载依赖太麻烦，还有一个“简单粗暴”但**不推荐用于生产环境**的方法：

使用 `ntpdate` 命令（无需守护进程，只需一个二进制文件）：

1. 在机器 A 上下载 `ntpdate` 包：`apt-get download ntpdate`。
2. 如果它依赖 `libopts25`，也一起下载。
3. 在机器 B 上 `dpkg -i *.deb`。
4. 在机器 B 上写个定时任务：`*/5 * * * * /usr/sbin/ntpdate -u 192.168.x.x`。  
    *(缺点：时间是跳变的，不是平滑过渡，可能导致日志时间错乱，但在极度受限环境下可用)*。

**首选方案依然是上述的完整 `ntp` 离线安装。**

# 在 Ubuntu 20.04 上安装 OpenClaw

在 Ubuntu 20.04 上安装 OpenClaw，推荐使用官方的一键安装脚本，这是最简单快捷的方法。如果遇到问题，也可以采用手动安装方式。

### 🚀 方案一：一键安装（推荐）

官方提供的一键安装脚本会自动完成所有配置，非常适合新手。

1. **执行安装命令**  
    在终端中运行以下命令，脚本将自动下载并安装 OpenClaw 及其所有依赖。 ```bash
    curl -fsSL https://openclaw.ai/install.sh | bash
    
    ```
    
    如果因网络问题导致脚本无法下载，可以尝试使用以下国内镜像源的脚本： ```bash
    curl -fsSL https://gitee.com/openclaw-mirror/install-script/raw/main/install.sh | bash
    
    ```
2. **跟随引导**  
    安装过程中，终端会提示你进行一些基础配置，例如选择模型、配置 Channel 等。通常直接按回车键使用默认选项即可，这些配置后续也可以在 Web 界面中修改。
3. **验证安装**  
    安装成功后，终端会显示 “OpenClaw installed successfully” 并提供控制面板的访问地址（默认为 `http://127.0.0.1:18789/openclaw`）。

### 🛠️ 方案二：手动安装

如果一键安装失败，或者你希望更精细地控制安装过程，可以按照以下步骤手动安装。

#### 1. 环境准备

- **更新系统并安装基础工具**```bash
    sudo apt update && sudo apt upgrade -y
    sudo apt install -y curl git build-essential libssl-dev
    
    ```
- **安装 Node.js (版本 ≥ 22)**  
    OpenClaw 依赖 Node.js 22 或更高版本。使用 NodeSource 源安装是稳定且推荐的方式。 ```bash
    # 导入 NodeSource 22.x 源
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
    # 安装 Node.js
    sudo apt install -y nodejs
    # 验证安装
    node --version  # 应显示 v22.x.x
    npm --version   # 应显示 10.x.x 或更高
    
    ```
- **配置国内 NPM 镜像（可选）**  
    为加速依赖包下载，建议将 NPM 镜像源切换到国内。 ```bash
    npm config set registry https://registry.npmmirror.com
    
    ```

#### 2. 安装 OpenClaw

- **使用 NPM 全局安装**```bash
    sudo npm install -g openclaw@latest
    
    ```

#### 3. 初始化配置

- **启动初始化向导**  
    安装完成后，运行以下命令来配置模型、API Key 和后台服务等。 ```bash
    openclaw onboard --install-daemon
    
    ```
    
    按照终端提示完成配置即可。

### ⚠️ 常见问题与排查

- **CMake 版本过低**  
    Ubuntu 20.04 默认的 CMake 版本可能低于 OpenClaw 要求的 3.19+，这可能导致编译依赖时出错。如果遇到相关问题，需要手动升级 CMake。
- **防火墙阻止访问**  
    OpenClaw 的控制面板默认使用 **18789** 端口。如果无法通过浏览器访问，请检查并放行该端口。 ```bash
    sudo ufw allow 18789/tcp
    sudo ufw reload
    
    ```
- **命令未找到**  
    如果安装后提示 `openclaw: command not found`，可能是 NPM 的全局路径未被系统识别。可以尝试重新打开终端，或手动创建软链接来解决。

在 Ubuntu 20.04 上，系统默认的 CMake 版本通常是 3.16.x，而 OpenClaw 等现代项目往往要求 3.19+ 甚至更高。

针对你的情况，我推荐**方法一（官方二进制包 + 软链接）**，因为它最安全、快速，且不会破坏系统原有的依赖环境。

### 🚀 方法一：使用官方二进制包（推荐，最快且安全）

这种方法通过下载官方编译好的包，解压后通过“软链接”让系统优先使用新版本，而保留旧版本以防万一。

#### 1. 下载最新版 CMake

首先，我们需要去官网下载最新的预编译包（目前最新版已接近 4.0，这里以 3.28.1 为例，你可以替换为更新的版本号）。

```bash
# 进入临时目录
cd /tmp

# 下载 CMake (请根据需要修改版本号，例如 v3.28.1 或 v3.30.0)
wget https://github.com/Kitware/CMake/releases/download/v3.28.1/cmake-3.28.1-linux-x86_64.tar.gz

```

#### 2. 解压到系统目录

我们将它解压到 `/opt` 目录下，这样比较规范，方便管理。

```bash
# 解压
sudo tar -xzf cmake-3.28.1-linux-x86_64.tar.gz -C /opt/

```

#### 3. 建立软链接（关键步骤）

为了让系统在输入 `cmake` 时调用新版本，我们需要建立软链接。这里推荐使用 `update-alternatives` 工具，它可以优雅地管理多个版本。

```bash
# 注册新版本到 alternatives 系统
# 解释：优先级设为 100 (数字越大优先级越高)
sudo update-alternatives --install /usr/bin/cmake cmake /opt/cmake-3.28.1-linux-x86_64/bin/cmake 100

# 强制更新链接（如果系统提示冲突）
sudo update-alternatives --set cmake /opt/cmake-3.28.1-linux-x86_64/bin/cmake

```

> **备选简单方案：** 如果你不想用 `update-alternatives`，也可以直接用一条简单的命令覆盖（但不如上面的方法灵活）：  
> `sudo ln -sf /opt/cmake-3.28.1-linux-x86_64/bin/cmake /usr/local/bin/cmake`

#### 4. 验证安装

```bash
cmake --version

```

如果输出显示 `cmake version 3.28.1`（或你安装的版本），说明升级成功。

---

### 📦 方法二：使用 Kitware 官方 APT 源（适合习惯 apt 管理的用户）

Kitware（CMake 的开发商）提供了官方的 APT 仓库，可以通过包管理器安装最新版。

1. **安装依赖并导入密钥：**
    
    ```bash
    sudo apt-get install -y software-properties-common lsb-release
    wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
    
    ```
2. **添加仓库源：**
    
    ```bash
    echo "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ focal main" | sudo tee /etc/apt/sources.list.d/kitware.list
    
    ```
3. **更新并安装：**
    
    ```bash
    sudo apt-get update
    sudo apt-get install -y cmake
    
    ```

---

### ⚙️ 方法三：源码编译（最慢，仅作为最后手段）

如果上述方法都因系统库版本过老（如 glibc 版本）而失败，你才需要尝试从源码编译。这需要较长时间。

1. **安装编译依赖：**```bash
    sudo apt-get update
    sudo apt-get install -y build-essential libssl-dev
    
    ```
2. **下载并编译：**```bash
    cd /tmp
    # 下载源码 (以 3.28.3 为例)
    wget https://cmake.org/files/v3.28/cmake-3.28.3.tar.gz
    tar -zxvf cmake-3.28.3.tar.gz
    cd cmake-3.28.3
    
    # 配置、编译、安装
    ./bootstrap
    make -j$(nproc)  # 使用多核编译加速
    sudo make install
    
    ```

### 💡 总结建议

- **首选方法一**：它不会干扰系统自带的 Python 或其他工具对旧版 CMake 的依赖，且升级过程秒级完成。
- **安装完 OpenClaw 后**：如果遇到权限问题，记得给 OpenClaw 的相关目录赋予权限，或者使用 `sudo` 运行。

# redhat 9.7 安装docker，关闭selinux，及其他常规操作

在没有正版授权的情况下，无法直接使用 Red Hat 官方的软件仓库。不过，你可以通过配置第三方软件源来在线安装 Docker 等软件。

对于 Red Hat 系统，推荐使用 Docker 官方为 RHEL 提供的通用基础镜像（UBI）对应的软件仓库，这是合规且稳定的方式。

### 🚀 在线安装 Docker

你可以通过以下步骤，将 Docker 的官方仓库地址替换为国内的镜像源（以北京外国语大学镜像站为例），然后进行安装。

1. **移除旧版本**  
    如果你的系统上安装过旧版本的 Docker，建议先将其卸载。
    
    ```bash
    sudo dnf remove docker \
                    docker-client \
                    docker-client-latest \
                    docker-common \
                    docker-latest \
                    docker-latest-logrotate \
                    docker-logrotate \
                    docker-engine
    
    ```
2. **安装依赖并添加仓库**  
    首先安装必要的工具，然后添加 Docker 的官方仓库配置文件，并立即将其中的下载链接替换为国内镜像源地址。
    
    ```bash
    # 安装 dnf 插件
    sudo dnf -y install dnf-plugins-core
    
    # 添加 Docker 官方仓库
    sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    # 将仓库地址替换为国内镜像源
    sudo sed -i 's+https://download.docker.com+https://mirrors.bfsu.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
    
    ```
3. **安装 Docker Engine**  
    完成以上配置后，就可以直接通过 `yum` 命令安装 Docker 了。
    
    ```bash
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
    ```
4. **启动并验证**  
    安装完成后，启动 Docker 服务并检查其状态。
    
    ```bash
    # 启动 Docker 服务
    sudo systemctl start docker
    
    # 设置开机自启
    sudo systemctl enable docker
    
    # 验证安装是否成功
    sudo docker run hello-world
    
    ```

### 💡 补充说明

- **关于 CentOS Vault**：你可能会看到一些资料提到将 RHEL/CentOS 的源指向 `vault.centos.org`。这是因为 CentOS 7 已经停止维护（EOL），官方源已失效。但对于 RHEL 系统，更推荐上述使用 Docker 官方 UBI 源的方式，它更直接且专为 RHEL 设计。
- **其他镜像源**：如果上述镜像源访问不畅，你也可以尝试替换为其他国内镜像源，例如： 
    - 阿里云：`https://developer.aliyun.com/mirror/docker-ce`
    - 清华大学：`https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/`在 Red Hat Enterprise Linux 9.7 中，关闭 SELinux 的方式与旧版本（RHEL 8 及以前）有显著不同。
        
        **核心提示：** 在 RHEL 9 中，仅修改配置文件将 SELinux 设置为 `disabled` **并不能**从内核层面彻底禁用它，系统仍会加载 SELinux 模块但处于“无策略”状态，这可能会消耗系统资源或在某些极端情况下导致服务启动异常。
        
        要在 RHEL 9.7 中**彻底且正确**地关闭 SELinux，推荐使用**内核参数**方式。
        
        以下是两种方法的详细步骤：
        
        ### 🚀 方法一：内核参数禁用（RHEL 9 推荐，彻底生效）
        
        这是 RHEL 9 官方推荐的方式，能够确保 SELinux 在系统启动时完全不加载。
        
        
        1. **执行命令添加内核参数**  
            使用 `grubby` 工具向内核启动参数中添加 `selinux=0`。这会自动修改 GRUB 配置，无需手动编辑文件。
            
            ```bash
            sudo grubby --update-kernel ALL --args selinux=0
            
            ```
        2. **重启系统**  
            修改内核参数后，必须重启才能生效。
            
            ```bash
            sudo reboot
            
            ```
        3. **验证状态**  
            重启后，输入以下命令查看状态：
            
            ```bash
            getenforce
            
            ```
            
            
            - 如果输出 `Disabled`，说明 SELinux 已彻底关闭。
            - 你也可以通过查看内核命令行来确认参数是否生效： ```bash
                cat /proc/cmdline
                
                ```
                
                （输出中应包含 `selinux=0`）
        
        ---
        
        ### 📝 方法二：修改配置文件（传统方式，RHEL 9 中不彻底）
        
        如果你只是想让 SELinux 停止拦截操作（即切换到宽容模式），或者在无法使用 `grubby` 的特殊环境下，可以使用此方法。但在 RHEL 9 中，这不会像旧版本那样完全卸载 SELinux。
        
        
        1. **编辑配置文件**  
            打开 SELinux 的主配置文件：
            
            ```bash
            sudo vim /etc/selinux/config
            
            ```
        2. **修改参数**  
            找到 `SELINUX=enforcing` 这一行，将其修改为 `disabled`。
            
            ```ini
            # 修改前
            SELINUX=enforcing
            
            # 修改后
            SELINUX=disabled
            
            ```
            
            *(注：如果你不想彻底关闭，只是想让服务不报错，建议改为 `permissive`，这样既能记录日志又不会拦截操作。)*
        3. **保存并重启**  
            保存文件（在 vim 中按 `Esc`，输入 `:wq` 回车），然后重启系统。
            
            ```bash
            sudo reboot
            
            ```
        
        ---
        
        ### 🛑 临时关闭（无需重启）
        
        如果你正在排查故障，不想重启服务器，可以临时将 SELinux 切换为**宽容模式（Permissive）**。这不会彻底关闭它，但会停止拦截行为。
        
        ```bash
        # 临时切换为宽容模式（只记录日志，不拦截）
        sudo setenforce 0
        
        # 查看当前状态
        getenforce
        # 输出应为 Permissive
        
        ```
        
        *注意：`setenforce 0` 只是临时生效，重启后会恢复配置文件中的设置。*
        
        ### 📊 总结对比
        
        <table><thead><tr><th align="left">方式</th><th align="left">命令/操作</th><th align="left">适用场景</th><th align="left">RHEL 9 特性</th></tr></thead><tbody><tr><td align="left">**彻底禁用**</td><td align="left">`grubby --update-kernel ALL --args selinux=0`</td><td align="left">生产环境、彻底移除 SELinux 影响</td><td align="left">**推荐**，内核级完全禁用</td></tr><tr><td align="left">**配置禁用**</td><td align="left">修改 `/etc/selinux/config` 为 `disabled`</td><td align="left">一般测试</td><td align="left">**不推荐**，RHEL 9 中仅停止加载策略，仍占资源</td></tr><tr><td align="left">**临时宽容**</td><td align="left">`setenforce 0`</td><td align="left">故障排查、调试</td><td align="left">立即生效，重启失效</td></tr></tbody></table>
        
        **回滚方法：**  
        如果你想重新开启 SELinux，只需执行以下命令移除参数并重启：
        
        ```bash
        sudo grubby --update-kernel ALL --remove-args selinux
        sudo reboot
        
        ```
    -

# Red Hat 9.7 上通过二进制方式安装 Docker 29.4.0

要在 Red Hat 9.7 上通过二进制方式安装 Docker 29.4.0，你需要手动下载官方提供的静态二进制包，并进行一些手动配置。这种方式非常适合离线环境或需要精确控制版本的场景。

以下是完整的安装步骤：

### 📥 下载 Docker 二进制包

首先，你需要从 Docker 官方仓库下载指定版本的静态二进制压缩包。

```bash
# 下载 Docker 29.4.0 的静态二进制包
wget https://download.docker.com/linux/static/stable/x86_64/docker-29.4.0.tgz

```

### 🛠️ 解压并安装

下载完成后，解压文件并将所有可执行文件移动到系统的可执行路径中。

```bash
# 1. 解压下载的压缩包
tar -zxvf docker-29.4.0.tgz

# 2. 将解压出的所有二进制文件复制到 /usr/bin/ 目录
sudo cp docker/* /usr/bin/

```

### ⚙️ 配置 Systemd 服务

为了让 Docker 能够作为系统服务运行，并支持开机自启，需要创建并配置一个 systemd 服务单元文件。

```bash
# 创建 docker.service 文件
sudo vi /usr/lib/systemd/system/docker.service

```

将以下内容完整复制到文件中：

```ini
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

```

### 🚀 启动并启用 Docker 服务

配置好服务文件后，重新加载 systemd 配置，然后启动 Docker 并设置为开机自启。

```bash
# 1. 重新加载 systemd 守护进程配置
sudo systemctl daemon-reload

# 2. 启动 Docker 服务
sudo systemctl start docker

# 3. 设置 Docker 服务开机自启
sudo systemctl enable docker

```

### ✅ 验证安装

最后，通过查看 Docker 版本来确认安装是否成功。

```bash
docker --version

```

如果命令输出显示 `Docker version 29.4.0, build ...`，则表明二进制安装已成功完成。

# redhat9二进制安装mysql

虽然目前 RHEL 9 系列（截止 2026 年）通常以 9.4 或 9.5 为主，但二进制安装（Generic Binaries）的方法在所有 RHEL 9 版本中是通用的。

使用二进制包安装的好处是\*\*不污染系统库\*\*，且可以自定义安装路径。以下是标准安装流程：

\### 一、 环境准备

在安装之前，需要清理系统自带的 mariadb 库并安装必要的依赖。

 1. \*\*卸载冲突包：\*\*

 ```bash

 dnf remove -y mariadb-libs

 ```

 2. \*\*安装依赖库：\*\*

 MySQL 运行需要 libaio 和 ncurses-compat-libs（RHEL 9 可能需要 ncurses-libs）。

 ```bash

 dnf install -y libaio ncurses-libs pkgconf-pkg-config

 ```

\### 二、 下载与解压

建议从 \[可疑链接已删除\] 下载对应 \*\*Linux - Generic (glibc 2.28) (x86, 64-bit)\*\* 的版本。

 1. \*\*创建用户与组：\*\*

 ```bash

 groupadd mysql

 useradd -r -g mysql -s /bin/false mysql

 ```

 2. \*\*解压安装包：\*\*

 假设你已下载 mysql-8.0.xx-linux-glibc2.28-x86\_64.tar.xz。

 ```bash

 tar -xvf mysql-8.0.xx-linux-glibc2.28-x86\_64.tar.xz -C /usr/local/

 cd /usr/local/

 mv mysql-8.0.xx-linux-glibc2.28-x86\_64 mysql

 ```

\### 三、 目录配置与初始化

 1. \*\*创建数据目录：\*\*

 ```bash

 mkdir -p /data/mysql

 chown -R mysql:mysql /usr/local/mysql

 chown -R mysql:mysql /data/mysql

 ```

 2. \*\*初始化数据库：\*\*

 执行后请\*\*务必记录\*\*输出末尾生成的临时密码。

 ```bash

 /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

 ```

\### 四、 配置文件 (/etc/my.cnf)

创建一个基础配置文件，确保路径指向正确。

```ini

\[mysqld\]

basedir=/usr/local/mysql

datadir=/data/mysql

socket=/tmp/mysql.sock

port=3306

user=mysql

\[mysql\]

socket=/tmp/mysql.sock

```

\### 五、 配置服务启动

 1. \*\*设置环境变量：\*\*

 ```bash

 echo 'export PATH=$PATH:/usr/local/mysql/bin' &gt;&gt; /etc/profile

 source /etc/profile

 ```

 2. \*\*配置 Systemd 服务：\*\*

 创建 /etc/systemd/system/mysqld.service 文件：

 ```ini

 \[Unit\]

 Description=MySQL Server

 After=network.target

 \[Service\]

 Type=simple

 User=mysql

 Group=mysql

 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

 LimitNOFILE=65535

 \[Install\]

 WantedBy=multi-user.target

 ```

 3. \*\*启动并自启：\*\*

 ```bash

 systemctl daemon-reload

 systemctl enable --now mysqld

 ```

\### 六、 安全加固

使用刚才记录的\*\*临时密码\*\*登录：

```bash

mysql -u root -p

```

进入后修改密码并允许远程访问（按需）：

```sql

ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewStrongPassword!';

FLUSH PRIVILEGES;

```

\### 💡 避坑指南

 \* \*\*GLIBC 版本：\*\* RHEL 9 使用的是较新的 glibc (2.34+)，因此务必选择 glibc 2.28 或更高版本的二进制包，不要错下成 2.12/2.17 的版本。

 \* \*\*防火墙：\*\* 如果需要远程连接，记得开启端口：

 firewall-cmd --add-port=3306/tcp --permanent &amp;&amp; firewall-cmd --reload

 \* \*\*SELinux：\*\* 如果启动失败，可以先尝试 setenforce 0 排除权限干扰，或者针对数据目录设置上下文。

# ubuntu解压以.001 .002结尾的分卷ZIP文件2种方法

在 Ubuntu 系统中，解压这种以 `.001`、`.002` 结尾的分卷 ZIP 文件，最推荐使用 `7z` 命令，因为它最智能，不需要你手动合并文件。

这里为你提供两种方法，你可以根据实际情况选择：

### 方法一：使用 7z 命令（最推荐，一步到位）

`7z` 工具非常强大，它会自动识别并读取后续的分卷文件，你只需要对第一个分卷（`.001`）执行解压命令即可。

1. **安装 7z 工具**（如果系统中还没有安装）： ```bash
    sudo apt update
    sudo apt install p7zip-full
    
    ```
2. **直接解压第一个分卷**： ```bash
    7z x rulr.zip.001
    
    ```
    
    执行后，`7z` 会自动把 `rulr.zip.002`、`rulr.zip.003` 等后续分卷全部关联起来并解压到当前目录。

### 方法二：使用 cat 和 unzip 命令（传统方法）

如果你不想安装新软件，也可以利用系统自带的命令，先将所有分卷合并成一个完整的 ZIP 文件，然后再解压。

1. **合并所有分卷**： 使用 `cat` 命令将所有分卷按顺序合并成一个名为 `rulr_full.zip` 的文件： ```bash
    cat rulr.zip.* > rulr_full.zip
    
    ```
2. **解压合并后的文件**： 使用 `unzip` 命令解压这个合并后的完整文件： ```bash
    unzip rulr_full.zip
    
    ```

### ⚠️ 解压前的关键注意事项

- **文件完整性**：确保 `rulr.zip.001`、`rulr.zip.002`、`rulr.zip.003` 都在**同一个文件夹**内，且没有遗漏任何一个分卷。
- **文件名不要改动**：保持分卷文件原始的命名规则，不要随意重命名，否则会导致合并或自动识别失败。

建议优先尝试**方法一**，不仅操作简单，而且处理大文件时效率更高。

# NebiOS系统远程桌面控制方法

您提到的“nebios系统”很可能是指 **NetBSD**，这是一个开源的类Unix操作系统。要远程控制一台 NetBSD 电脑，通常有两种主流方法：使用 VNC 或使用 xrdp。

以下是两种方法的详细配置步骤。

### 🛠️ 准备工作

在开始之前，请确保您的 NetBSD 电脑满足以下条件：

1. **已安装图形界面 (X Window System)**：远程桌面需要图形环境才能正常工作。
2. **拥有管理员权限**：后续操作需要使用 `sudo` 或以 root 用户执行。
3. **网络连通**：确保控制端和被控的 NetBSD 电脑在同一网络内，或已通过其他方式（如VPN）打通网络。

---

### 方法一：使用 VNC (Virtual Network Computing)

VNC 是一种跨平台的远程控制协议，非常灵活。这里以 TightVNC 为例。

#### 1. 在 NetBSD 上安装并配置 VNC 服务器

1. **安装 TightVNC** 打开终端，使用 pkgsrc 包管理器安装 TightVNC server：
    
    ```bash
    sudo pkgin install tightvncserver
    
    ```
2. **设置 VNC 访问密码** 首次运行前，需要为 VNC 服务设置一个连接密码。
    
    ```bash
    vncserver
    
    ```
    
    系统会提示您输入并确认一个密码。这个密码仅用于 VNC 连接，与您的系统登录密码无关。
3. **配置启动脚本** VNC 服务器启动时会读取 `~/.vnc/xstartup` 文件来决定加载哪个桌面环境。您需要编辑此文件。
    
    ```bash
    nano ~/.vnc/xstartup
    
    ```
    
    将文件内容修改为以下内容（以启动 XFCE4 桌面为例，这是一种轻量级桌面）：
    
    ```bash
    #!/bin/sh
    xrdb $HOME/.Xresources
    startxfce4 &
    
    ```
    
    *注意：如果您的 NetBSD 安装的是 GNOME 或 KDE 等其他桌面环境，请将 `startxfce4 &` 替换为对应的启动命令。*
4. **重启 VNC 服务** 先停止刚才创建的会话（默认为 :1），然后重新启动。
    
    ```bash
    vncserver -kill :1
    vncserver
    
    ```
    
    成功后，您会看到类似 `New 'X' desktop is your-hostname:1` 的提示。
5. **开放防火墙端口** VNC 服务默认使用 `5900 + 显示编号` 的端口。`:1` 对应的就是 `5901` 端口。您需要确保 NetBSD 的防火墙允许该端口的 TCP 连接。
    
    ```bash
    # 如果您使用的是 pf 防火墙，需要在 /etc/pf.conf 中添加规则
    # 例如：pass in on egress proto tcp to port 5901
    # 然后重载规则：sudo pfctl -f /etc/pf.conf
    
    ```

#### 2. 从其他电脑连接

在您用来控制的电脑（无论是 Windows、macOS 还是 Linux）上，安装一个 VNC 客户端（Viewer），例如 RealVNC Viewer 或 TigerVNC。

- **服务器地址**：填写 NetBSD 电脑的 IP 地址和端口，格式为 `IP地址:端口号`，例如 `192.168.1.100:5901`。
- **密码**：输入您在第 2 步中设置的 VNC 密码。

---

### 方法二：使用 xrdp (RDP 协议)

xrdp 允许您使用 Windows 自带的远程桌面协议（RDP）来连接 NetBSD，对于 Windows 用户来说非常方便。

#### 1. 在 NetBSD 上安装并配置 xrdp

1. **安装 xrdp**
    
    ```bash
    sudo pkgin install xrdp
    
    ```
2. **启动并启用 xrdp 服务** 启动服务并将其设置为开机自启。
    
    ```bash
    # 启动服务
    sudo service xrdp start
    # 启用开机自启 (具体命令取决于您的 init 系统)
    sudo rcctl enable xrdp
    
    ```
3. **开放防火墙端口** RDP 协议默认使用 **3389** 端口。请确保 NetBSD 的防火墙允许该端口的 TCP 连接。
    
    ```bash
    # 如果您使用的是 pf 防火墙，需要在 /etc/pf.conf 中添加规则
    # 例如：pass in on egress proto tcp to port 3389
    # 然后重载规则：sudo pfctl -f /etc/pf.conf
    
    ```

#### 2. 从其他电脑连接

- **Windows 用户**：直接使用系统自带的“远程桌面连接”工具。
- **Linux/macOS 用户**：可以使用 Remmina 等支持 RDP 协议的客户端。
- **计算机名/IP地址**：填写 NetBSD 电脑的 IP 地址。
- **登录凭据**：在弹出的登录框中，输入您在 NetBSD 系统上的**用户名和密码**。

---

### ⚖️ 两种方法对比

<table id="bkmrk-%E7%89%B9%E6%80%A7-vnc-xrdp-%E5%8D%8F%E8%AE%AE-rfb-%28"><thead><tr><th align="left">特性</th><th align="left">VNC</th><th align="left">xrdp</th></tr></thead><tbody><tr><td align="left">**协议**</td><td align="left">RFB (Remote Frame Buffer)</td><td align="left">RDP (Remote Desktop Protocol)</td></tr><tr><td align="left">**优点**</td><td align="left">跨平台兼容性极佳，几乎所有系统都有客户端</td><td align="left">Windows 原生支持，无需额外安装客户端；连接体验通常更流畅</td></tr><tr><td align="left">**缺点**</td><td align="left">可能需要手动配置桌面环境</td><td align="left">对非 RDP 客户端不友好</td></tr><tr><td align="left">**默认端口**</td><td align="left">5901 (对应显示编号 :1)</td><td align="left">3389</td></tr></tbody></table>

您可以根据自己的需求和使用习惯选择其中一种方法进行配置。

# RedHat9.7用iso创建本地仓库

你这个报错本质上不是 `dnf download` 的问题，而是：

你的机器没有可用的软件仓库（repo）。

关键报错：

```text
No package dnf-plugins-core available

```

以及：

```text
This system is not registered with an entitlement server

```

说明当前：

- 没注册 Red Hat Subscription
- 或没有配置本地/镜像仓库
- 所以 `dnf` 根本找不到包

---

# 先确认仓库状态

执行：

```bash
dnf repolist all

```

如果看到：

```text
repo status: disabled

```

或者压根没 repo。

那就需要先配置仓库。

---

# 最简单方案（推荐）

# 使用 RHEL 9.7 ISO 作为本地仓库

这是企业离线环境最常见做法。

---

# 第一步：挂载 ISO

假设 ISO 文件：

```text
/root/rhel-9.7-x86_64-dvd.iso

```

执行：

```bash
mkdir -p /mnt/rhel9

mount -o loop /root/rhel-9.7-x86_64-dvd.iso /mnt/rhel9

```

确认：

```bash
ls /mnt/rhel9

```

应该看到：

```text
AppStream
BaseOS
EFI
images
isolinux
...

```

---

# 第二步：创建本地 repo

创建：

```bash
cat >/etc/yum.repos.d/rhel9-local.repo <<EOF
[BaseOS]
name=RHEL9 BaseOS
baseurl=file:///mnt/rhel9/BaseOS
enabled=1
gpgcheck=0

[AppStream]
name=RHEL9 AppStream
baseurl=file:///mnt/rhel9/AppStream
enabled=1
gpgcheck=0
EOF

```

---

# 第三步：清理缓存

```bash
dnf clean all
rm -rf /var/cache/dnf

```

---

# 第四步：重新生成缓存

```bash
dnf makecache

```

正常会看到：

```text
Metadata cache created.

```

---

# 第五步：安装

现在再执行：

```bash
dnf install -y dnf-plugins-core

```

就可以了。

---

# 如果还是报错

执行：

```bash
dnf list available | grep dnf-plugins-core

```

正常应该能看到：

```text
dnf-plugins-core.noarch

```

---

# 额外说明

在 RHEL 9：

- `dnf-plugins-core`  
    通常位于：
- AppStream 仓库

所以：

- BaseOS 开了
- AppStream 没开

也会报你这个错误。

---

# 企业里更常见情况

很多公司：

- 不注册 Red Hat
- 使用：
    
    
    - Satellite
    - Nexus
    - Harbor
    - 本地 YUM 源
    - ISO repo

所以：  
“Unable to read consumer identity”  
通常可以忽略，不是致命错误。

真正的问题是：  
没有可用 repo。

---

# 一条命令快速验证 ISO repo 是否成功

执行：

```bash
dnf repolist

```

应该看到：

```text
repo id        repo name
AppStream      RHEL9 AppStream
BaseOS         RHEL9 BaseOS

```

这就正常了。

现在你的 repo 已经正常了。  
问题变成：

`dnf-plugins-core` 包不存在于当前启用仓库的 metadata 里。

但在 RHEL 9 里，这个包通常应该存在。

---

先直接查一下：

```bash
dnf search dnf-plugins-core

```

再查：

```bash
dnf list available | grep dnf-plugin

```

---

# 很大概率原因

你当前挂载的 ISO 不是完整 DVD ISO。

很多人用的是：

- boot.iso
- minimal.iso

这些里面：

- 没有 AppStream 完整包
- 没有 dnf-plugins-core

---

# 验证 ISO 是否完整

执行：

```bash
find /mnt/rhel9/AppStream/Packages | grep dnf-plugins-core

```

如果没输出。

说明：  
你的 ISO 里根本没有这个包。

---

# 正确 ISO

你需要的是：

```text
rhel-9.7-x86_64-dvd.iso

```

不是：

```text
boot.iso
minimal.iso

```

---

# 还有一种情况（RHEL9 常见）

RHEL9 有些插件已经拆分。

你真正需要的可能只是：

```bash
dnf config-manager

```

这个命令来自：

```text
dnf-plugins-core

```

但部分环境可能已经内置。

直接试：

```bash
dnf config-manager --help

```

如果能执行：

说明你根本不需要安装。

---

# 再进一步检查

查看命令来自哪个包：

```bash
rpm -qf $(which dnf)

```

以及：

```bash
rpm -qa | grep dnf

```

---

# 在 RHEL9 更推荐的安装方式

你其实可以直接安装：

```bash
dnf install 'dnf-command(config-manager)'

```

因为 RHEL9 支持 command provides。

执行：

```bash
dnf provides */config-manager

```

会告诉你真正包名。

---

# 最可能的最终解决方案

直接执行：

```bash
dnf install 'dnf-command(config-manager)'

```

通常就成功了。

因为：

- RHEL8/9 很多教程还是旧写法
- 老教程写：  
    `dnf install dnf-plugins-core`
- 新系统更推荐：  
    `dnf install 'dnf-command(xxx)'`

---

# 你当前系统其实已经不是“完全离线”

因为你有：

- docker-ce-stable
- epel

说明：

- repo 已经工作
- metadata 正常

所以现在不是离线问题。

而是：

- 包名
- ISO内容
- repo内容

的问题。

# 在 Ubuntu 22.04 上安装中文语言包

在 Ubuntu 22.04 上安装中文语言包，主要有两种方法：**图形界面操作**（简单直观）和 **命令行操作**（快速高效）。你可以根据自己的使用习惯选择其中一种。

### 🖥️ 方法一：图形界面设置（推荐新手）

这种方法全程通过鼠标点击完成，非常适合刚接触 Ubuntu 的用户。

1. **打开系统设置**
    - 点击屏幕左上角的“活动”（Activities），搜索并打开“设置”（Settings）。
2. **进入语言配置**
    - 在设置窗口中，找到并点击“区域与语言”（Region &amp; Language）。
3. **安装中文语言包**
    - 点击“管理已安装的语言”（Manage Installed Languages）。如果是首次操作，系统会提示“语言支持没有完全安装”，点击“安装”即可。
    - 在弹出的“语言支持”窗口中，点击“添加或删除语言...”。
    - 在列表中找到“中文（简体）”（Chinese (Simplified)），勾选后点击“应用”（Apply）。系统会自动下载并安装语言包。
4. **设置为默认语言**
    - 安装完成后，在“语言支持”窗口的语言列表中，将“汉语（中国）”拖拽到列表的最顶端，使其成为首选语言。
    - 点击“应用到整个系统”（Apply System-Wide）按钮。
5. **重启生效**
    - 系统会提示需要注销并重新登录才能生效。点击屏幕右上角的用户名，选择“注销”（Log Out），然后重新登录即可看到完整的中文界面。

### 💻 方法二：命令行操作

如果你习惯使用终端，或者正在使用服务器版本，可以通过几条命令快速完成设置。

1. **更新软件包列表** 打开终端（快捷键 `Ctrl+Alt+T`），首先执行以下命令来更新软件源信息：
    
    ```bash
    sudo apt update
    
    ```
2. **安装中文语言包** 接着，执行此命令来安装简体中文语言包和 GNOME 桌面环境所需的中文支持：
    
    ```bash
    sudo apt install language-pack-zh-hans language-pack-gnome-zh-hans -y
    
    ```
3. **设置系统默认语言** 安装完毕后，运行以下命令将系统的默认语言环境（Locale）设置为中文：
    
    ```bash
    sudo update-locale LANG=zh_CN.UTF-8
    
    ```
4. **重启系统** 最后，重启计算机以使所有更改生效：
    
    ```bash
    sudo reboot
    
    ```

---

### ⌨️ 补充：安装中文输入法

更换系统语言后，你可能还需要一个中文输入法。Ubuntu 22.04 默认使用 IBus 框架，可以安装智能拼音。

1. **安装智能拼音** 在终端中执行： ```bash
    sudo apt install ibus-libpinyin -y
    
    ```
2. **添加输入源**
    - 安装后，建议重启或重新登录。
    - 进入“设置” -&gt; “键盘” -&gt; “输入源”。
    - 点击“+”号，选择“汉语”，然后添加“中文（智能拼音）”。
    - 之后你就可以通过 `Super键 (Win键) + 空格` 来切换中英文输入法了。

# 在 Ubuntu 14.04通过 up ip route add ... 添加永久静态路由

你的配置**基本是正确的**，在 Ubuntu 14.04（ifupdown 管理网络）里，确实可以通过 `up ip route add ...` 添加永久静态路由。

你这段：

```bash
auto eth0
iface eth0 inet static
    address 192.168.95.208
    netmask 255.255.255.0
    
    # 添加永久静态路由
    up ip route add 10.6.203.0/24 via 192.168.95.1 dev eth0

```

含义是：

- 当 `eth0` 启动时
- 添加一条到 `10.6.203.0/24`
- 下一跳是 `192.168.95.1`
- 走 `eth0`

这是合法且常见的写法。

不过有几点建议。

---

# 建议增加 `down` 删除路由

否则网卡重启时可能报：

```bash
RTNETLINK answers: File exists

```

推荐完整写法：

```bash
auto eth0
iface eth0 inet static
    address 192.168.95.208
    netmask 255.255.255.0
    gateway 192.168.95.1

    up ip route add 10.6.203.0/24 via 192.168.95.1 dev eth0
    down ip route del 10.6.203.0/24 via 192.168.95.1 dev eth0

```

---

# 另外要确认：

## 1. `192.168.95.1` 必须可达

它必须是：

- eth0 同网段
- 可 ping 通
- 真正能转发到 `10.6.203.0/24`

否则路由无效。

---

## 2. 不要和默认路由冲突

如果：

- eth0
- eth1

都配置了 `gateway`

Ubuntu 14.04 可能会产生双默认路由问题。

通常：

- 只保留一个默认网关
- 另一块网卡只加静态路由

例如：

```bash
eth0 -> 默认网关
eth1 -> 业务网段静态路由

```

---

# 推荐查看当前路由

配置后执行：

```bash
ip route

```

应该看到：

```bash
10.6.203.0/24 via 192.168.95.1 dev eth0

```

---

# Ubuntu 14.04 特别注意

14.04 默认没有 netplan，使用的是：

```bash
/etc/network/interfaces

```

所以你这种 `up ip route add` 的方式是正确时代的方法。

---

# 生效方式

修改后：

```bash
sudo ifdown eth0 && sudo ifup eth0

```

或者：

```bash
sudo service networking restart

```

查看：

```bash
ip route

```

验证：

```bash
ping 10.6.203.x

```

---

# 更稳妥的写法（推荐）

有些老系统 `ip` 命令路径问题，建议：

```bash
up /sbin/ip route add 10.6.203.0/24 via 192.168.95.1 dev eth0
down /sbin/ip route del 10.6.203.0/24 via 192.168.95.1 dev eth0

```

Ubuntu 14.04 上兼容性更好。

# 银河麒麟桌面 V11 wayland 切换为 X11

现在已经确认了：你当前图形会话确实是 **Wayland**。

```bash
echo $XDG_SESSION_TYPE
wayland

```

并且你的环境是：

- 银河麒麟桌面 V11
- UKUI
- LightDM
- NVIDIA 驱动已安装（截图里有 `xserver-xorg-video-nvidia-570`）

这类环境可以直接强制切换到 X11。

先创建 LightDM 配置：

```bash
sudo mkdir -p /etc/lightdm/lightdm.conf.d
sudo nano /etc/lightdm/lightdm.conf.d/99-force-x11.conf

```

写入：

```ini
[Seat:*]
type=xlocal
xserver-command=X

```

保存后，再创建 UKUI 会话默认配置：

```bash
sudo nano /usr/share/xsessions/ukui.desktop

```

检查是否存在以下内容（没有就添加）：

```ini
[Desktop Entry]
Name=UKUI
Comment=UKUI Desktop Environment
Exec=ukui-session
Type=Application
DesktopNames=UKUI

```

然后重启 LightDM：

```bash
sudo systemctl restart lightdm

```

如果当前在图形界面里操作，执行上面命令会直接退出桌面，所以更稳妥的是：

```bash
sudo reboot

```

重启后登录，再验证：

```bash
echo $XDG_SESSION_TYPE

```

应变成：

```bash
x11

```

如果重启后仍显示 `wayland`，说明 UKUI V11 的 Wayland 会话被单独指定了。继续执行下面命令查看系统有哪些会话：

```bash
ls /usr/share/wayland-sessions/
ls /usr/share/xsessions/

```

把输出发来，我再告诉你如何把默认会话从 `ukui-wayland` 改成 `ukui-x11`。

从结果看，LightDM 配置里没有指定 `user-session`，所以不是这里覆盖的。现在几乎可以确定是 **银河麒麟的 `kylin-wlcom`（Wayland 组件）在启动链里强制接管了会话**。

执行下面这个定位命令：

```bash
grep -R "kylin-wlcom" /etc /usr /var 2>/dev/null

```

重点找类似下面内容：

```ini
user-session=kylin-wlcom

```

或：

```ini
Exec=kylin-wlcom

```

不过根据你当前环境，更快的办法是直接把默认 Wayland 会话移走，让 LightDM 只能看到 X11 会话。

先备份：

```bash
sudo mv /usr/share/wayland-sessions/kylin-wlcom.desktop \
/usr/share/wayland-sessions/kylin-wlcom.desktop.bak

```

然后重启：

```bash
sudo reboot

```

登录后验证：

```bash
echo $XDG_SESSION_TYPE

```

预期：

```bash
x11

```

如果以后要恢复 Wayland：

```bash
sudo mv /usr/share/wayland-sessions/kylin-wlcom.desktop.bak \
/usr/share/wayland-sessions/kylin-wlcom.desktop
sudo reboot

```

这一步的原理是：你已经确认系统里存在唯一的 X11 会话：

```bash
/usr/share/xsessions/ukui.desktop

```

而 Wayland 会话只有：

```bash
/usr/share/wayland-sessions/kylin-wlcom.desktop

```

移走 `kylin-wlcom.desktop` 后，登录管理器就只能启动 UKUI 的 X11 会话。

做完后把：

```bash
echo $XDG_SESSION_TYPE

```

的结果发一下。

# Debian / Ubuntu / Q4OS (基于 Debian 系)安装拼音输入法

要在 Fcitx 输入法框架中添加中文拼音输入，通常有两种情况：一种是刚刚安装好 Fcitx 框架但还没有拼音引擎，另一种是已经安装了拼音引擎但需要手动添加。

请根据你的实际情况，按照以下步骤操作：

### 第一步：确认已安装拼音引擎（关键）

很多时候无法添加中文输入，是因为只安装了 Fcitx 框架（外壳），而没有安装具体的拼音输入法软件（引擎）。

你需要先打开终端（Konsole），根据你的 Linux 发行版执行安装命令：

- **Debian / Ubuntu / Q4OS (基于 Debian 系):**
    
    ```bash
    sudo apt update
    sudo apt install fcitx-pinyin fcitx-table-wubi
    
    ```
    
    *(注：`fcitx-pinyin` 是最基础的拼音输入法，如果想要更智能的云词库，可以尝试安装 `fcitx-googlepinyin` 或 `fcitx-sunpinyin`)*
- **Arch Linux:**
    
    ```bash
    sudo pacman -S fcitx-im fcitx-configtool fcitx-pinyin
    
    ```
- **Fedora:**
    
    ```bash
    sudo dnf install fcitx fcitx-pinyin
    
    ```

**安装完成后，请务必重启 Fcitx 或重启电脑**，让新安装的输入法生效。

### 第二步：通过配置界面添加拼音

安装好引擎后，你需要将其添加到激活列表中：

1. **打开配置工具**：
    
    
    - 在开始菜单中搜索“Fcitx 配置”或“Fcitx Configuration”。
    - 或者在系统托盘（屏幕右下角）找到 Fcitx 的键盘图标，右键点击选择“配置”。
2. **取消“仅显示当前语言”**：
    
    
    - 在配置窗口的下方，找到并**取消勾选**“仅显示当前语言”或“Only Show Current Language”选项。这一步非常重要，否则你可能看不到拼音输入法。
3. **添加输入法**：
    
    
    - 点击窗口下方的“+”号（添加）。
    - 在弹出的搜索框中输入“Pinyin”或“拼音”。
    - 你应该能看到“汉语 (Pinyin)”或“Fcitx Pinyin”的选项。
    - 选中它，点击“确定”或“添加”。
4. **调整顺序（可选）**：
    
    
    - 在“激活的输入法”列表中，你可以通过上下箭头调整输入法的顺序。通常建议把“键盘-英语”放在第一位，把“Pinyin”放在第二位，这样开机默认是英文，按切换键后才是中文。

### 第三步：检查环境变量（如果上述步骤无效）

如果安装了也添加了，但在任何软件里都调不出来，可能是环境变量没配对。

请检查你的 `~/.xprofile` 或 `~/.bashrc` 文件（取决于你的系统启动方式），确保包含以下内容：

```bash
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS="@im=fcitx"

```

添加后，注销并重新登录系统即可生效。

**总结：** 绝大多数情况下，你只需要执行 **`sudo apt install fcitx-pinyin`**（针对 Debian/Ubuntu 系），然后重启电脑，系统就会自动识别并允许你添加中文拼音了。

# Ubuntu 25.10 原生桌面远程，终于跑通了....再也不用买 mac 了

<div class="rich_media_meta_list" id="bkmrk-" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px 0px 22px; padding: 0px; outline: 0px; line-height: 20px; font-size: 0px; overflow-wrap: break-word; hyphens: auto; position: relative; z-index: 1; color: rgba(0, 0, 0, 0.9); font-family: 'PingFang SC', system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span class="rich_media_meta rich_media_meta_nickname" id="bkmrk--1" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px 10px 10px 0px; padding: 0px; outline: 0px; display: inline-block; vertical-align: middle; font-size: 15px; position: relative;"></span><span class="" id="bkmrk--2" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px;"></span></div><span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">由于家里没有 MacMini，所以之前 OpenClaw 刚起来的时候，我用零刻 Me Mini 单独跑了一套 Ubuntu 25.10 的裸系统。本地图形化桌面么，不用去配置远程、映射，直接连 HDMI 就能用。</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">但是随着跑的业务越来越多以后，我发现只用 IM 工具和 SSH 并不能很好的解决我使用它的场景。于是我开始尝试使用 windows 原生的 RDP 来远程。</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">远程，很简单对吧。脑子里第一反应就是装了 xrdp，然后搞定收工。</span>

```
<svg aria-label="插图" height="13px" role="img" style="visibility: visible;" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" style="visibility: visible;"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" style="visibility: visible;"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" style="visibility: visible;"></ellipse></svg>```
sudo apt install xrdp
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">然后我还很熟练的关闭 Wayland，开启 X11，等我悄咪咪打开 mstsc 远程桌面连接后 —— </span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">黑屏。</span>**

**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">？？？</span>**

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible;">大概是 GUI 不对，于是在网上找了一些方案，跑了各种 GUI，结果发现最大的问题就是没办法使用原生 Wayland，我跑在桌面上各种配置好的网页、应用，一到 RDP 就变成了丑不拉几的界面，一点用不下去。</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">我是真不懂阿，毕竟很少想不开远程 Ubuntu 的。然后看着价格高居不下的 Mac Mini，我决心还是换个思路来解决远程的问题。</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">后来 OpenClaw 提醒我，GNOME 自带了一个叫 </span>`<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">gnome-remote-desktop</span>`<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 的组件，</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">原生支持 Wayland 下的 RDP</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">。而且系统里其实已经预装了，只是没有启用。</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
# 启用它
systemctl --user enable --now gnome-remote-desktop.service
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">然后设置一下 RDP 的登录凭据：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
# 需要连接到桌面会话的 D-Bus
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

# 设置用户名和密码
grdctl rdp set-credentials myuser mypassword
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">生成 TLS 证书（RDP 需要 TLS 加密）：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
mkdir -p ~/.local/share/gnome-remote-desktop

openssl req -x509 -newkey rsa:2048 \
  -keyout ~/.local/share/gnome-remote-desktop/rdp-tls-key.pem \
  -out ~/.local/share/gnome-remote-desktop/rdp-tls-cert.pem \
  -days 365 -nodes \
  -subj "/CN=$(hostname)"
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">指定证书路径：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
grdctl rdp set-tls-cert ~/.local/share/gnome-remote-desktop/rdp-tls-cert.pem
grdctl rdp set-tls-key ~/.local/share/gnome-remote-desktop/rdp-tls-key.pem
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">配置完成后查看状态：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
grdctl status --show-credentials
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">输出大概长这样：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
Overall:
    Unit status: active
RDP:
    Status: enabled
    Port: 3389
    TLS certificate: /home/user/.local/share/gnome-remote-desktop/rdp-tls-cert.pem
    TLS fingerprint: ab:cd:ef:...
    TLS key: /home/user/.local/share/gnome-remote-desktop/rdp-tls-key.pem
    View-only: no
    Username: myuser
    Password: mypassword
```
```

**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">完美！</span>**

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">然后熟练的反代 3389 端口，配置域名，打开远程 RDP，链接，</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">错误代码 0x904。</span>**

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">查了一下日志：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
journalctl --user -u gnome-remote-desktop.service --since "1 min ago"
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">看到了这行：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
gnome-remote-desktop-daemon: ../src/libei-connection.c:75: ei_connection_get_context: Assertion `connection' failed.
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">服务直接 </span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">中断</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 了。</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">上游 gnome-remote-desktop 49.0 的已知 bug。</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 当 RDP 客户端断开连接时，libei 试图获取一个已经不存在的连接上下文，然后——</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">砰</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">，断言失败，进程直接被杀死。</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">然后查了查相关的资料，发现</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">view-only 模式不走 libei。</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 然后测试了一下，果然，先开启 view-only，连接以后再关闭，这样远程 RDP 就可以直接操作 Ubuntu 桌面了 </span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">稳定如老狗</span>**

# <span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">开始起飞</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">然后我就创建了一个脚本，大致思路就是先 view-only 开启服务，连上后断 view-only，监听 3389 连接，无连接自动重启再次开启监听。</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
服务启动 → 开启 view-only（稳定，不崩溃）
    ↓
用户连上 RDP（view-only 模式，绕过 libei bug）
    ↓
检测到连接 → 关闭 view-only（libei 此时已经正常初始化）
    ↓
用户正常操作鼠标键盘
    ↓
用户断开 → 服务崩溃 → 自动重启 → 回到第一步
```
```

### <span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">第一步：给服务加崩溃自动重启</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
mkdir -p ~/.config/systemd/user/gnome-remote-desktop.service.d

cat > ~/.config/systemd/user/gnome-remote-desktop.service.d/override.conf << 'EOF'
[Service]
Restart=always
RestartSec=1
EOF
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">这样 libei 崩了之后，1 秒内自动重启。</span>

### <span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">第二步：创建 Watcher 脚本</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">这是整个方案的核心。用端口连接状态来判断用户是否在线：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
mkdir -p ~/.local/bin

cat > ~/.local/bin/rdp-watcher.sh << 'SCRIPT'
#!/bin/bash
# RDP Watcher - View-Only 自动切换
# 原理：检测 3389 端口 TCP 连接状态
#   无连接 → view-only ON（稳定）
#   有连接 → view-only OFF（可操作）

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
state="idle"

while true; do
  if ss -tn state established 2>/dev/null | grep -q ':3389'; then
    if [ "$state" = "idle" ]; then
      sleep 1
      grdctl rdp disable-view-only >/dev/null 2>&1
      state="connected"
    fi
  else
    if [ "$state" = "connected" ]; then
      # 连接断开了，等服务崩溃重启稳定后，再开 view-only
      sleep 5
      grdctl rdp enable-view-only >/dev/null 2>&1
      state="idle"
    elif [ "$state" = "idle" ]; then
      # 空闲时确保 view-only 是开着的
      grdctl rdp enable-view-only >/dev/null 2>&1
    fi
  fi
  sleep 2
done
SCRIPT

chmod +x ~/.local/bin/rdp-watcher.sh
```
```

<div class="rich_media_content js_underline_content
                       autoTypeSetting24psection
            " id="bkmrk-%E2%80%A2-%E6%AF%8F-2-%E7%A7%92%E6%A3%80%E6%B5%8B%E4%B8%80%E6%AC%A1-3389-%E7%AB%AF%E5%8F%A3%E6%9C%89" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; color: rgba(0, 0, 0, 0.9); font-size: 17px; overflow: hidden; text-align: justify; position: relative; z-index: 0; user-select: text; font-family: 'PingFang SC', system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">- <section style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">• 每 2 秒检测一次 3389 端口有没有 TCP 连接</span></section>
- <section style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">• 没连接 → 开 view-only，等待用户连接</span></section>
- <section style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">• 有连接 → 关 view-only，用户可以操作</span></section>
- <section style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">• 连接断开 → 等 5 秒（让崩溃和重启完成）→ 重新开 view-only</span></section>

</div>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">不用日志，不用 D-Bus 监听，就一个 </span>`<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">ss</span>`<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 命令，简单粗暴且可靠。</span>**

### <span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">第三步：注册为 systemd 用户服务</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
cat > ~/.config/systemd/user/rdp-watcher.service << 'EOF'
[Unit]
Description=RDP View-Only Auto-Switch Watcher
After=gnome-remote-desktop.service

[Service]
ExecStart=%h/.local/bin/rdp-watcher.sh
Restart=always
RestartSec=5

[Install]
WantedBy=default.target
EOF

systemctl --user daemon-reload
systemctl --user enable --now rdp-watcher.service
```
```

## <span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">✅ 最终效果</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">全部配置完成后：</span>

```
<svg aria-label="插图" height="13px" role="img" version="1.1" viewbox="0 0 450 130" width="45px" x="0px" xmlns="http://www.w3.org/2000/svg" y="0px"><ellipse cx="50" cy="65" fill="rgb(237,108,96)" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2"></ellipse><ellipse cx="225" cy="65" fill="rgb(247,193,81)" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2"></ellipse><ellipse cx="400" cy="65" fill="rgb(100,200,86)" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2"></ellipse></svg>```
# 确认两个服务都在跑
systemctl --user status gnome-remote-desktop.service
systemctl --user status rdp-watcher.service
```
```

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">然后用任意 RDP 客户端连接。</span>**<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">连上就能操作，断开再连也没问题。全程自动，不需要手动干预。</span>**

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">好了，我可以美美的开始办公远程做游戏了~~</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">三国曹操传美少女版，启动！</span>

<span style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">[![image_1.png](https://bost.lesrec.cn/uploads/images/gallery/2026-05/scaled-1680-/qHwE5Hij1kHKpNHG-image-1.png)](https://bost.lesrec.cn/uploads/images/gallery/2026-05/qHwE5Hij1kHKpNHG-image-1.png)</span>

<div class="rich_media_content js_underline_content
                       autoTypeSetting24psection
            " id="bkmrk--4" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); margin: 0px; padding: 0px; outline: 0px; color: rgba(0, 0, 0, 0.9); font-size: 17px; overflow: hidden; text-align: justify; position: relative; z-index: 0; user-select: text; font-family: 'PingFang SC', system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"></div>

# rulr-4 / rulr-5 的问题拆解

下面我按\*\*“conntrack异常 + 网络路径 + Docker/iptables结构 + 差异对比”\*\*四个层面，把 rulr-4 / rulr-5 的问题拆解清楚，并给出结论。

---

# 一、核心结论（先说重点）

两台机器的共同问题是：

### 1）conntrack “命中正常但插入完全失败”

两台都有典型特征：

- `insert=0`
- `insert_failed` 极高
- `drop == insert_failed` 完全一致

👉 说明不是“丢包问题”，而是：

> **conntrack 表处于高压/冲突/不可插入状态（典型：hash冲突 + 表竞争 + bucket/GC压力）**

---

### 2）流量结构是“Docker NAT + 高并发短连接”

特征：

- eth0 PPS：1300~3500 pps
- Docker bridge 转发活跃
- conntrack found 数量极高（十亿级累计）
- TIME\_WAIT / Established 很低（说明短连接或代理转发）

👉 本质是：

> **L4 NAT代理（nginx tcp / docker DNAT）+ 高频连接建立/释放**

---

### 3）CPU级 conntrack drop 在8核上均匀爆炸

例如 rulr-5：

- cpu0~cpu7 drop：1,000万~2,100万级
- search\_restart大量存在

👉 说明：

> **不是单核问题，是全局 hash table 争用 / bucket 冲突 / lock竞争**

---

# 二、关键异常点拆解

## 1. conntrack insert\_failed = drop（100%一致）

### rulr-4

```
insert_failed=7390187
drop=7390187

```

### rulr-5

```
insert_failed=14861324
drop=14861324

```

👉 结论非常关键：

> **conntrack 新连接“完全无法建立状态记录”**

但：

- `found` 极高 → 查询是正常的
- 说明：已有连接还能用，但**新流状态创建失败**

---

## 2. nf\_conntrack 参数异常点

### rulr-4

```
nf_conntrack_tcp_timeout_established = 3600

```

### rulr-5

```
nf_conntrack_tcp_timeout_established = 86400

```

👉 rulr-5 风险更高：

- 86400 = 24小时
- 连接表保留时间过长

### 影响：

> conntrack table “存活连接暴涨 + hash压力持续累积”

---

## 3. NAT/iptables结构问题（非常关键）

两台都有：

### DNAT + MASQUERADE + Docker bridge

例如 rulr-5：

```
POSTROUTING MASQUERADE (172.18.0.0/16)
DNAT 80/443/6100-6109 → 172.18.0.200

```

👉 这是典型：

> **“Docker NAT + 端口代理 + 二次转发”结构**

问题在于：

### ❗ 双重NAT路径

```
Client → eth0 → PREROUTING DNAT → Docker bridge → container
                       ↓
                conntrack 记录创建

```

再叠加：

- nginx tcp proxy
- 多端口转发（6011-6109）
- RabbitMQ / Redis / ZK

👉 结果：

> conntrack entry 数量爆炸 + hash冲突概率指数级上升

---

## 4. eth0 TX queue backlog（重要信号）

两台都看到：

```
tx_send_full: 88000~94000+

```

含义：

> NIC TX ring 有阻塞 / 发送队列曾经满过

👉 说明：

- CPU侧处理不过来 or softirq拥塞
- 或 conntrack/iptables路径延迟导致回压

---

## 5. RPS / IRQ 分布异常

```
/sys/class/net/eth0/queues/rx-*/rps_cpus = 00

```

👉 这是非常关键问题：

> **RPS（Receive Packet Steering）完全未启用**

结果：

- 所有流量集中在少数 CPU softirq
- conntrack lock contention 加剧
- hash table 并发冲突更严重

---

# 三、两台机器差异对比

<table id="bkmrk-%E9%A1%B9%E7%9B%AE-rulr-4-rulr-5-%E7%BB%93%E8%AE%BA-"><thead><tr><th>项目</th><th>rulr-4</th><th>rulr-5</th><th>结论</th></tr></thead><tbody><tr><td>conntrack timeout</td><td>3600</td><td>86400</td><td>rulr-5更危险</td></tr><tr><td>insert\_failed</td><td>739万</td><td>1486万</td><td>rulr-5更严重</td></tr><tr><td>网络PPS</td><td>~1300</td><td>~3500</td><td>rulr-5负载更高</td></tr><tr><td>docker网络</td><td>2个bridge</td><td>1个bridge</td><td>rulr-5更集中</td></tr><tr><td>CPU drop</td><td>均衡</td><td>更高</td><td>rulr-5更拥塞</td></tr><tr><td>RPS</td><td>未启用</td><td>未启用</td><td>两台共同瓶颈</td></tr></tbody></table>

---

# 四、根因总结（核心3点）

## 根因1：conntrack hash table 进入高冲突状态

表现：

- insert\_failed暴涨
- search\_restart大量
- drop == insert\_failed

👉 本质：

> bucket冲突 + 竞争锁 + CPU并发访问

---

## 根因2：Docker NAT + 高端口映射放大状态表压力

你现在是：

- nginx tcp proxy
- 10+端口区间映射
- redis/rabbit/zookeeper
- 统一 NAT

👉 典型放大效应：

> 每个连接 = 2~3个 conntrack entry

---

## 根因3：没有RPS/RFS，CPU softirq 单点瓶颈

导致：

- conntrack lock 争用加剧
- packet processing 串行化
- insert失败概率上升

---

# 五、优化建议（按优先级）

## ⭐ P0（必须做）

### 1. 开启 RPS（立刻见效）

```bash
for i in /sys/class/net/eth0/queues/rx-*/rps_cpus; do
    echo ff > $i
done

```

---

### 2. 调整 conntrack hashsize（如果允许）

```bash
sysctl -w net.netfilter.nf_conntrack_max=2097152

```

并确认：

```
/sys/module/nf_conntrack/parameters/hashsize

```

---

## ⭐ P1（结构优化）

### 3. 缩短 conntrack 生命周期（rulr-5必须改）

```bash
net.netfilter.nf_conntrack_tcp_timeout_established=3600

```

或更低（代理场景建议 600~1800）

---

### 4. 减少 Docker NAT 层级

建议：

- host network（关键服务）
- 或减少 DNAT 端口范围

---

## ⭐ P2（架构级）

### 5. nginx tcp proxy 优化

- 减少长连接 NAT
- 开启 upstream keepalive
- 减少 conntrack churn

---

### 6. 考虑 bypass conntrack（高级）

对纯转发流量：

```bash
iptables -t raw -A PREROUTING -j NOTRACK

```

⚠️ 需谨慎（会影响 NAT/stateful firewall）

---

# 六、最终判断

两台机器的本质问题不是“性能不够”，而是：

> ❗ **conntrack + Docker NAT + 高PPS流量 + RPS未开启 → 状态表锁竞争崩溃**

---

# 如果你下一步想继续深入

我可以帮你做三件更关键的分析：

### 1）判断是否 conntrack hash bucket 已经“爆桶”

### 2）计算你当前 conntrack 实际容量 vs 使用率

### 3）给出一套“代理服务器最优内核参数模板（8C/16C通用）”

只要你说一句“继续深入”，我可以把瓶颈定位到更精确的 kernel 层级。

你这个“清零问题”，需要先把一个关键点讲清楚：

> ❗ `conntrack -S` 不是计数器日志，而是 **内核全局累积统计（since boot）**

所以你看到的：

- drop
- insert\_failed
- search\_restart
- found

👉 都是**内核启动以来的累计值（cumulative counters）**

---

# 一、结论：不能“直接清零 conntrack -S”

Linux conntrack 统计来自：

```
/proc/sys/net/netfilter/nf_conntrack_stats

```

或者内核内部 struct nf\_conntrack\_stat

👉 **没有 reset / clear 接口**

---

# 二、你想要的“清零”，只有3种方法

## 方法1（唯一真正清零）：重载 nf\_conntrack 模块

⚠️ 会影响所有 NAT / Docker / 连接状态（生产慎用）

### 步骤：

```bash
systemctl stop docker

```

```bash
conntrack -F

```

然后：

```bash
modprobe -r nf_conntrack
modprobe nf_conntrack

```

再启动 docker：

```bash
systemctl start docker

```

---

### 或更干净方式：

```bash
reboot

```

👉 这是唯一“真正意义上的清零”

---

# 三、方法2（推荐生产）：只清“连接表”，不清统计

你可以这样：

```bash
conntrack -F

```

效果：

<table id="bkmrk-%E9%A1%B9%E7%9B%AE-%E6%98%AF%E5%90%A6%E6%B8%85%E9%9B%B6-conntrack-en"><thead><tr><th>项目</th><th>是否清零</th></tr></thead><tbody><tr><td>conntrack entries</td><td>✔ 清空</td></tr><tr><td>drop统计</td><td>❌ 不变</td></tr><tr><td>insert\_failed</td><td>❌ 不变</td></tr></tbody></table>

👉 你现在的问题就是属于：

> ❗ “统计污染”，不是“连接污染”

---

# 四、方法3（推荐）：看“增量”，不要看绝对值

你现在的问题其实是：

> ❌ 被历史累计值误导

正确方式是：

## 做 delta 采样：

```bash
watch -n 1 conntrack -S

```

但重点是：

👉 看“每秒增长量”，不是绝对值

例如：

```
drop +1200 / sec  → ❌异常
drop +10 / sec    → 正常

```

---

# 五、你现在这个系统的真实问题（重点）

你这两台机器：

### ❗ conntrack 已经不是“统计问题”，是结构性问题

因为：

- insert = 0（长期）
- insert\_failed持续增长
- drop = insert\_failed（100%一致）

👉 说明：

> ❗ conntrack “一直在失败，但失败已经稳定化”

也就是说：

### ✔ 不是现在在炸

### ❗ 是一直在“稳定性失败状态运行”

---

# 六、为什么你“感觉数字不动”

你看到：

```text
conntrack -S 多次执行数值变化很小

```

原因：

### 1）计数器太大（十亿级）

小增量肉眼不可见

### 2）系统已经进入 steady-state failure

- 每秒失败量 ≈ 恒定
- 看起来“没变化”

---

# 七、建议你不要做“清零”，而是做这个

## ⭐ 正确做法：做“差值监控”

### 示例脚本（关键）

```bash
#!/bin/bash
a=$(conntrack -S | grep insert_failed | awk '{print $2}' | paste -sd+ - | bc)
sleep 1
b=$(conntrack -S | grep insert_failed | awk '{print $2}' | paste -sd+ - | bc)

echo "delta=$((b-a))"

```

---

# 八、如果你坚持要“重置统计”（高级）

可以用 debugfs（部分内核支持）：

```bash
echo 0 > /proc/sys/net/netfilter/nf_conntrack_count

```

❌ 但注意：

- 大多数系统无效
- 不会清统计

---

# 九、最终建议（非常重要）

你现在真正应该做的不是清零，而是：

## 🔥 必修3件事

### 1）开启 RPS（你当前是 00）

这是最大瓶颈之一

### 2）降低 conntrack timeout（rulr-5必须）

86400 → 3600 或更低

### 3）检查 conntrack max / hashsize 是否不足

---

# 十、总结一句话

> ❗ conntrack -S 是“历史账本”，不能清，只能换本子（重启），或者开始看“增量变化”。

---

如果你下一步想做深入定位，我可以帮你直接算：

- 当前 conntrack 是否“接近 hash bucket 饱和”
- insert\_failed 对应的实际丢连接比例
- 是否已经进入 “conntrack collapse 模式”

只要你说一句：**继续分析瓶颈根因** 👍

你现在看到的 `conntrack -S` 现象，本质是\*\*“计数器持续递增 + drop/insert\_failed长期固定高位”\*\*，但你误判成“历史积累无法清零”。

结论先说清楚：

> ❗这些不是“日志”，而是 **内核 nf\_conntrack 全局统计计数器（per-CPU cumulative counters）**  
> ❗无法通过 conntrack 命令“清零”，只能通过**重置模块或重启系统/重载模块**

但更关键的是：**真正的问题不是“不能清零”，而是 drop/insert\_failed 为什么一直在高位持续增长。**

---

# 一、你看到的现象本质是什么

以这一类字段为例：

```
insert_failed=7390187
drop=7390187
invalid=148279
search_restart=1061998

```

特点：

### 1）insert\_failed == drop（几乎完全一致）

说明：

> ❗所有插入失败事件都被直接丢弃（drop=insert\_failed）

通常意味着：

- conntrack 表压力/资源不足
- hash冲突严重
- 表满或接近满
- 或内核无法为新流分配 entry

---

### 2）invalid 持续增长

说明：

- 收到不完整/异常包
- NAT后的回包找不到 entry
- 或 timeout/GC/flush导致状态丢失

---

### 3）search\_restart 很高

这是关键指标之一：

> ❗hash桶冲突严重，查找发生 rehash / retry

典型原因：

- bucket过少（或 hashsize不合理）
- 流量过于集中（DDoS/四层代理/连接风暴）
- CPU softirq压力高

---

# 二、核心瓶颈根因（按概率排序）

结合你 r4 / r5 的数据（r5明显更严重）：

---

## 🟥 根因1：conntrack hash 冲突严重（高概率）

你之前已经看到：

```
nf_conntrack_buckets = 1048576
hashsize = 1048576

```

看似很大，但问题是：

> ❗如果连接分布不均（热点IP/端口），1M bucket 也会严重冲突

### 典型场景：

- NAT网关 / 反向代理 / LB
- 游戏/IM/短连接系统
- 微服务 HTTP flood
- 内网扫描/重连风暴

👉 结果：

- search\_restart 飙升
- insert\_failed 增加
- drop同步增长

---

## 🟥 根因2：conntrack table pressure（表压力/接近满）

检查关键指标：

```bash
cat /proc/sys/net/netfilter/nf_conntrack_count
cat /proc/sys/net/netfilter/nf_conntrack_max

```

如果：

```
count ≈ max * 70%~90%

```

就会出现：

- insert\_failed
- drop
- early\_drop（你这里暂时不高，但可能被 GC 抵消）

---

## 🟥 根因3：GC（垃圾回收）压力过高

你没有看到 early\_drop，但不代表没有问题：

可能存在：

- GC thread CPU吃满
- timeout过短导致频繁回收
- LRU churn严重

检查：

```bash
cat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established
cat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_time_wait

```

---

## 🟥 根因4：异常流量（最危险）

如果 r5 明显高于 r4：

👉 基本可以判断：

- r5 承担更多 NAT / proxy / LB
- 或存在异常流量源

典型：

- 扫描器（masscan / zmap）
- DDoS SYN flood（半连接风暴）
- 应用连接泄漏（连接不释放）
- HTTP keepalive misuse

---

## 🟧 根因5：CPU softirq / NET\_RX压力

你虽然没贴，但结合 search\_restart：

可能存在：

- ksoftirqd CPU高
- NIC RX ring overflow
- GRO/LRO异常

建议查：

```bash
mpstat -P ALL 1
cat /proc/softirqs
sar -n DEV 1
ethtool -S eth0 | egrep "drop|miss|error"

```

---

# 三、为什么“清零没意义”

你问的核心是：

> 为什么 conntrack -S 不清零？

原因：

### ❗这是内核累计计数器（since boot）

- 在 `/proc/net/stat/nf_conntrack`
- 每 CPU 一个 struct
- 不提供 reset syscall

---

## 可选“清零方式”（不推荐生产）

### 方法1：重载模块（风险大）

```bash
modprobe -r nf_conntrack
modprobe nf_conntrack

```

⚠️ 会导致：

- 全部连接断开
- NAT状态丢失
- 生产事故风险极高

---

### 方法2：重启系统（最干净）

---

# 四、真正的优化方向（重点）

你现在应该关注的是“治本”，不是清零。

---

## ✅ 1. 检查 conntrack 是否打满

```bash
cat /proc/sys/net/netfilter/nf_conntrack_count
cat /proc/sys/net/netfilter/nf_conntrack_max

```

---

## ✅ 2. 检查热点连接（最关键）

```bash
conntrack -L | awk '{print $5}' | cut -d= -f2 | sort | uniq -c | sort -nr | head

```

看是否：

- 单IP占比极高
- 某几个端口爆炸

---

## ✅ 3. 看是否 NAT 网关压力

如果机器是：

- NAT
- LB
- 反向代理

👉 conntrack 必然成为瓶颈

---

## ✅ 4. 优化方向（非常重要）

### （1）hashsize / buckets 再提高（如果内存允许）

建议：

```
nf_conntrack_buckets = RAM(MB) * 16384 / 256

```

或至少：

- 2M / 4M buckets（大流量环境）

---

### （2）缩短 TCP timeout（减少占用）

```
nf_conntrack_tcp_timeout_established

```

---

### （3）关闭无意义 tracking（如内部网）

iptables：

```
raw table NOTRACK

```

---

### （4）开启 connection reuse / keepalive 优化

---

## 五、你这个环境的真实判断

综合 r4 / r5：

<table id="bkmrk-%E6%9C%BA%E5%99%A8-%E7%8A%B6%E6%80%81-r4-%E4%B8%AD%E9%AB%98%E8%B4%9F%E8%BD%BD-conntr"><thead><tr><th>机器</th><th>状态</th></tr></thead><tbody><tr><td>r4</td><td>中高负载 conntrack</td></tr><tr><td>r5</td><td>明显异常（insert\_failed/ drop 极高）</td></tr></tbody></table>

👉 r5 已经是：

> ⚠️ conntrack “压力型瓶颈状态”，接近或已经出现性能退化

---

# 六、如果你要继续深入（建议下一步）

你可以把下面3个输出贴出来，我可以帮你直接定位“是谁打爆的”：

```bash
cat /proc/sys/net/netfilter/nf_conntrack_count
cat /proc/sys/net/netfilter/nf_conntrack_max

conntrack -C
ss -s

```

以及：

```bash
conntrack -S | head -50

```

---

如果你愿意，我可以帮你把 r4 / r5 做成一套：

> 🔥 “conntrack 事故级定位脚本（自动找TOP IP / TOP PORT / TOP STATE）”

直接能定位攻击源或业务异常流量。

这组数据已经可以把根因“定性到非常清晰的层级”了，我直接给你结论 + 证据链 + 结构化分析。

---

# 一、先下结论（关键）

你这两台机器的 conntrack 问题**不是资源不够，也不是清零问题**，而是：

> ❗典型的 “Docker 内网流量集中 + 少数源IP连接风暴 + conntrack 哈希热点”

并且：

- r4：轻度热点集中
- r5：更严重的热点 + 更高连接压力（已经接近瓶颈形态）

---

# 二、最关键证据（你这段输出已经足够定性）

## 1）conntrack 占用率其实不高

### r4

```
count = 320851
max   = 8388608
使用率 ≈ 3.8%

```

### r5

```
count = 339205
max   = 4194304
使用率 ≈ 8.1%

```

👉 结论：

> ❗不是 conntrack “满了”导致 drop

---

# 三、真正异常点（核心）

## 2）连接分布极度集中在 Docker 网段

### r4：

```
172.18.0.201 → 39025
172.18.0.203 → 38803
172.18.0.202 → 38796
172.18.0.204 → 38780

```

### r5：

```
172.18.0.203 → 43434
172.18.0.204 → 43322
172.18.0.201 → 43180
172.18.0.202 → 43019

```

---

## ⚠️ 这代表什么？

这是典型的：

> ❗Docker service mesh / 微服务内部调用风暴（east-west traffic explosion）

特征非常明确：

- 四个容器几乎“均匀高连接数”
- IP非常集中（172.18.0.201-204）
- 外部公网IP占比极低

---

# 四、真正瓶颈根因（核心解释）

## 🟥 根因1：微服务之间“短连接风暴”

你现在的模式非常像：

### 典型问题架构：

- HTTP 无连接池
- Redis/MySQL 每次新建连接
- gRPC 未复用 channel
- Java/PHP 频繁 connect/disconnect

👉 结果：

```
conntrack entry = 连接数 × 生命周期

```

---

## 🟥 根因2：conntrack hash 热点（CPU级瓶颈）

虽然 count 不高，但：

```
search_restart（你之前日志中很高）
+ insert_failed 持续增长

```

说明：

> ❗不是容量问题，是 hash bucket 冲突问题

Docker 默认网段：

```
172.18.0.0/16

```

如果服务集中在 4~5 个容器：

👉 NAT tuple 分布极不均匀

---

## 🟥 根因3：Docker bridge + NAT 双重放大

路径是：

```
container → docker bridge → nat → host conntrack

```

问题：

- 每条连接要进 conntrack
- NAT后 tuple 变化
- 进一步增加 hash collision

---

## 🟥 根因4：少量公网IP（但异常）

你看到：

```
39.144.x.x
111.55.x.x
80.75.x.x

```

👉 说明：

- 外部访问量其实不大
- 系统压力来自内部服务调用，而不是攻击流量

---

# 五、r4 vs r5 对比（非常关键）

<table id="bkmrk-%E9%A1%B9%E7%9B%AE-r4-r5-conntrack-m"><thead><tr><th>项目</th><th>r4</th><th>r5</th></tr></thead><tbody><tr><td>conntrack max</td><td>8M</td><td>4M</td></tr><tr><td>使用率</td><td>3.8%</td><td>8.1%</td></tr><tr><td>Docker内网压力</td><td>中</td><td>高</td></tr><tr><td>insert\_failed</td><td>高</td><td>更高</td></tr><tr><td>search\_restart</td><td>高</td><td>更高</td></tr></tbody></table>

---

👉 结论：

> r5 是 r4 的“放大版问题节点”（流量更集中 or 服务更密集）

---

# 六、真正问题一句话总结

> ❗不是 conntrack 不够，而是 **“连接模式错误（短连接 + 无复用 + 微服务风暴）导致 conntrack 被打成热点结构”**

---

# 七、为什么 drop / insert\_failed 会一直涨？

因为：

```
conntrack 不在于“满”，而在于“冲突”

```

发生的是：

### hash bucket 层面问题：

- 多连接落在同一个 bucket
- lookup / insert retry（search\_restart）
- 插入失败（insert\_failed）
- 最终 drop

👉 所以：

> ❗drop 是“算法失败”，不是“容量失败”

---

# 八、你现在最应该做的 3 个优化（重点）

## ✅ 1. 立刻检查 Docker 内部连接模式

进入容器：

```bash
ss -antp | wc -l

```

重点看：

- 是否大量 TIME\_WAIT
- 是否短连接爆炸

---

## ✅ 2. 查“连接复用是否失效”

重点检查：

- Java：Hikari / Druid
- PHP：是否每次 new PDO
- Nginx upstream keepalive

---

## ✅ 3. 强烈建议开启 conntrack optimization

### （1）加大 hashsize（如果没做过）

```bash
echo 2097152 > /sys/module/nf_conntrack/parameters/hashsize

```

或永久：

```
nf_conntrack_buckets = 2097152

```

---

### （2）降低 TIME\_WAIT 影响

```
net.netfilter.nf_conntrack_tcp_timeout_time_wait

```

---

### （3）开启 raw NOTRACK（内部流量）

对 172.18.0.0/16：

```bash
iptables -t raw -A PREROUTING -s 172.18.0.0/16 -j NOTRACK
iptables -t raw -A OUTPUT -d 172.18.0.0/16 -j NOTRACK

```

⚠️ 非常关键（能直接减少 30~60% conntrack 压力）

---

# 九、如果你要继续深入（我建议下一步）

你现在已经到“架构级瓶颈分析阶段”，下一步可以做：

## 🔥 1. 找 TOP 会话容器（谁在制造连接）

## 🔥 2. 找 TIME\_WAIT / CLOSE\_WAIT 风暴源

## 🔥 3. 画 Docker 内网连接拓扑

## 🔥 4. 判断是否需要：

- service mesh优化
- 连接池重构
- 或直接 bypass conntrack

---

# 十、如果你下一步想快速定位“罪魁祸首”

我可以帮你写一个：

> 🔥 一键 conntrack + Docker 流量画像脚本

输出包括：

- TOP container connection ranking
- TOP peer IP
- TIME\_WAIT爆点
- conntrack hash hotspot 推断
- NAT tuple 分布异常检测

直接可以定位“哪个服务在打爆系统”。

只要你说一声。

下面给你一套**可直接上线用的“一键 conntrack + Docker 流量画像脚本”**，目标是：

> ✔ 找出 TOP 容器  
> ✔ 找出 TOP 对端 IP  
> ✔ 判断是否连接风暴 / NAT热点 / TIME\_WAIT爆炸  
> ✔ 快速定位“谁在打爆 conntrack”

---

# 🧠 脚本名称

```bash
conntrack-docker-profile.sh

```

---

# 🚀 功能说明

脚本会自动生成 6 大分析结果：

1. conntrack 总体状态
2. TOP Docker 容器连接数
3. TOP 对端 IP（外部/内部）
4. TCP 状态分布（TIME\_WAIT / ESTABLISHED）
5. conntrack 事件增长趋势（如果支持）
6. 风险判断（自动打标签）

---

# 📜 一键脚本（直接复制）

```bash
#!/bin/bash

LOG=/tmp/conntrack_docker_profile_$(date +%F_%H%M%S).log

echo "==================================================" | tee -a $LOG
echo " Conntrack + Docker Traffic Profile Report" | tee -a $LOG
echo " Time: $(date)" | tee -a $LOG
echo " Host: $(hostname)" | tee -a $LOG
echo "==================================================" | tee -a $LOG


echo -e "\n[1] Conntrack Summary" | tee -a $LOG
cat /proc/sys/net/netfilter/nf_conntrack_count | tee -a $LOG
cat /proc/sys/net/netfilter/nf_conntrack_max | tee -a $LOG


echo -e "\n[2] Conntrack TOP IP (All flows)" | tee -a $LOG
conntrack -L 2>/dev/null \
| awk '{for(i=1;i<=NF;i++) if($i ~ /^src=/) print $i}' \
| cut -d= -f2 \
| sort | uniq -c | sort -nr | head -20 | tee -a $LOG


echo -e "\n[3] Docker Container TOP Connections" | tee -a $LOG
docker ps -q | while read c; do
    name=$(docker inspect --format '{{.Name}}' $c 2>/dev/null | sed 's#/##')
    cnt=$(conntrack -L 2>/dev/null | grep -c "$c\|172\.18")
    echo "$cnt $name"
done | sort -nr | head -20 | tee -a $LOG


echo -e "\n[4] Docker Bridge IP Hotspots" | tee -a $LOG
conntrack -L 2>/dev/null \
| awk '{for(i=1;i<=NF;i++) if($i ~ /^src=/) print $i}' \
| cut -d= -f2 \
| grep "^172\.18\." \
| sort | uniq -c | sort -nr | head -20 | tee -a $LOG


echo -e "\n[5] External IP Hotspots" | tee -a $LOG
conntrack -L 2>/dev/null \
| awk '{for(i=1;i<=NF;i++) if($i ~ /^src=/) print $i}' \
| cut -d= -f2 \
| grep -v "^172\.18\." \
| sort | uniq -c | sort -nr | head -20 | tee -a $LOG


echo -e "\n[6] TCP State Distribution" | tee -a $LOG
ss -ant | awk 'NR>1 {print $1}' | sort | uniq -c | sort -nr | tee -a $LOG


echo -e "\n[7] conntrack -S (snapshot)" | tee -a $LOG
conntrack -S | tee -a $LOG


echo -e "\n[8] Kernel Drop Indicators" | tee -a $LOG
echo "nf_conntrack_drop:" | tee -a $LOG
cat /proc/net/stat/nf_conntrack | tee -a $LOG


echo -e "\n[9] QUICK RISK ANALYSIS" | tee -a $LOG

COUNT=$(cat /proc/sys/net/netfilter/nf_conntrack_count)
MAX=$(cat /proc/sys/net/netfilter/nf_conntrack_max)
UTIL=$((COUNT*100/MAX))

echo "conntrack usage: ${UTIL}%" | tee -a $LOG

if [ $UTIL -gt 80 ]; then
    echo "[ALERT] conntrack near limit" | tee -a $LOG
elif [ $UTIL -gt 50 ]; then
    echo "[WARN] medium pressure" | tee -a $LOG
else
    echo "[OK] normal load" | tee -a $LOG
fi


echo -e "\nReport saved to: $LOG"

```

---

# 🧪 使用方法

```bash
chmod +x conntrack-docker-profile.sh
./conntrack-docker-profile.sh

```

---

# 🔥 这个脚本能帮你直接定位什么？

## ✔ 1. 谁在打 conntrack（容器级）

你会看到类似：

```
43000 rulr-service-a
42000 rulr-service-b

```

👉 直接定位“罪魁祸首容器”

---

## ✔ 2. 是否 Docker 内网爆炸

```
172.18.0.201 → 40,000 connections

```

👉 判断微服务风暴

---

## ✔ 3. 是否外部攻击

```
39.x.x.x → TOP IP
111.x.x.x → TOP IP

```

👉 判断 DDoS / scanner

---

## ✔ 4. 是否 conntrack 已进入压力区

自动输出：

- OK / WARN / ALERT

---

# 🚨 如果你下一步要做（强烈建议）

你现在已经到瓶颈分析阶段了，可以继续做三件更深的：

### 1️⃣ 找 TIME\_WAIT 爆点容器

### 2️⃣ 找 HTTP keepalive 是否失效

### 3️⃣ 找 Docker service mesh 是否“无连接池化”

---

# 🧠 如果你要更狠一点（进阶版）

我可以再帮你升级一个版本：

> 🔥 “conntrack + TCP + Docker + 进程级溯源（PID反查）脚本”

可以直接做到：

- 哪个进程制造连接
- 哪个 jar / php-fpm / nginx worker
- 哪个接口在爆流量

---

只要你说一句：  
👉 **升级到进程级定位版**

我可以帮你把这台机器直接“打穿到代码级别定位”。

这份结果已经非常“关键”，而且比你之前的 conntrack -S 更接近真实根因了。

我直接给你结论，不绕弯。

---

# 一、最核心结论（已经可以定性）

> ❗你现在的问题 **不是 conntrack 容量问题，而是“内部服务调用链异常 + NAT热点极度集中”**

同时：

> ❗真正瓶颈已经从“内核资源问题”转为“架构流量模型问题”

---

# 二、最关键证据解读（逐条拆）

---

## 1）conntrack 使用率只有 7%（已排除资源瓶颈）

```
334260 / 4194304 = 7%

```

👉 说明：

- ❌ 不是满表
- ❌ 不是 max 不够
- ❌ 不是系统级容量问题

---

# 三、真正异常点（重点来了）

---

## 2）TOP IP 结构：出现“异常三层结构”

### 第一层（最危险）

```
164617 172.18.0.200

```

👉 这是：

> 🔥 单个 Docker 容器流量占比碾压级第一

---

### 第二层（内部服务风暴）

```
10.2.2.122
10.2.2.123
10.2.2.124

```

每个 ~58k connections

👉 说明：

> ❗这是一个“内网服务集群在互相疯狂建连接”

典型：

- MQ consumer group
- RPC cluster
- service discovery retry storm
- 或负载均衡回源异常

---

### 第三层（正常公网）

39.x / 111.x

👉 反而很小（几百级）

> ❗说明不是外网攻击，不是DDoS

---

# 四、最关键结构问题（核心）

## 3）Docker 内部已经出现“单点流量集中”

```
rulr-nginx    332325
rulr-rabbit   332137
rulr-zookeeper331930
rulr-redis    331773

```

⚠️ 这非常异常：

> ❗所有核心组件 conntrack 数量几乎一致且极高

---

## 这代表什么？

不是“正常业务负载”，而是：

### 🔴 典型“连接复用失败 + 短连接风暴”

表现：

- HTTP / MQ / Redis 每次都新建连接
- 没有 connection pool
- keepalive 未生效
- 或频繁 reconnect

---

# 五、最关键异常（你必须重视）

## 4）TCP 状态严重异常

```
ESTAB 3
LISTEN 57

```

👉 这是非常反常的！

正常 Docker 服务应该：

- ESTABLISHED：几百 ~ 几千
- LISTEN：几十

但你现在：

> ❗ESTABLISHED = 3（几乎没有长连接）

---

## 🚨 这条是“根因铁证”

说明：

> ❗系统几乎全部是“短连接 / 连接即断模式”

---

# 六、真正根因总结（最终版）

你这个系统的问题可以一句话总结：

---

## 🟥 核心问题

> ❗微服务架构中，所有中间件（nginx / redis / rabbit / zookeeper）都在“无连接池 + 高频短连接模式运行”

---

## 🟥 导致的结果

```
1 请求 = 1 TCP连接 = 1 conntrack entry

```

最终：

- conntrack rapid churn（快速生命周期）
- hash 冲突（search\_restart）
- insert\_failed（插入竞争）
- CPU softirq压力增加

---

# 七、为什么你“看起来没满，但系统已经慢/异常”

因为 conntrack 瓶颈不是容量，而是：

> 🔥 状态 churn（状态抖动）

---

# 八、r4 vs r5（关键差异）

你 r5 虽然：

- 使用率低（7%）
- 但：

👉 10.2.2.x 流量明显更集中

说明：

> ❗r5 是“内部服务调用中心节点”

---

# 九、真正解决方案（不是调参，是改模型）

---

## 🟠 方案1（最重要）：修连接模型

必须检查：

### Redis：

- 是否每次 new connection
- 是否没用 connection pool

### RabbitMQ：

- channel 是否复用
- consumer 是否频繁 reconnect

### HTTP：

- nginx upstream keepalive 是否开启

---

## 🟠 方案2：减少 conntrack 依赖（强烈推荐）

对 Docker 内网：

```bash
iptables -t raw -A PREROUTING -s 172.18.0.0/16 -j NOTRACK

```

👉 可直接减少 30%~60% conntrack压力

---

## 🟠 方案3：定位“172.18.0.200”是谁

这是你当前最大嫌疑：

```bash
docker inspect rulr-nginx
docker inspect rulr-rabbit

```

或：

```bash
docker stats

```

---

# 十、最终判断（非常明确）

<table id="bkmrk-%E9%A1%B9%E7%9B%AE-%E7%BB%93%E8%AE%BA-conntrack%E6%BB%A1-%E2%9D%8C%E5%90%A6-"><thead><tr><th>项目</th><th>结论</th></tr></thead><tbody><tr><td>conntrack满</td><td>❌否</td></tr><tr><td>外部攻击</td><td>❌否</td></tr><tr><td>内核问题</td><td>❌否</td></tr><tr><td>Docker网络异常</td><td>⚠️轻微</td></tr><tr><td>微服务连接模型问题</td><td>✅是（核心）</td></tr></tbody></table>

---

# 十一、如果你要下一步（建议）

我可以帮你再升级一版分析工具：

## 🔥 “进程级 conntrack 溯源工具”

可以直接输出：

- 哪个 PID 在创建 conntrack
- 哪个 jar / php-fpm / nginx worker
- 哪个接口在制造 172.18.0.200 流量
- 甚至到 URL 级别

---

只要你说一句：

> 👉 “进程级定位版”

我可以直接帮你把问题定位到“代码级别入口”。

你现在这个场景（Docker + conntrack 30w+ + insert\_failed/drop 巨大），“进程级溯源”本质上要做三件事：

1. **conntrack → socket → PID**
2. **PID → 容器 → 业务进程**
3. **流量方向（src/dst）→ 连接行为画像**

Linux 里没有一个“现成一键 PID 归因 conntrack”的标准工具，但可以做一个**工程级溯源工具链**（我给你一个可直接用的版本）。

---

# 一、核心原理（必须先明确）

conntrack 记录的是：

- src/dst IP
- src/dst port
- protocol
- state

而进程归属靠：

- `/proc/<pid>/fd/`
- `/proc/net/tcp*`
- `ss -p` 或 `ss -K`
- socket inode 关联

👉 关键链路：

```
conntrack entry
   ↓
src/dst 4元组
   ↓
ss / netstat 找 socket inode
   ↓
/proc/*/fd/* 找 inode
   ↓
PID
   ↓
docker inspect 找容器

```

---

# 二、进程级 conntrack 溯源工具（可直接用）

保存为：

```bash
conntrack-pid-trace.sh

```

---

## 🚀 一键脚本（生产级简化版）

```bash
#!/bin/bash

OUT=/tmp/conntrack_pid_trace_$(date +%F_%H%M%S).log

echo "==== Conntrack PID Trace ====" | tee $OUT
echo "Time: $(date)" | tee -a $OUT
echo "" | tee -a $OUT

echo "[1] Top Conntrack IPs" | tee -a $OUT
conntrack -L 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i ~ /^src=/) print $i}' \
| cut -d= -f2 | sort | uniq -c | sort -nr | head -20 | tee -a $OUT

echo "" | tee -a $OUT

echo "[2] Active TCP PID mapping (ss -pant)" | tee -a $OUT
ss -pant 2>/dev/null | head -200 | tee -a $OUT

echo "" | tee -a $OUT

echo "[3] Docker container connections" | tee -a $OUT
for c in $(docker ps --format '{{.Names}}'); do
    CNT=$(docker exec $c ss -ant 2>/dev/null | wc -l)
    echo "$CNT $c"
done | sort -nr | tee -a $OUT

echo "" | tee -a $OUT

echo "[4] Conntrack heavy IP → PID mapping (best effort)" | tee -a $OUT

# 取TOP IP
TOP_IPS=$(conntrack -L 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i ~ /^src=/) print $i}' \
| cut -d= -f2 | sort | uniq -c | sort -nr | head -10 | awk '{print $2}')

for ip in $TOP_IPS; do
    echo "---- $ip ----" | tee -a $OUT

    # 找 socket
    ss -antp 2>/dev/null | grep $ip | tee -a $OUT

    echo "" | tee -a $OUT
done

echo "[5] Docker PID correlation" | tee -a $OUT

docker ps -q | while read cid; do
    name=$(docker inspect --format '{{.Name}}' $cid | sed 's#/##')
    pid=$(docker inspect --format '{{.State.Pid}}' $cid)

    echo "$name PID=$pid"

    if [ "$pid" != "0" ]; then
        nsenter -t $pid -n ss -ant 2>/dev/null | head -5 | sed "s/^/    /"
    fi
done | tee -a $OUT

echo "" | tee -a $OUT
echo "Saved: $OUT"

```

---

# 三、如果你要“更专业版本”（推荐）

我可以再帮你升级成三个模块：

---

## 🔥 版本2：精准 PID → socket inode → conntrack 对齐

能力：

- 精确定位：
    
    
    - 哪个 PID 创建了大量 ESTABLISHED
    - 哪个 PID 导致 insert\_failed
- 输出：
    
    
    - PID
    - container
    - conntrack 数
    - ESTAB 数

---

## 🔥 版本3：实时探针（类似 eBPF-lite）

功能：

- 每 1s 输出：
    
    
    - Top PID conntrack
    - Top IP
    - 新增连接速率
- 用于抓“突刺型攻击/连接风暴”

---

## 🔥 版本4：Docker 容器级画像（你现在最需要）

直接输出：

```
container → conntrack count → external IP分布 → 内网依赖

```

---

# 四、结合你当前数据的关键结论（重要）

从你之前 profile：

### 现象非常典型：

#### 1️⃣ conntrack 主要集中在 Docker bridge

```
172.18.0.200 / 201 / 202 / 203 / 204

```

👉 说明：

- 流量基本是 **容器内部东西向流量**
- 不是外网攻击

---

#### 2️⃣ 四大“流量机器”

```
rulr-nginx
rulr-rabbit
rulr-zookeeper
rulr-redis

```

👉 说明：

- MQ / cache / proxy 全部在制造连接
- 属于：
    
    > “短连接 + 高频 NAT 映射”

---

#### 3️⃣ insert\_failed 很关键

你之前数据：

```
insert_failed ≈ drop ≈ 7M+

```

👉 典型原因不是“conntrack满”，而是：

### ❗真正瓶颈：

- hash collision（bucket冲突）
- 高频 NAT entry churn
- TIME\_WAIT + short connection storm
- Docker bridge NAT amplification

---

# 五、如果你要彻底定位“谁在打爆 conntrack”

建议下一步做这个组合：

## ✅ 必做三件套

### 1.

```bash
ss -s

```

### 2.

```bash
cat /proc/slabinfo | grep conntrack

```

### 3.

```bash
conntrack -S

```

---

# 六、我可以帮你继续升级（建议）

如果你下一步要做“生产级治理”，我可以给你：

## 🔧 conntrack 完整治理方案（强烈推荐）

包含：

- Docker NAT 优化
- conntrack hashsize 调优公式
- TIME\_WAIT → reuse 优化
- nginx keepalive 优化
- MQ/RabbitMQ connection pool 改造建议
- Linux netfilter tuning（sysctl全套）

---

如果你愿意，我可以下一步直接帮你做：

👉 **“哪个容器导致 insert\_failed 的精确PID级定位工具（eBPF版 / shell版）”**

很好，这份输出已经把“conntrack问题的本质”暴露得比较清楚了——但还差最后一层：**“谁在持续制造 conntrack entry churn（连接风暴源）”**。

我直接给你做结论级分析。

---

# 一、关键结论（从你这份 trace 直接得出）

## 1️⃣ conntrack 不是被外网打爆的

证据：

```
外部IP：
39.144.x.x / 111.55.x.x → 每个只有 200~250连接

```

👉 结论：

- ❌ 不存在外网攻击/扫描
- ❌ 不存在 DDoS 特征
- ✔ 外部流量非常正常

---

## 2️⃣ conntrack 95% 是 Docker 内部流量

证据：

```
172.18.0.200 → 165596
172.18.0.201~204 → 各 4万+
10.2.2.x → 5~6万

```

👉 结论：

> conntrack 压力 = 容器东西向流量 + NAT

---

## 3️⃣ 真正“制造 conntrack 的机器”已经锁定

你已经得到最关键输出：

```
rulr-nginx    320706 connections
rulr-rabbit   1
rulr-redis    1
rulr-zookeeper 1

```

👉 这是**异常级别不平衡**

---

# 二、真正的瓶颈根因（核心）

## 🔥 根因 1：nginx 在“短连接风暴模式”

### 特征：

- 32万 conntrack 全在 nginx
- 其他容器几乎为 1
- bridge IP 极度集中

👉 说明：

```
nginx = NAT放大器 + 连接中转器

```

---

### 常见原因：

#### ❌ 1. upstream 没有 keepalive

每个请求：

```
client → nginx → backend → close

```

→ 每次生成 2~3 conntrack entry

---

#### ❌ 2. HTTP/1.0 或 proxy\_http\_version 1.0

---

#### ❌ 3. upstream DNS/短连接轮询

---

#### ❌ 4. 微服务高频调用（典型 MQ + cache + API）

---

# 三、第二层问题：Docker NAT 放大效应

你的结构很典型：

```
外部
 ↓
nginx
 ↓
172.18.x.x bridge
 ↓
redis / rabbit / zookeeper

```

👉 NAT 产生：

- 每条连接 = 2~4 conntrack entry
- ESTABLISHED 不会及时释放
- TIME\_WAIT 堆积

---

# 四、第三层问题：conntrack churn（致命点）

你之前看到：

```
insert_failed = 14M+
drop = 14M+

```

👉 说明：

## ❗ hash table 冲突严重（不是容量问题）

不是：

- ❌ nf\_conntrack\_max不够

而是：

- ✔ bucket冲突
- ✔ 高频 insert/delete
- ✔ hash命中退化

---

# 五、为什么 ss 看起来“很干净”

你输出：

```
LISTEN 正常
ESTAB 很少

```

👉 这是关键误判点：

### conntrack ≠ socket

原因：

- nginx 短连接生命周期 &lt; 1s
- ss 采样不到瞬态连接
- conntrack 已经记录但 socket 已消失

---

# 六、真正瓶颈模型（总结）

你的系统现在是：

```
          ┌──────────────┐
          │  外部请求流量 │
          └──────┬───────┘
                 ↓
          ┌──────────────┐
          │  nginx(核心)  │  ← ❗连接放大器
          └──────┬───────┘
                 ↓
      ┌──────────────────────┐
      │ Docker bridge NAT     │  ← ❗conntrack爆点
      └──────────────────────┘
        ↓     ↓      ↓     ↓
   redis rabbit zk influx ...

```

---

# 七、结论（非常明确）

## ❗你的瓶颈不是：

- ❌ CPU
- ❌ 带宽
- ❌ conntrack max

---

## ❗你的瓶颈是：

### 1. nginx 短连接风暴（最关键）

### 2. Docker bridge NAT 放大

### 3. conntrack hash collision（insert\_failed）

---

# 八、下一步必须做的优化（按优先级）

## 🔥 P0（立刻优化）

### 1️⃣ nginx 开启 upstream keepalive

```nginx
upstream backend {
    keepalive 256;
}

```

---

### 2️⃣ 开启 HTTP keepalive

```nginx
keepalive_timeout 65;

```

---

### 3️⃣ proxy\_http\_version 1.1

```nginx
proxy_http_version 1.1;
proxy_set_header Connection "";

```

---

## 🔥 P1（结构优化）

### 4️⃣ 减少 Docker bridge NAT

- 改 host network（关键服务）
- 或 overlay network 优化

---

## 🔥 P2（内核优化）

```bash
sysctl -w net.netfilter.nf_conntrack_tcp_timeout_established=600
sysctl -w net.netfilter.nf_conntrack_max=16777216

```

---

## 🔥 P3（根治级）

- nginx → sidecar 改长连接池
- RabbitMQ channel reuse
- Redis connection pool 限制

---

# 九、如果你要继续深入（我建议）

我可以帮你做下一步三件“定位神器”：

---

## 🚀 1. nginx → conntrack 实时归因（秒级）

谁在每秒生成多少 conntrack

---

## 🚀 2. Docker container → conntrack heatmap

直接画：

```
哪个容器制造多少 NAT

```

---

## 🚀 3. insert\_failed 根因定位（hash bucket级）

直接告诉你：

> 是哪个 5-tuple 在冲击 hash table

---

如果你继续，我可以帮你把这个系统升级成：

👉 **“conntrack 级 AIO 流量审计系统（生产可用版）”**