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

推荐订阅源

Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
腾讯CDC
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
B
Blog
S
SegmentFault 最新的问题
P
Privacy & Cybersecurity Law Blog
T
Threatpost
博客园 - 聂微东
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
C
Check Point Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
爱范儿
爱范儿
IT之家
IT之家
S
Secure Thoughts
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
有赞技术团队
有赞技术团队
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
雷峰网
雷峰网
Project Zero
Project Zero
博客园 - Franky
H
Heimdal Security Blog
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

杂烩饭

使用 RustFS 自建对象存储 使用cURL命令管理对象存储(s3) AGENTS.md Prometheus使用kubeconfig做k8s的service 自动发现监控 定制Windows系统镜像iso 使用create-dmg工具制作dmg安装包 使用create-dmg工具制作dmg安装包 - 技术栈 superpowers Nexus仓库搭建记录 Git Merge代码冲突的解决方式 在 Linux 系统中安装与配置 OpenVPN:从入门到精通 使用 Easytier-web 管理 easytier 节点 ingress-nginx 去除指定context path 在macOS上使用MusicPlayer2听本地音乐 Kubernetes 1.34 + RHEL10 + Cilium + kube-vip 高可用集群部署 使用腾讯云CLS收集TKE(k8s)业务日志 容器调试工具之BusyBox 无Docker环境进行容器镜像操作 使用kubeadm部署一套高可用k8s集群1.34 for Ubuntu 正在运行的Docker容器增加端口映射 在Kubernetes中部署一个单节点Elasticsearch 使用openssl制作自签名双向认证(mTLS)证书 minio自建对象存储 ingress-nginx 使用自定义的nginx配置 P12格式证书与PEM格式转换 使用blackbox-exporter做域名监控 记一次MySQL字符集转换导致的故障 使用双向认证增加git仓库安全性 Prometheus自动监控K8S集群中的service Grafana 查询SQL 自定义变量 使用selenium来实现Grafana的自动截图
轻量级组网工具WireGuard
张理坤 · 2025-09-04 · via 杂烩饭

要先打开服务器的内核转发:net.ipv4.ip_forward = 1
假设 WireGuard 自身的虚拟网段是 10.8.0.0/24, 给服务器分配的 IP 是:10.8.0.1,服务器的公网 IP 是:124.221.31.148

服务器基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14

sudo apt-get install -y wireguard


sudo yum install -y wireguard-tools


echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p


mkdir -p /etc/wireguard
cd /etc/wireguard
umask 077

生成公私钥对

先生成一些公私钥对,服务器需要 1 个,每个客户端 1 个。

1
2
3
4
5
6
7
8
9
10
11
12
13

wg genkey > server_privatekey
wg pubkey < server_privatekey > server_publickey


wg genkey > client1_privatekey
wg pubkey < client1_privatekey > client1_publickey
wg genpsk > client1_preSharedKey


wg genkey > client2_privatekey
wg pubkey < client2_privatekey > client2_publickey
wg genpsk > client2_preSharedKey

那么服务器的配置文件 wg0.conf:

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
27
28
29
echo "
# Server
[Interface]
PrivateKey = $(cat server_privatekey)
Address = 10.8.0.1/24
ListenPort = 51820
MTU = 1420
PreUp =
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostUp = iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT
PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PreDown =
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT

# Client 1
[Peer]
PublicKey = $(cat client1_publickey)
PresharedKey = $(cat client1_preSharedKey)
AllowedIPs = 10.8.0.2/32, 192.168.1.0/24

# Client 2
[Peer]
PublicKey = $(cat client2_publickey)
PresharedKey = $(cat client2_preSharedKey)
AllowedIPs = 10.8.0.3/32" > wg0.conf
  • AllowedIPs 表示这些 IP/段,发送到这个 Peer 上。

设置开机自启动:

1
systemctl enable --now wg-quick@wg0

Client 1 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
echo "
[Interface]
PrivateKey = $(cat client1_privatekey)
Address = 10.8.0.2/24
DNS = 1.1.1.1
MTU = 1420

[Peer]
PublicKey = $(cat server_publickey)
PresharedKey = $(cat client1_preSharedKey)
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 0
Endpoint = 124.221.31.148:51820 " > client1.conf
  • AllowedIPs 表示这些 IP/段,发送到这个 Peer 上。
  • Endpoint 是这个 Peer 的入口,这里配置的就是服务器的端口。

Client 2 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
echo "
[Interface]
PrivateKey = $(cat client2_privatekey)
Address = 10.8.0.3/24
DNS = 1.1.1.1
MTU = 1420

[Peer]
PublicKey = $(cat server_publickey)
PresharedKey = $(cat client2_preSharedKey)
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 0
Endpoint = 124.221.31.148:51820 " > client2.conf

常用操作

1
2
3
4
5
6
7
8
9
10
11

wg-quick up wg0


wg-quick down wg0


wg


wg-quick down wg0 && wg-quick up wg0 && wg

wg-quick 可以自动创建网卡

实际案例

从 client1 可以直接访问 client2 的虚拟 IP:根据 AllowIPs 配置,client1 的所有流量都会转发给 server,server 接收到请求 10.8.0.3 的流量,根据 server 的 AllowIPs 配置,转发给了 client2,回包同理。
image.png|776

如果想打通两个内网,需要将对方的内网 IP 段添加到 AllowIPs 列表里。并且在内网机器上添加路由。

名称 说明
机器 A IP 10.0.1.3/24
机器 B IP 10.0.2.3/24

机器 A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /etc/wireguard/wg0.conf on Server A
[Interface]
Address = 10.200.200.1/24
ListenPort = 51820
PrivateKey = <Server_A_Private_Key>
# Enable IP forwarding
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
# Server B
PublicKey = <Server_B_Public_Key>
AllowedIPs = 10.200.200.2/32, 10.0.2.0/24 # 关键:包含对端内网网段
Endpoint = 10.0.2.3:51820
PersistentKeepalive = 25

如果机器 A 是内网的网关,那么它就已经是默认路由了。否则机器 A 局域网内的其他机器需要手动添加一条路由规则:

1
2
3
4
5

sudo ip route add 10.0.2.0/24 via 10.0.2.3


route add 10.0.2.0 mask 255.255.255.0 10.0.2.3

机器 B

和 A 同理。

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 杂烩饭