wiregurd
# 使用 wiregurd搭建VPN
# WireGuard 完整安装配置指南
# 一、安装准备
# 环境假设
- 中转服务器:云服务器,Ubuntu 20.04+,公网IP
- 局域网A:192.168.1.0/24,网关设备安装 WireGuard
- 局域网B:192.168.2.0/24,网关设备安装 WireGuard
- WireGuard 虚拟网段:10.0.0.0/24
# 二、安装 WireGuard
# Ubuntu/Debian 系统
# 更新系统
sudo apt update
sudo apt upgrade -y
# 安装 WireGuard
sudo apt install wireguard -y
# 安装工具
sudo apt install wireguard-tools -y
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# CentOS/RHEL 系统
# 安装 EPEL 源
sudo yum install epel-release -y
# 安装 WireGuard
sudo yum install wireguard-tools -y
1
2
3
4
5
2
3
4
5
# Windows 系统
- 下载官方客户端:https://www.wireguard.com/install/
- 运行安装程序
- 启动 WireGuard 应用
# 三、生成密钥对
# 在每台设备上执行:
# 创建配置目录
sudo mkdir -p /etc/wireguard
cd /etc/wireguard
# 生成私钥
wg genkey | sudo tee server-private.key
sudo chmod 600 server-private.key
# 生成公钥
sudo cat server-private.key | wg pubkey | sudo tee server-public.key
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 为三台设备生成密钥:
# 中转服务器
wg genkey | tee server-private.key | wg pubkey > server-public.key
# 局域网A
wg genkey | tee clientA-private.key | wg pubkey > clientA-public.key
# 局域网B
wg genkey | tee clientB-private.key | wg pubkey > clientB-public.key
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 四、配置文件
# 1. 中转服务器配置 (/etc/wireguard/wg0.conf)
[Interface]
# 服务器私钥
PrivateKey = <server-private.key 内容>
# 服务器在 WireGuard 网络中的 IP
Address = 10.0.0.1/24
# 监听端口
ListenPort = 51820
# 启用 IP 转发
PostUp = sysctl -w net.ipv4.ip_forward=1
# 添加 iptables 规则
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
# 停止时清理规则
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
# 局域网A
[Peer]
PublicKey = <clientA-public.key 内容>
# 允许的 IP 范围
AllowedIPs = 10.0.0.2/32, 192.168.1.0/24
# 局域网B
[Peer]
PublicKey = <clientB-public.key 内容>
AllowedIPs = 10.0.0.3/32, 192.168.2.0/24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2. 局域网A配置 (/etc/wireguard/wg0.conf)
[Interface]
PrivateKey = <clientA-private.key 内容>
Address = 10.0.0.2/24
# 启用 IP 转发
PostUp = sysctl -w net.ipv4.ip_forward=1
# 添加路由规则
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
# 连接到中转服务器
[Peer]
PublicKey = <server-public.key 内容>
Endpoint = <中转服务器公网IP>:51820
AllowedIPs = 10.0.0.0/24, 192.168.2.0/24
PersistentKeepalive = 25
# 尝试直连局域网B
[Peer]
PublicKey = <clientB-public.key 内容>
AllowedIPs = 192.168.2.0/24
PersistentKeepalive = 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 3. 局域网B配置 (/etc/wireguard/wg0.conf)
[Interface]
PrivateKey = <clientB-private.key 内容>
Address = 10.0.0.3/24
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
# 连接到中转服务器
[Peer]
PublicKey = <server-public.key 内容>
Endpoint = <中转服务器公网IP>:51820
AllowedIPs = 10.0.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25
# 尝试直连局域网A
[Peer]
PublicKey = <clientA-public.key 内容>
AllowedIPs = 192.168.1.0/24
PersistentKeepalive = 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 五、启动服务
# 在所有设备上执行:
# 设置配置文件权限
sudo chmod 600 /etc/wireguard/wg0.conf
# 启动 WireGuard
sudo wg-quick up wg0
# 设置开机自启
sudo systemctl enable wg-quick@wg0
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 常用管理命令:
# 查看状态
sudo wg show
# 停止服务
sudo wg-quick down wg0
# 重启服务
sudo wg-quick down wg0 && sudo wg-quick up wg0
# 查看系统日志
sudo journalctl -u wg-quick@wg0 -f
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 六、防火墙配置
# 中转服务器防火墙:
# Ubuntu/Debian (ufw)
sudo ufw allow 51820/udp
sudo ufw reload
# CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --add-port=51820/udp
sudo firewall-cmd --reload
1
2
3
4
5
6
7
2
3
4
5
6
7
# 云服务器安全组:
- 开放入站规则:UDP 51820 端口
- 来源:0.0.0.0/0
# 七、测试连接
# 1. 检查 WireGuard 状态
sudo wg show
1
# 2. 测试网络连通性
# 从局域网A测试
ping 10.0.0.1 # 测试到中转服务器
ping 10.0.0.3 # 测试到局域网B的 WireGuard IP
ping 192.168.2.1 # 测试到局域网B的网关
# 从局域网B测试
ping 10.0.0.1 # 测试到中转服务器
ping 10.0.0.2 # 测试到局域网A的 WireGuard IP
ping 192.168.1.1 # 测试到局域网A的网关
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3. 检查是否实现直连
# 查看 WireGuard 连接信息
sudo wg show wg0
# 查看路由表
ip route show table all
# 测试延迟
ping -c 10 192.168.2.1
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 八、故障排除
# 常见问题:
连接失败
# 检查防火墙 sudo ufw status # 检查端口监听 sudo netstat -ulnp | grep 51820
1
2
3
4无法访问内网
# 检查 IP 转发 cat /proc/sys/net/ipv4/ip_forward # 检查 iptables 规则 sudo iptables -L -n -v
1
2
3
4密钥错误
# 重新生成密钥对 wg genkey | tee private.key | wg pubkey > public.key
1
2
# 调试命令:
# 查看详细日志
sudo journalctl -u wg-quick@wg0 -f
# 手动测试配置
sudo wg-quick up wg0 --verbose
# 检查网络接口
ip addr show wg0
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 九、优化建议
- 定期更新系统和 WireGuard
- 监控连接状态和延迟
- 备份配置文件
- 使用强密码保护密钥文件
- 考虑使用 DNS 解析内网设备
# 十、注意事项
- 确保所有设备时间同步
- 密钥文件权限设置为 600
- 防火墙正确配置
- 网络段不要冲突
- 定期检查连接状态
上次更新: 2025/06/19, 11:56:30