找回密码
 立即注册
查看: 15|回复: 0

MikroTik WireGuard 完整配置教程

[复制链接]

47

主题

15

回帖

982万

积分

管理员

积分
9820456
发表于 前天 17:01 | 显示全部楼层 |阅读模式
       WireGuard 是一款现代化的 VPN 协议,以其简洁、高效和安全性著称。相比传统的 IPsec 或 OpenVPN,WireGuard 代码量更少、配置更简单、性能更出色。本教程将详细介绍如何在 MikroTik RouterOS 上配置 WireGuard 的各种应用场景。

1. WireGuard 简介
WireGuard 被设计为一种通用 VPN,既能在嵌入式设备上运行,也能在超级计算机上高效工作。它的核心特点包括:
  • 极简配置:仅需几行命令即可完成
  • 高性能:基于现代加密技术,速度快于 OpenVPN
  • 安全性:使用先进加密算法(Curve25519、ChaCha20Poly1305 等)
  • 跨平台:支持 Windows、macOS、Linux、Android、iOS

在 RouterOS 中,WireGuard 从 7.x 版本开始原生支持,无需额外安装包。

2. 基础概念:接口与对等端
WireGuard 中没有传统的“服务端”和“客户端”概念,而是通过 接口(Interface) 和 对等端(Peer) 进行点对点连接。

概念
说明
WireGuard 接口虚拟 VPN 接口,有自己的 IP 地址和监听端口。每个接口自动生成公钥/私钥对
对等端(Peer)允许连接到本接口的远程设备,通过公钥识别。可以配置允许的 IP 地址和端点信息


3. 场景一:站点到站点(Site-to-Site)VPN
两个办公室需要安全互联,各自有独立的内网网段。这是 WireGuard 最常见的应用场景之一。
3.1 网络拓扑
  1. Office 1 (10.1.202.0/24)  <--- WireGuard 隧道 --->  Office 2 (10.1.101.0/24)
  2.            │                                                │
  3.       Router A                                         Router B
  4.      (MikroTik)                                       (MikroTik)
复制代码

3.2 配置步骤步骤一:在 Router A(Office 1)创建 WireGuard 接口
  1. /interface wireguard
  2. add listen-port=13231 name=wireguard1
复制代码
创建后,接口会自动生成公钥和私钥。使用以下命令查看:
  1. <font color="#000000">/interface wireguard print
复制代码
输出示例:
  1. Flags: X - disabled; R - running
  2. 0  R name="wireguard1" mtu=1420 listen-port=13231
  3.       private-key="yKt9NJ4e5qlaSgh48WnPCDCEkDmq+VsBTt/DDEBWfEo="
  4.       public-key="u7gYAg5tkioJDcm3hyS7pm79eADKPs/ZUGON6/fF3iI="
复制代码
步骤二:在 Router A 添加对等端(指向 Router B)
  1. /interface wireguard peers
  2. add allowed-address=10.1.101.0/24 endpoint-address=192.168.80.1 \
  3.     endpoint-port=13231 interface=wireguard1 \
  4.     public-key="v/oIzPyFm1FPHrqhytZgsKjU7mUToQHLrW+Tb5e601M="
复制代码
关键参数说明:
  • allowed-address:对方内网网段,用于路由
  • endpoint-address/port:对方公网 IP 和 WireGuard 监听端口
  • public-key:对方 WireGuard 接口的公钥



步骤三:在 Router A 配置 IP 地址和路由

  1. # 为隧道接口配置 IP(点对点)
  2. /ip address
  3. add address=10.255.255.1/30 interface=wireguard1

  4. # 添加路由:访问对方内网走 WireGuard 接口
  5. /ip route
  6. add dst-address=10.1.101.0/24 gateway=wireguard1
复制代码
步骤四:在 Router B 进行对称配置
Router B 的配置与 Router A 类似,只是 IP 和公钥互换:
  1. # 创建接口
  2. /interface wireguard
  3. add listen-port=13231 name=wireguard1

  4. # 添加对等端(指向 Router A)
  5. /interface wireguard peers
  6. add allowed-address=10.1.202.0/24 endpoint-address=192.168.90.1 \
  7.     endpoint-port=13231 interface=wireguard1 \
  8.     public-key="u7gYAg5tkioJDcm3hyS7pm79eADKPs/ZUGON6/fF3iI="

  9. # 配置 IP 和路由
  10. /ip address
  11. add address=10.255.255.2/30 interface=wireguard1

  12. /ip route
  13. add dst-address=10.1.202.0/24 gateway=wireguard1
复制代码
3.3 防火墙配置
默认防火墙会阻止 WireGuard 流量,需要在 input 链中添加允许规则:
  1. /ip firewall filter
  2. add chain=input protocol=udp dst-port=13231 action=accept \
  3.     comment="Allow WireGuard"
复制代码
4. 场景二:远程访问 VPN(Road Warrior)
移动设备(手机、笔记本)需要安全接入公司内网。WireGuard 的“服务端”需配置为 responder 模式,避免不必要的日志。
4.1 服务端(MikroTik)配置创建 WireGuard 接口
  1. /interface wireguard
  2. add listen-port=51820 name=wg-server private-key="自动生成或手动指定"
复制代码
查看生成的公钥:
  1. /interface wireguard print
复制代码
为每个客户端添加对等端
假设第一个客户端的隧道 IP 为 10.10.10.2/32:
  1. /interface wireguard peers
  2. add allowed-address=10.10.10.2/32 interface=wg-server \
  3.     public-key="客户端公钥" \
  4.     is-responder=yes
复制代码
配置接口 IP 和 NAT
  1. # 为 WireGuard 接口分配 IP
  2. /ip address
  3. add address=10.10.10.1/24 interface=wg-server

  4. # 启用 NAT,让客户端通过 VPN 访问互联网
  5. /ip firewall nat
  6. add chain=srcnat out-interface=ether1 action=masquerade

  7. # 如果客户端需要访问内网,添加路由
  8. /ip route
  9. add dst-address=192.168.88.0/24 gateway=wg-server
复制代码
4.2 客户端配置Windows / macOS / Linux / 移动端
  1. [Interface]
  2. PrivateKey = <客户端自动生成的私钥>
  3. Address = 10.10.10.2/24
  4. DNS = 10.10.10.1

  5. [Peer]
  6. PublicKey = <MikroTik 的公钥>
  7. AllowedIPs = 0.0.0.0/0          # 所有流量走 VPN
  8. Endpoint = <路由器公网IP>:51820
  9. PersistentKeepalive = 25         # NAT 穿透
复制代码
配置导出:QR 码方式
RouterOS 支持直接生成客户端配置和二维码:
  1. /interface wireguard peers
  2. show-client-config <peer-name>
复制代码
此命令会显示配置文本和二维码,手机客户端扫码即可自动配置。
5. 场景三:MikroTik 作为 WireGuard 客户端
你的 MikroTik 路由器需要作为 VPN 客户端,连接到第三方 VPN 服务商或自建服务端。
5.1 导入配置文件
如果你已有标准的 WireGuard 配置文件(.conf 格式),可以使用导入命令:
  1. /interface wireguard wg-import file=config.conf
复制代码
注意:配置文件开头不能有 # 注释,否则会报错。
5.2 手动配置客户端创建 WireGuard 接口
  1. /interface wireguard
  2. add name=wg-client private-key="<客户端私钥>"
复制代码
添加对等端(服务端)
  1. /interface wireguard peers
  2. add interface=wg-client \
  3.     public-key="<服务端公钥>" \
  4.     endpoint-address="vpn.example.com" \
  5.     endpoint-port=51820 \
  6.     allowed-address=0.0.0.0/0 \
  7.     persistent-keepalive=25s
复制代码
配置接口 IP
  1. /ip address
  2. add address=10.10.10.5/24 interface=wg-client
复制代码
5.3 策略路由:仅指定内网流量走 VPN
如果不想所有流量都走 VPN,而是只让特定内网网段(如 192.168.100.0/24)通过 WireGuard 访问外网,需要使用策略路由:
  1. # 创建独立路由表
  2. /routing table
  3. add name=via-wg fib

  4. # 在独立路由表中添加默认路由
  5. /ip route
  6. add dst-address=0.0.0.0/0 gateway=wg-client routing-table=via-wg

  7. # 配置 NAT
  8. /ip firewall nat
  9. add chain=srcnat out-interface=wg-client action=masquerade

  10. # 添加策略规则:指定源地址的流量使用 via-wg 路由表
  11. /routing rule
  12. add action=lookup-only-in-table src-address=192.168.100.0/24 table=via-wg
复制代码
这样配置后,只有源 IP 为 192.168.100.0/24 的流量会走 WireGuard 隧道。
6. 场景四:多 WAN 环境下的 WireGuard
当路由器有多个 WAN 出口时,WireGuard 隧道可能因回包路径错误而无法建立。这是因为两个方向的握手可能走了不同出口。
6.1 问题现象
WireGuard 对等端双方都会主动发送握手包。如果服务端有两条 WAN 线路,客户端的握手从 WAN1 进入,服务端可能从 WAN2 回复,导致连接失败。
6.2 解决方案:策略路由标记
使用 mangle 规则,确保回包走原路。
  1. # 1. 记录进入的 WireGuard 握手源 IP(以 WAN2 为例)
  2. /ip firewall mangle
  3. add action=add-src-to-address-list chain=prerouting \
  4.     address-list=WAN2_WireGuard_clients address-list-timeout=1m \
  5.     dst-port=13231 in-interface=ether2 protocol=udp \
  6.     comment="记录从 WAN2 进入的 WireGuard 客户端 IP"

  7. # 2. 标记出站连接的标记
  8. /ip firewall mangle
  9. add action=mark-connection chain=output \
  10.     dst-address-list=WAN2_WireGuard_clients dst-port=13231 \
  11.     new-connection-mark=wan2 protocol=udp

  12. # 3. 标记路由,指定使用对应路由表
  13. /ip firewall mangle
  14. add action=mark-routing chain=output connection-mark=wan2 \
  15.     dst-port=13231 new-routing-mark=wan2 protocol=udp

  16. # 4. 确保使用正确的源 IP 发出
  17. /ip firewall nat
  18. add action=masquerade chain=srcnat out-interface=ether2
复制代码
7. 高级特性:WireGuard + HotSpot 双因素认证
RouterOS v7 支持将 HotSpot 绑定到 WireGuard 接口,实现“先 VPN 连接,再 Portal 认证”的双重安全机制。
7.1 配置步骤
  1. # 1. 确保 WireGuard 接口已配置
  2. /interface wireguard print

  3. # 2. 在 WireGuard 接口上配置 HotSpot
  4. /ip hotspot
  5. add interface=wg1

  6. # 或使用 setup 向导
  7. /ip hotspot setup
  8. # 选择接口时选 wg1
复制代码
7.2 配置 TOTP 用户
  1. /ip hotspot user
  2. add name=peer1 totp-secret=HVR4CFHAFOWFGGFAGSA5JVTIMMPG6GMT
复制代码
客户端连接 WireGuard 后,会弹出 HotSpot 登录页面,需要输入用户名和 6 位 TOTP 动态密码。
8. IPv6 与 WireGuard
WireGuard 原生支持 IPv6。以下是一个纯 IPv6 隧道的配置示例:
8.1 服务端配置(Debian 示例)
  1. [Interface]
  2. Address = 2a14:9b80:0:1::1/64
  3. ListenPort = 51820
  4. PrivateKey = <私钥>

  5. [Peer]
  6. PublicKey = <MikroTik 公钥>
  7. AllowedIPs = 2a14:9b80:101::/48
  8. Endpoint = [2001:41f0:6f67::2]:51820
复制代码
8.2 MikroTik 客户端配置
  1. /interface wireguard
  2. add listen-port=51820 mtu=1420 name=wg-ipv6

  3. /interface wireguard peers
  4. add allowed-address=::/0 endpoint-address=2001:678:3a4:1::100 \
  5.     endpoint-port=51820 interface=wg-ipv6 \
  6.     public-key="服务端公钥" persistent-keepalive=30s

  7. /ipv6 address
  8. add address=2a14:9b80:101::1 interface=wg-ipv6

  9. /ipv6 route
  10. add dst-address=::/0 gateway=wg-ipv6 routing-table=via-wg6
复制代码
9. 故障排查指南9.1 检查接口状态
  1. /interface wireguard print
  2. /interface wireguard peers print
复制代码
查看 last-handshake 字段,如果一直为 0,说明握手未成功。
9.3 检查防火墙
确认 WireGuard 端口(默认 13231 或 51820)已放行:
  1. /ip firewall filter print where protocol=udp
复制代码
9.4 查看日志
  1. /log print where topics~"wireguard"
复制代码
9.5 常见问题速查

问题
可能原因
解决方案
握手不成功公钥配置错误核对两端公钥
握手不成功防火墙阻止添加 input 允许规则
握手不成功NAT 穿透问题配置 persistent-keepalive
能 ping 通但对端网络不通路由缺失检查 allowed-address 和路由表
多 WAN 下不稳定回包路径错误配置 mangle 规则确保对称路由
导入配置失败配置文件有注释移除 # 开头的行

10. 总结
WireGuard 在 MikroTik RouterOS 上的配置非常灵活,适用场景涵盖:
  • 站点到站点 VPN:企业分支互联
  • 远程访问 VPN:移动办公接入
  • 第三方 VPN 客户端:隐私保护
  • 多 WAN 环境:需特殊处理对称路由

掌握 WireGuard 的关键在于理解 接口与对等端的关系 以及 路由与策略路由的配合。建议从简单场景开始,逐步扩展到复杂环境。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|培训报名|EDCwifi.BBS ( 粤ICP备17065502号-1 粤公网安备44030702001530号 )

GMT+8, 2026-4-3 08:51 , Processed in 0.191133 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表