CentOS 7 中一键安装WireGuard脚本

 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
user110.0.0.2203.0.113.10
user210.0.0.3203.0.113.11
user310.0.0.4203.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"

📥 使用方法

  1. 登录服务器(root)
  2. 下载并编辑脚本
curl -O https://your-domain/wg-multi-ip.sh  # 或直接粘贴上面内容到文件
vi wg-multi-ip.sh
  1. 修改配置区

    • GATEWAY:你的默认网关(如 172.31.0.1
    • INTERFACE:网卡名(如 eth0
    • USERS:你的用户和出口 IP 映射
  2. 运行脚本

chmod +x wg-multi-ip.sh
./wg-multi-ip.sh
  1. 重启服务器(必须!加载新内核):
reboot
  1. 下载客户端配置
    • 文件在 /root/wg-clients/user1.conf 等
    • 通过 SFTP 或 cat 复制内容

🔒 安全提示

  • 脚本生成的 .conf 文件包含私钥,请妥善保管
  • 不要公开分享配置文件
  • 如需新增用户,可手动扩展脚本或重新运行(注意备份)

🧪 验证

连接任一客户端后访问:

curl ifconfig.me

应返回对应的出口公网 IP。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一路生花工作室

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

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

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

打赏作者

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

抵扣说明:

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

余额充值