2022年2月

安装

装完需要重启,不然内核模块加载不上
内核大于5.6则默认集成了wireguard
centos8

sudo dnf install elrepo-release epel-release -y
sudo dnf install kmod-wireguard wireguard-tools -y

cd /etc/wireguard/
wg genkey | tee privatekey | wg pubkey > publickey

配置

服务端

根据之前的[[编程开发/主机运维/wireguard安装与使用]]配置好机器之后,在增加几个转发
10.10.10.1是wireguard的网段,192.168.50.0是家庭内网的网段

iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT # 放行 VPN 网段
iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT # 放行 VPN 转发到内网的流量
iptables -I FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT # 放行内网 转发到 VPN 的流量

配置文件修改为

[Interface]
ListenPort = 12000
Address = 10.10.10.1/24
PrivateKey = 当前机器的私钥
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

PostUp = iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT
PostUp = iptables -I FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT
PostUp = iptables -I FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT


PostDown = iptables -D FORWARD -s 10.10.10.1/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.10.10.1/24 -i wg0 -d 192.168.50.0/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 192.168.50.0/24 -i wg0 -d 10.10.10.1/24 -j ACCEPT

# arch-local
[Peer]
PublicKey = arch的公钥
AllowedIPs = 10.10.10.2/32,10.10.10.0/24, 192.168.50.0/24


# xps
[Peer]
PublicKey = xps的公钥
AllowedIPs = 10.10.10.5/32,10.10.10.0/24

# ios
[Peer]
PublicKey = ios的公钥
AllowedIPs = 10.10.10.4/32,10.10.10.0/24

如果客户端无法访问,需要关闭服务端的防火墙 systemctl disable firewall ; systemctl stop firewall
PostDownPostUp分别代表在启动和停止wg的时候执行的命令

客户端

内网中转机器

192.168.50.91 是中转机器的家庭内网IP
这里的私钥和公钥需要重新生成,不能重复,不然会导致无法链接

arch-local

[Interface]
PrivateKey = 当前机器的私钥
Address = 10.10.10.2/32

PostUp = iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source 192.168.50.91
PostDown = iptables -t nat -D POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source 192.168.50.91
PostDown = iptables -t nat -F

[Peer]
PublicKey = 外网机器的公钥
AllowedIPs = 10.10.10.0/24
Endpoint = 1.1.1.1:12000
PersistentKeepalive = 25

PersistentKeepalive = 25是定时保活

外网机器

xps

[Interface]
PrivateKey = 当前机器的私钥
Address = 10.10.10.5/32
DNS = 223.5.5.5,223.6.6.6

[Peer]
PublicKey = 外网机器的公钥
AllowedIPs = 10.10.10.0/24,192.168.50.0/24
Endpoint = 1.1.1.1:12000

添加到NetWorkManager

# nmcli con import type wireguard file /etc/wireguard/wg0.conf
Connection 'wg0' (21d939af-9e55-4df2-bacf-a13a4a488377) successfully added.

参考资料

使用 WireGuard 无缝接入内网 | Devld
系统运维|用 NetworkManager 配置 WireGuard 虚拟私有网络
https://www.reddit.com/r/WireGuard/comments/a0m8s1/vpn_lan_not_working_wg_shows_no_allowedips/
https://try.popho.be/wg.html
WireGuard dabblings
WireGuard (简体中文) - ArchWiki)