CentOS 7 的一键安装脚本,用于部署 WireGuard 多出口 IP 方案,支持:
- 自动安装 WireGuard(含内核升级)
- 配置多个用户(Peer)
- 每个用户绑定指定的公网出口 IP
- 自动配置策略路由、iptables、IP 转发
- 生成客户端配置文件(.conf)
✅ 适用于你描述的场景:一台服务器有多个公网 IP,不同用户走不同出口 IP 上网
📜 使用前准备
1. 确保你的服务器满足:
- CentOS 7(x86_64)
- 至少 2 个公网 IP(主 IP + 辅助 IP)
- root 权限
- 云平台已放行 UDP 51820 端口
2. 准备用户与出口 IP 映射表(示例):
| 用户名 | 内网 IP | 公网出口 IP |
|---|---|---|
| user1 | 10.0.0.2 | 203.0.113.10 |
| user2 | 10.0.0.3 | 203.0.113.11 |
| user3 | 10.0.0.4 | 203.0.113.12 |
你需要在脚本开头修改
USERS变量!
🚀 一键安装脚本:wg-multi-ip.sh
#!/bin/bash
# WireGuard Multi-Exit IP Installer for CentOS 7
# Author: Qwen
# Usage: Run as root
set -e
# ================== 配置区(请按需修改)==================
# 默认网关(必须正确!可通过 `ip route show default` 查看)
GATEWAY="203.0.113.1"
# 服务器主网卡名称(通常是 eth0, ens3 等)
INTERFACE="eth0"
# 用户列表:格式为 "用户名:出口公网IP"
# 注意:内网 IP 将自动分配为 10.0.0.2, 10.0.0.3...
declare -A USERS=(
["user1"]="203.0.113.10"
["user2"]="203.0.113.11"
["user3"]="203.0.113.12"
)
# =======================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
# 检查是否为 root
[[ $EUID -ne 0 ]] && error "请以 root 身份运行此脚本!"
# 检查是否为 CentOS 7
if ! grep -q "CentOS Linux 7" /etc/os-release; then
error "此脚本仅支持 CentOS 7!"
fi
log "开始安装 WireGuard 多出口 IP 方案..."
# 安装必要工具
yum install -y epel-release curl wget tar gzip
# 添加 ELRepo 仓库(用于新内核)
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
# 安装主线内核(kernel-ml)
log "安装最新主线内核..."
yum --enablerepo=elrepo-kernel install -y kernel-ml kernel-ml-devel
# 设置默认启动新内核
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
# 安装 WireGuard
log "安装 WireGuard..."
yum install -y wireguard-dkms wireguard-tools
# 启用 IP 转发
echo 'net.ipv4.ip_forward = 1' > /etc/sysctl.d/99-wg.conf
echo 'net.ipv4.conf.all.rp_filter = 0' >> /etc/sysctl.d/99-wg.conf
echo "net.ipv4.conf.${INTERFACE}.rp_filter = 0" >> /etc/sysctl.d/99-wg.conf
sysctl --system
# 创建 WireGuard 目录
WG_DIR="/etc/wireguard"
mkdir -p "$WG_DIR"
cd "$WG_DIR"
# 生成服务端密钥
wg genkey | tee privatekey | wg pubkey > publickey
chmod 600 privatekey
SERVER_PRIVATE=$(cat privatekey)
SERVER_PUBLIC=$(cat publickey)
SERVER_IP="10.0.0.1"
# 初始化 wg0.conf
cat > wg0.conf <<EOF
[Interface]
Address = ${SERVER_IP}/24
PrivateKey = $SERVER_PRIVATE
ListenPort = 51820
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o ${INTERFACE} -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 ${INTERFACE} -j MASQUERADE; \
iptables -D FORWARD -i %i -j ACCEPT; \
iptables -D FORWARD -o %i -j ACCEPT
EOF
# 初始化策略路由脚本
RT_SCRIPT="/etc/wg-routing.sh"
cat > "$RT_SCRIPT" <<EOF
#!/bin/bash
# WireGuard Policy Routing Script
GATEWAY="$GATEWAY"
INTERFACE="$INTERFACE"
EOF
chmod +x "$RT_SCRIPT"
# 生成用户配置
i=2
for user in "${!USERS[@]}"; do
PUBLIC_IP="${USERS[$user]}"
CLIENT_IP="10.0.0.$i"
TABLE_NAME="wg_$user"
TABLE_ID=$((99 + i))
# 添加路由表
echo "$TABLE_ID $TABLE_NAME" >> /etc/iproute2/rt_tables
# 生成客户端密钥
client_private=$(wg genkey)
client_public=$(echo "$client_private" | wg pubkey)
# 添加 Peer 到 wg0.conf
cat >> wg0.conf <<EOF
# User: $user
[Peer]
PublicKey = $client_public
AllowedIPs = $CLIENT_IP/32
EOF
# 生成客户端配置
mkdir -p /root/wg-clients
cat > "/root/wg-clients/${user}.conf" <<EOF
[Interface]
PrivateKey = $client_private
Address = $CLIENT_IP/24
DNS = 8.8.8.8
[Peer]
PublicKey = $SERVER_PUBLIC
Endpoint = $(hostname -I | awk '{print $1}'):51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
# 添加策略路由命令到脚本
cat >> "$RT_SCRIPT" <<EOF
ip route replace default via \$GATEWAY dev \$INTERFACE src $PUBLIC_IP table $TABLE_NAME
ip rule add fwmark $TABLE_ID table $TABLE_NAME 2>/dev/null
iptables -t mangle -A PREROUTING -s $CLIENT_IP -j MARK --set-mark $TABLE_ID 2>/dev/null
EOF
log "已创建用户: $user -> 出口 IP: $PUBLIC_IP"
((i++))
done
# 应用当前策略路由(临时)
bash "$RT_SCRIPT"
# 启动 WireGuard
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
# 设置开机自动加载策略路由
echo "$RT_SCRIPT" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
log "✅ 安装完成!"
log "客户端配置文件位于:/root/wg-clients/"
log "请将 .conf 文件导入 WireGuard 客户端。"
log "⚠️ 首次使用请重启服务器以加载新内核:reboot"
📥 使用方法
- 登录服务器(root)
- 下载并编辑脚本:
curl -O https://your-domain/wg-multi-ip.sh # 或直接粘贴上面内容到文件
vi wg-multi-ip.sh
-
修改配置区:
GATEWAY:你的默认网关(如172.31.0.1)INTERFACE:网卡名(如eth0)USERS:你的用户和出口 IP 映射
-
运行脚本:
chmod +x wg-multi-ip.sh
./wg-multi-ip.sh
- 重启服务器(必须!加载新内核):
reboot
- 下载客户端配置:
- 文件在
/root/wg-clients/user1.conf等 - 通过 SFTP 或
cat复制内容
- 文件在
🔒 安全提示
- 脚本生成的
.conf文件包含私钥,请妥善保管 - 不要公开分享配置文件
- 如需新增用户,可手动扩展脚本或重新运行(注意备份)
🧪 验证
连接任一客户端后访问:
curl ifconfig.me
应返回对应的出口公网 IP。
3538

被折叠的 条评论
为什么被折叠?



