RouterOS 详细部署与分流
192.168.1.1,CMSingBox 为 192.168.1.20,局域网为 192.168.1.0/24。/export file=before-cmsingbox,并保留 WinBox MAC 连接能力。不要直接粘贴与自己网段不一致的命令。一、部署前检查
- RouterOS v7,已安装与当前版本、架构一致的
container包。 - 设备架构为 amd64、arm64 或普通 armv7;镜像会从多架构清单自动匹配。
- 建议使用 USB / SATA / NVMe 外置存储,避免占满内置闪存。
- 路由器能够解析域名并访问 Docker Hub,系统时间正确。
/system/resource/print
/system/package/print where name=container
/disk/print
/ip/dns/print二、开启 Container
执行后按终端提示完成物理确认;x86/CHR 通常需要冷重启。远程操作前先确认能接触设备。
/system/device-mode/update container=yes
/system/reboot重连后执行 /system/device-mode/print,确认 container: yes。
三、创建容器网络
把 veth 加入现有 LAN bridge,使容器成为独立局域网设备。确认 192.168.1.20 位于 DHCP 池外且没有被占用。
/ping 192.168.1.20 count=3
/interface/veth/add name=veth-cmsingbox address=192.168.1.20/24 gateway=192.168.1.1
/interface/bridge/port/add bridge=bridge interface=veth-cmsingbox/interface/bridge/print。若 LAN bridge 叫 bridge-lan,请替换命令中的 bridge。四、拉取多架构镜像
以下以外置磁盘 disk1 为例。RouterOS 会从 Docker Hub 多架构清单选择本机版本。
/container/config/set registry-url=https://registry-1.docker.io tmpdir=disk1/tmp
/container/mounts/add list=cmsingbox-data src=disk1/cmsingbox-data dst=/data
/container/add remote-image=darkver8/cmsingbox:latest interface=veth-cmsingbox root-dir=disk1/cmsingbox-root mountlists=cmsingbox-data name=cmsingbox logging=yes start-on-boot=yes
/container/print detail等待下载、解压完成并显示 status=stopped 后启动:
/container/start [find where name="cmsingbox"]
/container/print detail
/log/print where topics~"container"打开 http://192.168.1.20:80,初始账号、密码均为 admin。先修改密码,再添加订阅、应用配置并启动 sing-box。
五、验证基础功能
| 服务 | 地址 | 验证 |
|---|---|---|
| 管理后台 | 192.168.1.20:80 | 浏览器登录 |
| 代理控制台 | 192.168.1.20:9090/ui/ | 启动 sing-box 后打开 |
| HTTP / SOCKS5 | 192.168.1.20:2080 | 终端手动填写代理 |
| DNS | 192.168.1.20:53 | 后台开启后测试 |
如果只使用显式 HTTP/SOCKS5 代理,到这里即可,不需要添加 ROS 分流规则。
六、旁路由分流原理
- DNS 分流:CMSingBox 将需要代理的域名解析为 FakeIP
198.18.0.0/15。 - 路由分流:RouterOS 把 FakeIP 和纯 IP 目标送至
192.168.1.20。
先在 CMSingBox“设置 → DNS”开启独立 DNS,监听 0.0.0.0:53;再在“设置 → 透明代理”启用透明代理,保存、应用配置并确认 sing-box 正常运行。
七、DNS 分流(二选一)
方式 A:DHCP 下发 CMSingBox DNS(推荐)
/ip/dhcp-server/network/print
/ip/dhcp-server/network/set [find where gateway="192.168.1.1"] dns-server=192.168.1.20终端需重新获取 DHCP 租约。只想代理部分设备时,可在 CMSingBox DNS 的例外设备列表中设置直连来源 IP。
方式 B:劫持指定设备 DNS
先把终端加入 proxy_device。绝不能加入 CMSingBox 的 192.168.1.20,否则形成环回。
/ip/firewall/address-list/add list=proxy_device address=192.168.1.100 comment="CMSingBox client"
/ip/firewall/nat/add chain=dstnat action=dst-nat protocol=udp dst-port=53 src-address-list=proxy_device dst-address=192.168.1.1 to-addresses=192.168.1.20 to-ports=53 comment="cmsingbox_dns"
/ip/firewall/nat/add chain=dstnat action=dst-nat protocol=tcp dst-port=53 src-address-list=proxy_device dst-address=192.168.1.1 to-addresses=192.168.1.20 to-ports=53 comment="cmsingbox_dns"WinBox 也可在 IP → DHCP Server → Leases 把终端设为静态租约,并将 Address List 填为 proxy_device。
八、路由分流(必做)
FakeIP 必须加入目标列表。Telegram 等纯 IP 连接可按需追加,相关 IP 段需由管理员持续维护。
/ip/firewall/address-list/add list=proxy_cidr address=198.18.0.0/15 comment="CMSingBox FakeIP"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.4.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.8.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.12.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.16.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.20.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.108.56.0/22 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=91.105.192.0/23 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=149.154.160.0/20 comment="Telegram"
/ip/firewall/address-list/add list=proxy_cidr address=185.76.151.0/24 comment="Telegram"创建路由表并标记命中目标。@main 确保下一跳仍由主表解析:
/routing/table/add name=cmsingbox fib
/ip/route/add dst-address=0.0.0.0/0 gateway=192.168.1.20@main routing-table=cmsingbox comment="cmsingbox_route"
/ip/firewall/mangle/add chain=prerouting dst-address-list=proxy_cidr src-address=!192.168.1.20 action=mark-routing new-routing-mark=cmsingbox passthrough=no comment="cmsingbox_mark"
/ip/firewall/nat/add chain=srcnat src-address=192.168.1.0/24 dst-address=192.168.1.20 action=masquerade comment="cmsingbox_hairpin"若使用默认拒绝的 Forward 防火墙,请把以下允许规则放在最终 Drop 之前,具体顺序按现有规则调整:
/ip/firewall/filter/add chain=forward dst-address-list=proxy_cidr action=accept comment="cmsingbox_forward_to_proxy"
/ip/firewall/filter/add chain=forward src-address-list=proxy_cidr action=accept comment="cmsingbox_forward_from_proxy"九、只分流指定设备
停用全局标记,再新建带 proxy_device 来源条件的规则:
/ip/firewall/mangle/disable [find where comment="cmsingbox_mark"]
/ip/firewall/mangle/add chain=prerouting src-address-list=proxy_device dst-address-list=proxy_cidr action=mark-routing new-routing-mark=cmsingbox passthrough=no comment="cmsingbox_mark_device"以后只需增删 proxy_device 地址列表。不要加入路由器、CMSingBox 或其他基础设施地址。
十、验证分流
- 终端重新获取 DHCP,确认 DNS 指向
192.168.1.20或命中 DNS 劫持。 - 代理域名应解析到
198.18.0.0/15;直连域名应返回真实 IP。 - 检查
/ip/firewall/mangle/print stats与/ip/firewall/nat/print stats计数。 - 在 CMSingBox DNS 监控和代理控制台确认请求。
- 分别测试国内站点、代理站点和纯 IP 应用。
192.168.1.20:2080 显式代理。十一、断网回滚
先停用引流并恢复 DNS,以下命令不会删除容器或数据:
/ip/firewall/mangle/disable [find where comment~"cmsingbox_mark"]
/ip/firewall/nat/disable [find where comment="cmsingbox_dns"]
/ip/dhcp-server/network/set [find where gateway="192.168.1.1"] dns-server=192.168.1.1让终端重新获取租约。修复后可使用相同条件的 enable [find ...] 恢复规则。
十二、更新与备份
数据保存在 disk1/cmsingbox-data。先在 CMSingBox 设置中导出备份,再更新镜像:
/container/stop [find where name="cmsingbox"]
/container/update [find where name="cmsingbox"]
/container/start [find where name="cmsingbox"]
/container/print detail旧版 ROS 若不支持 update,保留挂载目录并重新创建容器;不要删除 disk1/cmsingbox-data。
十三、故障排查
| 现象 | 重点检查 |
|---|---|
| 镜像一直 extracting | 磁盘速度、空间和内存,完成前不要 start |
| 拉取失败 | RouterOS DNS、时间、Docker Hub 与 registry-url |
| 后台打不开 | veth 地址、bridge 名称、IP 冲突、Forward 防火墙 |
| 订阅或内核下载失败 | 容器网关 192.168.1.1、DNS 和出站连接 |
| FakeIP 无法访问 | 路由表、mangle 计数、透明代理和环回排除 |
| 部分应用失败 | 纯 IP 目标列表与 CMSingBox 最终出站 |
/container/print detail
/log/print where topics~"container"
/interface/veth/print detail
/ip/firewall/mangle/print stats
/ip/firewall/nat/print stats
/ip/route/print detail where routing-table=cmsingbox设备限制与 Container 命令以 MikroTik 官方文档为准。