白菜

Recent content on 白菜

马上订阅 白菜 RSS 更新: https://blog.baicai.me/index.xml

通过 WireGuard 搭建 VPN 访问家里内网

admin@baicai.me (白菜)
2023年10月8日 15:19

家里网络没有公网 IP,因此需要一台具有公网 IP 的服务器作为 WireGuard 网络的“server”。家中需要有一台设备作为 WireGuard 网络中的节点。我们将使用手机,在 4G 网络下检查 VPN 是否搭建成功。

IP 段选择

WireGuard 组网需要使用一个不与你的任何设备的网络相冲突的 IP 地址段。像 192.0.2.0/24 、198.51.100.0/24 、203.0.113.0/24 这些分配为用于文档和示例中的“TEST-NET”,这些地址段通常不会被你需要连接的其他网络所使用。

在下面的配置中,我会分别将 192.0.2.1、192.0.2.2、192.0.2.3 分配给公网服务器、家中的 Mac 和 iPhone。

在服务器上配置 WireGuard

要使用 WireGuard,首先需要确保 Linux 内核支持。可使用 modinfo wireguard 命令检查是否内置了 WireGuard。也可用过 uname -r 检查内核版本是否为 5.6 以上。

安装 wireguard

Debian

apt install wireguard

其他系统参考:install

完成服务器端的配置

在正确安装 wireguard 后,你可以通过如下命令快速创建一组公钥和私钥。

$ wg genkey | tee peer_A.key | wg pubkey > peer_A.pub && cat peer_A.key && cat peer_A.pub

创建 /etc/wireguard/wg0.conf 并填配置

[Interface]
PrivateKey = (your server private key here)
Address = 192.0.2.1/24
ListenPort = 51820
#PreUp = echo WireGuard PreUp
#PostUp = iptables -A FORWARD...

剩余内容已隐藏

查看完整文章以阅读更多