MikroTik ax3 部署 AmneziaWG 隧道连接 US VPS
日期:2026-05-29 ~ 2026-05-30
操作人:Hermes AI + user
状态:✅ 已完成
目标
在 hAP ax3(ROS 7.23, arm64)上用 Docker 容器跑 AmneziaWG 客户端,通过 AWG 2.0 混淆隧道连接 US VPS(38.64.61.213:51000),实现翻墙出口。
最终架构
局域网设备 → ax3 路由 → 容器(192.168.88.6) → AWG隧道 → US VPS(10.9.9.1) → 互联网
原生 AWG 2.0
- ax3 容器:
wiktorbgu/amneziawg-mikrotik:latest(arm64) - VETH:
192.168.88.6/24,网关192.168.88.1(桥) - AWG IP:
10.9.9.5 - 协议:AWG 2.0(Jc/Jmin/Jmax/S1-S4/H1-H4/I1 全混淆参数)
关键踩坑
| 坑 | 原因 | 解决 |
|---|---|---|
ROS 7.16 mounts= 报 bad parameter |
RouterOS Bug | 配置烧进镜像 或 用 /file/set 预置配置 |
| 容器每重建换新密钥 | fresh root-dir 生成新密钥对 | 记录公钥 → 更新 VPS peer AllowedIPs |
| ROS 7.23 容器启动报 "no command" | 新版强制显式 cmd= |
加 cmd="sleep infinity" |
| VPS 收到包但不回复 | 回包走错网口(ens3 而非 awg0) | ip route add 192.168.88.0/24 dev awg0 |
| VPS 不转发到外网 | 缺 FORWARD + NAT 规则 | iptables FORWARD + MASQUERADE |
| VPS 丢弃 ax3 来的包 | rp_filter 拦截 | sysctl -w net.ipv4.conf.awg0.rp_filter=0 |
| 容器主动发 TCP 不通(iperf3 -c) | amneziawg-go 用户态 TUN bug | 翻墙走内核转发,不受影响 |
| PostUp 接口名 | ROS <7.20 用 eth0, ≥7.20 用 veth 名 | 当前用 iif veth-awg |
部署流程(速查)
# 1) US VPS 生成客户端
ssh root@38.64.61.213
bash /root/awg/manage_amneziawg.sh add ax3
cat /root/awg/ax3.conf # 复制内容
# 2) 传配置到 ax3
scp ax3.conf xf@192.168.29.125:
# 3) ax3 建容器
/interface/veth/add address=192.168.88.6/24 gateway=192.168.88.1 name=veth-awg
/interface/bridge/port/add bridge=bridge interface=veth-awg
/container/add file=usb1/tar/awg-native-arm64.tar interface=veth-awg \
root-dir=usb1/docker/awg-v8 start-on-boot=yes logging=yes \
dns=1.1.1.1,8.8.8.8 cmd="sleep infinity"
/container/start [find where interface=veth-awg]
# 4) 预置配置
/file/set ax3.conf name=usb1/docker/awg-v8/etc/amnezia/amneziawg/awg.conf
# 5) VPS 端配置(必做)
ip route add 192.168.88.0/24 dev awg0
iptables -I FORWARD -i awg0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.88.0/24 -o ens3 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.9.9.0/24 -o ens3 -j MASQUERADE
sysctl -w net.ipv4.conf.awg0.rp_filter=0
# 6) ax3 加路由
/ip/route/add dst-address=10.9.9.0/24 gateway=192.168.88.6 comment=awg
/ping 10.9.9.1 # 应 0% 丢包 ~175ms
实测带宽
| 方向 | 平均 | 峰值 | 说明 |
|---|---|---|---|
| 下行 VPS→ax3 | ~40 Mbps | 88 Mbps | 看 4K YouTube 足够 |
| 上行 ax3→VPS | ~22 Mbps | 34 Mbps | amneziawg-go CPU 瓶颈 |
技能文件
详细文档已保存至 Hermes Agent 技能 ax3-awg-native-container。
参考:容器官方文档
来源:wiktorbgu/amneziawg-mikrotik - Docker Hub
基本信息
- 镜像:
wiktorbgu/amneziawg-mikrotik:latest - 大小: ~37MB(压缩)
- 架构: linux/arm64, linux/arm, linux/amd64
- 更新: 7天前(活跃维护)
- 功能: AmneziaWG 客户端 + 服务端,支持 AWG 2.0
MikroTik 配置(文档推荐)
/interface/bridge/add name=Bridge-Docker
/ip/address/add address=192.168.254.1/24 interface=Bridge-Docker
/interface/veth/add address=192.168.254.4/24 gateway=192.168.254.1 name=AMNEZIAWG
/interface/bridge/port/add bridge=Bridge-Docker interface=AMNEZIAWG
/container/mounts/add dst=/etc/amnezia/amneziawg name=amnezia_wg_conf src=/usb1/docker_configs/amnezia_wg_conf
/container/add remote-image=wiktorbgu/amneziawg-mikrotik interface=AMNEZIAWG root-dir=/usb1/docker/amneziawg start-on-boot=yes logging=yes mounts=amnezia_wg_conf dns=1.1.1.1,8.8.8.8,9.9.9.9
客户端配置示例
[Interface]
PrivateKey = <客户端私钥>
Address = 10.10.10.2/24
MTU = 1420
Jc = 6
Jmin = 50
Jmax = 1000
S1 = 0
S2 = 0
H1 = 1
H2 = 2
H3 = 3
H4 = 4
PostUp = iptables -t nat -A POSTROUTING -o %i -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o %i -j MASQUERADE
PostUp = iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
PostDown = iptables -D FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
Table = awg
PostUp = ip rule add priority 300 from all iif AMNEZIAWG lookup awg || true
PostDown = ip rule del from all iif AMNEZIAWG lookup awg || true
[Peer]
PublicKey = <服务端公钥>
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Endpoint = <VPS_IP>:<PORT>
注意事项
- AWG 协议参数:S1/S2/H1-H4 必须客户端和服务端完全一致,Jc/Jmin/Jmax 可独立调整
- ROS 版本:< 7.20 时 PostUp 用
iif eth0,>= 7.20 用iif VETH名 - 配置文件:必须用 LF(Unix)换行符,CRLF 不工作
- 日志:ARMv5 容器不输出日志到 MikroTik,用
awg命令检查状态 - 状态检查:
/container/shell <编号>进去后执行awg
其他推荐容器
wiktorbgu/amneziawg-proxy-mikrotik— AWG→WG 代理模式(C版 66KB,Go版 843KB)wiktorbgu/mihomo-mikrotik— 单向翻墙方案(不支持双向网络互通)
附录:完整技能文档 (ax3-awg-native-container)
以下内容来自 Hermes Agent 技能库,是本次部署的完整技术参考。
架构
VPS(AWG服务端) ←──AWG隧道──→ ax3 容器(arm64) ──VETH──→ RouterOS 桥
ROS 版本差异
| 项目 | ROS 7.16 | ROS 7.23 |
|---|---|---|
| cmd= | 可选 | 必传,否则 "no command specified" |
| mounts= | 完全失效 | 同样报错,不可靠 |
| 默认 ENTRYPOINT | 不生效 | cmd="" 也不触发 |
| PostUp iif | iif eth0 | iif veth-awg |
| /container/shell | 几乎不可用 | 可用 |
最简部署
# 1. VETH + 桥接
/interface/veth/add address=192.168.88.6/24 gateway=192.168.88.1 name=veth-awg
/interface/bridge/port/add bridge=bridge interface=veth-awg
# 2. 容器(cmd 必传)
/container/add file=usb1/tar/awg-native-arm64.tar interface=veth-awg \
root-dir=usb1/docker/awg-v8 start-on-boot=yes logging=yes \
dns=1.1.1.1,8.8.8.8 cmd="sleep infinity"
/container/start [find where interface=veth-awg]
# 3. 配置预置法
scp root@38.64.61.213:/root/awg/ax3.conf .
scp ax3.conf xf@192.168.29.125:ax3.conf
/file/set ax3.conf name=usb1/docker/awg-v8/etc/amnezia/amneziawg/awg.conf
# 4. 路由
/ip/route/add dst-address=10.9.9.0/24 gateway=192.168.88.6
VPS 端配置(必做)
# 生成客户端
bash /root/awg/manage_amneziawg.sh add ax3
# 更新 peer AllowedIPs
awg set awg0 peer <公钥> allowed-ips <IP>/32,192.168.88.0/24
# 路由+转发+NAT(缺一不可)
ip route add 192.168.88.0/24 dev awg0
iptables -I FORWARD -i awg0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.88.0/24 -o ens3 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.9.9.0/24 -o ens3 -j MASQUERADE
sysctl -w net.ipv4.conf.awg0.rp_filter=0
客户端配置模板
[Interface]
PrivateKey = <客户端私钥>
Address = 10.9.9.5/32
MTU = 1280
Jc = 3
Jmin = 75
Jmax = 204
S1 = 144
S2 = 106
S3 = 42
S4 = 5
H1 = 96251339-735378484
H2 = 1147003365-1312041290
H3 = 1380950216-1477896172
H4 = 1728712810-2128543926
I1 = <r 144>
Table = awg
PostUp = ip rule add priority 300 from all iif veth-awg lookup awg || true
PostDown = ip rule del from all iif veth-awg lookup awg || true
[Peer]
PublicKey = <VPS公钥>
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Endpoint = 38.64.61.213:51000
镜像制作(含 iperf3)
FROM wiktorbgu/amneziawg-mikrotik:latest
RUN apk add --no-cache iperf3
COPY awg.conf /etc/amnezia/amneziawg/awg.conf
docker buildx build --platform linux/arm64 -t awg-client:latest --load .
docker save awg-client:latest -o awg-client-arm64.tar
备份恢复后
- SSH 密钥会变 →
ssh-keygen -R 192.168.29.125 - 所有容器停止 → 需重建(ROS 7.23 要求 cmd=)
- VETH 接口保留
- 镜像文件保留
容器主动 TCP vs 内核转发
| 场景 | 通? | 原因 |
|---|---|---|
| 容器自己跑 iperf3 -c | 否 | amneziawg-go 用户态 TUN bug |
| LAN 设备经容器转发上网 | 是 | 内核路由转发 |
已知问题速查
| 问题 | 绕过方式 |
|---|---|
| mounts= 报 bad parameter | 配置预置法 |
| 容器重建换密钥 | 新 root-dir,更新 VPS peer |
| 无 awg 路由表 | echo "100 awg" >> rt_tables |
| default 路由不通外网 | 用 0.0.0.0/0 |
| VPS 收到包不到互联网 | FORWARD + MASQUERADE + rp_filter |
| 下行 40Mbps / 上行 22Mbps | arm64 CPU 瓶颈 |
| root-dir 复用报错 | 每次用新目录名 |
IP 迁移指南
更换 ax3 网段时(如从 192.168.88.x 换成正式网段),需更新三处:
ax3 侧
/interface/veth/set veth-awg address=<新IP>/24 gateway=<新桥IP>
/ip/route/set [find comment=awg] gateway=<新容器IP>
VPS 侧
ip route del 192.168.88.0/24 dev awg0
ip route add <新网段>/24 dev awg0
iptables -t nat -D POSTROUTING -s 192.168.88.0/24 -o ens3 -j MASQUERADE
iptables -t nat -A POSTROUTING -s <新网段>/24 -o ens3 -j MASQUERADE
awg set awg0 peer <公钥> allowed-ips <新容器IP>/32,<新网段>/24
Hermes 侧
ssh-keygen -R <旧IP>