跳到主要内容

WireGuard 配置

wireguard配置

1.服务端

  • 安装wireguard
# 添加 ELRepo 仓库
sudo dnf install -y epel-release elrepo-release
sudo dnf update -y

# 安装 WireGuard
sudo dnf install -y kmod-wireguard wireguard-tools
  • 生成密钥对
# 创建配置目录
sudo mkdir -p /etc/wireguard
cd /etc/wireguard

# 生成服务器密钥对
sudo wg genkey | tee server-private.key | wg pubkey > server-public.key
sudo chmod 600 server-private.key
  • 创建配置文件 /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
ListenPort = 51838
PrivateKey = gBof2wK8WjIg06t9z574Ous7DZWMVQ

# iptable配置
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
SaveConfig = false
# 如果需要IPv6支持,添加:
PostUp = ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE


[Peer]
# 对应Mac tianliang
PublicKey = R4/X9LNywobnCps1KArLMUfzuri9yK
AllowedIPs = 10.0.0.122/32


[Peer]
# 对应安卓note11tp
PublicKey = uQv+5xgYez5Gj/THdYnIHqrj2KKlEE
AllowedIPs = 10.0.0.101/32

[Peer]
# 对应 armbian
PublicKey = fYI/iyNV7eEOCG3VBJOZiHDtDCT5
AllowedIPs = 10.0.0.2/32
  • 配置防火墙
# 开放对应的udp端口
# 添加新端口规则
sudo iptables -A INPUT -p udp --dport 51838 -j ACCEPT

# 删除旧端口规则(如果需要)
sudo iptables -D INPUT -p udp --dport 51838 -j ACCEPT

# 保存规则
sudo service iptables save
# 或
sudo iptables-save > /etc/sysconfig/iptables

# 重新加载规则
sudo iptables-restore < /etc/sysconfig/iptables
  • 启动
systemctl start wg-quick@wg0.service

# 查看状态
wg show

2.客户端

  • 安装wireguard
# 添加 ELRepo 仓库
sudo dnf install -y epel-release elrepo-release
sudo dnf update -y

# 安装 WireGuard
sudo dnf install -y kmod-wireguard wireguard-tools
  • 生成密钥对
# 创建配置目录
sudo mkdir -p /etc/wireguard
cd /etc/wireguard

# 生成服务器密钥对
sudo wg genkey | tee server-private.key | wg pubkey > server-public.key
sudo chmod 600 server-private.key
  • 创建配置文件 /etc/wireguard/c1.conf
[Interface]
PrivateKey = QGa/QkPTdsP9ywBrNKnc9IM67XUuZpVbxqeyWohhMHo=
Address = 10.0.0.2/32

[Peer]
PublicKey = BF8S1b0/hZOhlqg+aWJY9yiKURd+6biQ8Zy5WLPzoik=
Endpoint = 3o3.top:51838
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25
  • 启动
wg-quick up c1

# 关闭
wg-quick down c1

# 查看状态
wg show