


















之前我用过 Zerotier,但感觉不够轻量。后来发现 WireGuard 在性能、安全性和配置简洁性上更有优势,于是决定尝试一下。
WireGuard 是一个开源的VPN工具,基于UDP协议,使用户能够使用IPv4或IPv6的虚拟IP地址,实现内网穿透。
WireGuard 是一种现代化的 VPN(虚拟专用网络)协议和软件。
运行在内核层,使用最新的加密算法,目标是 简单、快速、安全。
相比传统的 VPN(如 OpenVPN、IPSec),WireGuard 更轻量,配置更直观。
Wireguard官网:WireGuard
快速入门指南:WireGuard快速入门
# Ubuntu/Debian
sudo apt update
sudo apt install wireguard
# Arch Linux
sudo pacman -S wireguard
# Alpine
sudo apk add wireguard-tools wireguard-tools-doc
# RedHat
sudo dnf install wireguard wireguard-tools
# WireGuard模块是否加载
sudo modprobe wireguard
lsmod | grep wireguard
# 内核版本(建议5.6+)
uname -r
WireGuard配置文件存放在/etc/wireguard目录下,默认文件名格式为wg0.conf。
# 生成密钥对(公钥和私钥)
wg genkey | tee /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key
# 密钥文件权限
chmod 600 /etc/wireguard/private.key
chmod 644 /etc/wireguard/public.key
创建并编辑/etc/wireguard/wg0.conf文件
sudo vim /etc/wireguard/wg0.conf
配置文件内容如下:
[Interface]
# 服务器私钥
PrivateKey = YOUR_SERVER_PRIVATE_KEY
# 监听的端口
ListenPort = 51820
# 服务器的虚拟IPv4和IPv6地址
Address = 10.0.0.1/24, fd86:ae86::1/64
# 配置MTU
# MTU = 1420
# 客户端配置
[Peer]
# 客户端公钥
PublicKey = CLIENT_PUBLIC_KEY
# 允许的客户端IP范围
AllowedIPs = 10.0.0.2/32, fd86:ae86::2/128
# 保持连接(心跳)
# PersistentKeepalive = 25
为了服务器能转发客户端流量,服务端需要开启IP 转发,客户端则不需要。
# 编辑sysctl.conf
sudo vim /etc/sysctl.conf
# IPv4 转发
net.ipv4.ip_forward=1
# 内核允许数据包源验证(除非使用网络分流)
# net.ipv4.conf.all.src_valid_mark=1
# IPv6 转发
net.ipv6.conf.all.disable_ipv6=0
net.ipv6.conf.all.forwarding=1
net.ipv6.conf.default.forwarding=1
# 规则生效
sudo sysctl -p
如果服务器使用防火墙,需要放行规则允许UDP wireguard端口。
国内大厂要去控制台 - 防火墙规则添加相应的UDP端口,不然无法连接。
# ufw
sudo ufw allow 51820/udp
# nftables
nft add rule inet filter input udp dport 51820 accept
nft add rule inet filter forward udp dport 51820 accept
# firewalld
sudo firewall-cmd --add-port=51820/udp --permanent
sudo firewall-cmd --reload
# iptables
sudo iptables -A INPUT -p udp --dport 51820 -j ACCEPT
sudo iptables -A FORWARD -p udp --dport 51820 -j ACCEPT
# 启用wg0接口
sudo wg-quick up wg0
# 停止wg0接口
sudo wg-quick down wg0
# 查看wg0接口状态
wg show wg0
# 开机启动
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
# 关闭开机启动
sudo systemctl disable wg-quick@wg0
Alpine系统上,设置服务开机启动的方法为:
# 创建启动wg0服务脚本
echo -e '#!/bin/sh\nwg-quick up wg0' > /etc/local.d/wg0.start
# 赋予执行权限
chmod +x /etc/local.d/wg0.start
# 添加到默认运行级别,使其开机运行
rc-update add local default
# 创建密钥对
wg genkey | tee /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key
# 密钥文件权限
chmod 600 /etc/wireguard/private.key
chmod 644 /etc/wireguard/public.key
创建并编辑/etc/wireguard/client.conf文件
sudo vim /etc/wireguard/client.conf
配置文件内容如下:
[Interface]
# 客户端私钥
PrivateKey = CLIENT_PRIVATE_KEY
# 客户端的虚拟IP地址
Address = 10.0.0.2/24, fd86:ae86::2/64
# DNS服务器(可选)
DNS = 8.8.8.8, 1.1.1.1, 2606:4700:4700::1111
[Peer]
# 服务器公钥
PublicKey = SERVER_PUBLIC_KEY
# 服务器端点(公网IP和端口)
Endpoint = YOUR_SERVER_IP:51820
# 允许的客户端IP范围
AllowedIPs = 10.0.0.0/24, fd86:ae86::/64
# 保持连接(心跳)
PersistentKeepalive = 25
# 启动client接口
wg-quick up client
# 停止client接口
wg-quick down client
# 查看client接口状态
wg show client
wg-quick扩展了WireGuard的配置文件,提供了一些额外的功能。
PostUp:接口启动后执行的命令
添加路由
设置 iptables NAT 转发
开启防火墙规则
PostDown:接口关闭前执行的命令
删除路由
清理 iptables 规则
恢复防火墙状态
它的好处是:不用手动敲 iptables 或路由命令,配置文件里写好就能自动管理。
# NAT转发规则
[Interface]
................
PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
在服务端添加多个客户端配置文件,并启动多个 WireGuard 接口。
[Peer]
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32, fd86:ae86::2/128
[Peer]
PublicKey = CLIENT2_PUBLIC_KEY
AllowedIPs = 10.0.0.3/32, fd86:ae86::3/128
然后启动多个 WireGuard 接口:
# 停止wg0接口
wg-quick down wg0
# 启动wg0接口
wg-quick up wg0
配置服务端和客户端的AllowedIPs字段,将流量路由到指定的网段。
| 写法 | 含义 | 使用场景 |
|---|---|---|
| 10.0.0.0/24, fd86:ae86::/64 | ✅只路由内网网段流量 | 内网访问、分流模式 |
| 0.0.0.0/0, ::/0 | 🌐全部流量走VPN | 全局代理、统一出口 |
| 10.0.0.0/24, fd86:ae86::/64, 0.0.0.0/0, ::/0 | 🔀优先内网,再走VPN | 混合模式 |
PersistentKeepalive 在配置手机端和pc端的时候,可以配置为25-30秒,保持连接活跃,避免长时间无流量导致掉线。
MTU 默认为1420字节,可以根据实际情况调整。
DNS 可以配置为国内的DNS服务器,提高访问速度。
预共享密钥(Pre-Shared Key,PSK)是一种用于WireGuard VPN的密码,用于在WireGuard VPN中实现对端认证。
预共享密钥的生成和设置:
# 生成预共享密钥
wg genpsk | tee /etc/wireguard/psk.key
# 密钥文件权限
chmod 600 /etc/wireguard/psk.key
配置服务端和客户端的PresharedKey字段,将预共享密钥添加到[Peer]配置文件中。
PresharedKey = YOUR_PRESHARED_KEY
WireGuard 管理命令:
# 开启网口
wg-quick up wg0
# 关闭网口
wg-quick down wg0
# 查看所有接口状态
wg show
# 查看特定接口状态
wg show wg0
# 查看接口连接状态
wg show wg0 peers
# 查看连接状态
wg show wg0 latest-handshakes
系统接口状态检测:
# 查看系统接口
ip link show wg0
# 查看系统接口地址
ip address show wg0
# 查看系统路由表
ip route show table main
ip route show table local
# 获取特定路由
ip route get 10.0.0.2
ip route get 10.0.0.2 dev wg0
# 查看内核日志
dmesg | grep wireguard
# 监控异常连接
wg show | grep -i "latest handshake"
# 实时监控WireGuard流量
watch -n 1 'sudo wg show'
# 查看接口统计
ip -s link show wg0
/etc/wireguard/*.key/etc/wireguard/*.confufw status 或 nft list rulesetnetstat -tulpn | grep 51820# 检查防火墙规则
ufw status
nft list ruleset
iptables -L -v -n | grep 51820
firewall-cmd --list-all
# 检查服务端监听状态
netstat -tulpn | grep 51820
WireGuard 是一个轻量级、安全、快速的 VPN 工具,适合用于内网穿透、混合网络、云计算等场景。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。