Centos7下WireGuard的部署

目的:CentOS 7 上实现一台拥有 多个公网 IP 的服务器,让 不同客户端通过连接后使用指定的公网 IP 出口上网


✅ 推方案:使用 WireGuard + 策略路由(Policy Routing)

🌟 优势:

  • 轻量、高性能、低延迟(适合游戏/视频)
  • 加密强度高(基于 Curve25519)
  • 配置简单,每个客户端可独立配置出口策略
  • 原生支持 per-peer 路由控制
  • 完美配合 Linux 策略路由实现“用户绑定出口 IP”

🧾 环境假设

  • 操作系统:CentOS 7
  • 服务器网卡:eth0
  • 公网 IP(均绑定在 eth0 或辅助 IP):
    • 203.0.113.10 → 用户 A
    • 203.0.113.11 → 用户 B
    • 203.0.113.12 → 用户 C
  • 默认网关:203.0.113.1(请根据实际修改)

💡 如果你在阿里云/腾讯云,请确保这些 IP 已在控制台正确绑定(如弹性 IP 或辅助私网 IP + 公网映射)。


🔧 第一步:安装 WireGuard(CentOS 7)

# 启用 EPEL 和 ELRepo 内核仓库(WireGuard 需要较新内核或 dkms)
yum install -y epel-release
curl -o /etc/yum.repos.d/elrepo.repo https://www.elrepo.org/elrepo.repo
yum install -y yum-plugin-elrepo

# 安装 WireGuard(使用 DKMS 编译模块)
yum install -y kernel-ml kernel-ml-devel
yum install -y wireguard-dkms wireguard-tools

# 重启以使用新内核(重要!)
reboot

⚠️ CentOS 7 默认内核太旧,必须升级到 5.x+ 或使用 DKMS 编译模块。上述 kernel-ml 是主线最新内核。


🔧 第二步:生成服务端密钥

cd /etc/wireguard
wg genkey | tee privatekey | wg pubkey > publickey
chmod 600 privatekey

记录下:

  • 服务端私钥:$(cat privatekey)
  • 服务端公钥:$(cat publickey)

🔧 第三步:为每个用户创建独立 Peer(客户端)

我们将为每个用户分配:

  • 独立的内网 IP(如 10.0.0.2, 10.0.0.3...)
  • 绑定到特定公网出口 IP

1. 创建客户端密钥(本地生成,或在服务器上临时生成)

# 用户 A
wg genkey | tee clientA_privatekey | wg pubkey > clientA_publickey
# 用户 B
wg genkey | tee clientB_privatekey | wg pubkey > clientB_publickey
# 用户 C
wg genkey | tee clientC_privatekey | wg pubkey > clientC_publickey

2. 编辑 /etc/wireguard/wg0.conf

[Interface]
Address = 10.0.0.1/24
PrivateKey = <服务端私钥>
ListenPort = 51820
# 启用 IP 转发和 NAT(基础)
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE; \
         iptables -A FORWARD -i %i -j ACCEPT; \
         iptables -A FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE; \
           iptables -D FORWARD -i %i -j ACCEPT; \
           iptables -D FORWARD -o %i -j ACCEPT

# 用户 A:内网 IP 10.0.0.2 → 出口 203.0.113.10
[Peer]
PublicKey = <clientA_publickey>
AllowedIPs = 10.0.0.2/32

# 用户 B:10.0.0.3 → 203.0.113.11
[Peer]
PublicKey = <clientB_publickey>
AllowedIPs = 10.0.0.3/32

# 用户 C:10.0.0.4 → 203.0.113.12
[Peer]
PublicKey = <clientC_publickey>
AllowedIPs = 10.0.0.4/32

注意:此时所有流量仍走默认出口。下一步通过策略路由绑定具体公网 IP。


🔧 第四步:配置策略路由(关键步骤)

1. 添加自定义路由表

echo "100 out_ip1" >> /etc/iproute2/rt_tables
echo "101 out_ip2" >> /etc/iproute2/rt_tables
echo "102 out_ip3" >> /etc/iproute2/rt_tables

2. 配置每个出口的路由表(替换网关为你的实际网关)

# 用户 A: 10.0.0.2 → 203.0.113.10
ip route add default via 203.0.113.1 dev eth0 src 203.0.113.10 table out_ip1

# 用户 B: 10.0.0.3 → 203.0.113.11
ip route add default via 203.0.113.1 dev eth0 src 203.0.113.11 table out_ip2

# 用户 C: 10.0.0.4 → 203.0.113.12
ip route add default via 203.0.113.1 dev eth0 src 203.0.113.12 table out_ip3

3. 使用 iptables 打标记(MARK)

# 清理旧规则
iptables -t mangle -F PREROUTING

# 标记来源 IP
iptables -t mangle -A PREROUTING -s 10.0.0.2 -j MARK --set-mark 100
iptables -t mangle -A PREROUTING -s 10.0.0.3 -j MARK --set-mark 101
iptables -t mangle -A PREROUTING -s 10.0.0.4 -j MARK --set-mark 102

4. 添加策略路由规则

ip rule add fwmark 100 table out_ip1
ip rule add fwmark 101 table out_ip2
ip rule add fwmark 102 table out_ip3

5. (可选但推荐)关闭反向路径过滤

echo 'net.ipv4.conf.all.rp_filter = 0' >> /etc/sysctl.conf
echo 'net.ipv4.conf.eth0.rp_filter = 0' >> /etc/sysctl.conf
sysctl -p

🔧 第五步:启用 IP 转发

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

🔧 第六步:启动 WireGuard

systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0

检查状态:

wg show
ip a show wg0

📱 第七步:生成客户端配置文件

用户 A 的配置 (clientA.conf):

[Interface]
PrivateKey = <clientA_privatekey>
Address = 10.0.0.2/24
DNS = 8.8.8.8

[Peer]
PublicKey = <服务端公钥>
Endpoint = <你的服务器公网IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

其他用户类似,只需改 AddressPrivateKey

.conf 文件导入 WireGuard 客户端(Windows/macOS/iOS/Android 均支持)。


✅ 验证效果

  1. 用户 A 连接后访问 https://ip138.com → 显示 203.0.113.10
  2. 用户 B 显示 203.0.113.11
  3. 用户 C 显示 203.0.113.12

💾 持久化策略路由(防止重启丢失)

创建启动脚本 /etc/network-wg.sh

#!/bin/bash
# 策略路由配置
ip route replace default via 203.0.113.1 dev eth0 src 203.0.113.10 table out_ip1
ip route replace default via 203.0.113.1 dev eth0 src 203.0.113.11 table out_ip2
ip route replace default via 203.0.113.1 dev eth0 src 203.0.113.12 table out_ip3

ip rule add fwmark 100 table out_ip1 2>/dev/null
ip rule add fwmark 101 table out_ip2 2>/dev/null
ip rule add fwmark 102 table out_ip3 2>/dev/null

iptables -t mangle -A PREROUTING -s 10.0.0.2 -j MARK --set-mark 100 2>/dev/null
iptables -t mangle -A PREROUTING -s 10.0.0.3 -j MARK --set-mark 101 2>/dev/null
iptables -t mangle -A PREROUTING -s 10.0.0.4 -j MARK --set-mark 102 2>/dev/null

赋予执行权限并加入启动:

chmod +x /etc/network-wg.sh
echo "/etc/network-wg.sh" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local

🛡 安全与维护建议

  • 使用强密钥(WireGuard 自动生成即足够安全)
  • 限制 AllowedIPs 为 0.0.0.0/0 表示全流量走 VPN(如需分流可调整)
  • 定期更新系统和内核
  • 可结合 fail2ban 防暴力破解(虽然 WireGuard 无端口扫描风险)

✅ 总结

功能实现方式
多用户每个 Peer 独立密钥 + 内网 IP
多出口 IP策略路由 + iptables MARK + 多路由表
安全性WireGuard(现代加密)✅
性能极低开销,适合游戏/高清视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路生花工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值