惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

钧言极客

Alpine Linux 服务器配置指北 Visual Studio Code 安装开启 中文语言包插件 Linux 安装 MediaInfo:轻松解析视频文件信息 Hugo 实现的短标签 PVE存储合并实践:整合local-lvm到local Debian 安装NVIDLA显卡驱动和CUDA工具包 Linux 管理 UFW 防火墙 Alpine VNC重置密码
组网工具WireGuard入门指南
JunYan · 2026-01-22 · via 钧言极客

前言

之前我用过 Zerotier,但感觉不够轻量。后来发现 WireGuard 在性能、安全性和配置简洁性上更有优势,于是决定尝试一下。

WireGuard 是一个开源的VPN工具,基于UDP协议,使用户能够使用IPv4或IPv6的虚拟IP地址,实现内网穿透。

  • WireGuard 是一种现代化的 VPN(虚拟专用网络)协议和软件。

  • 运行在内核层,使用最新的加密算法,目标是 简单、快速、安全。

  • 相比传统的 VPN(如 OpenVPN、IPSec),WireGuard 更轻量,配置更直观。

Wireguard官网:WireGuard

快速入门指南:WireGuard快速入门

安装WireGuard

Linux 安装

# 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 服务端配置

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 转发

为了服务器能转发客户端流量,服务端需要开启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

启动 WireGuard 服务

# 启用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

WireGuard 客户端配置

生成密钥对

# 创建密钥对
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

启动 WireGuard 客户端

# 启动client接口
wg-quick up client

# 停止client接口
wg-quick down client

# 查看client接口状态
wg show client

高级玩法

wg-quick扩展特性

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

常见问题(FAQ)

常见问题:连接失败

  • 🔑 公钥/私钥配置错误 → 检查 /etc/wireguard/*.key
  • 🔑 预共享密钥不一致 → 检查 /etc/wireguard/*.conf
  • 🌐 防火墙未放行 → 使用 ufw statusnft list ruleset
  • 📡 服务端未监听 → 使用 netstat -tulpn | grep 51820
# 检查防火墙规则
ufw status
nft list ruleset
iptables -L -v -n | grep 51820
firewall-cmd --list-all

# 检查服务端监听状态
netstat -tulpn | grep 51820

总结

WireGuard 是一个轻量级、安全、快速的 VPN 工具,适合用于内网穿透、混合网络、云计算等场景。