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

InterServer Web Hosting and VPS

WireGuard是一个易于配置、快速且安全的开源 VPN,它利用了最新的加密技术。目的是提供一种更快、更简单、更精简的通用 VPN,它可以轻松地在树莓派这类低端设备到高端服务器上部署。
WireGuard

如果家里网络没有公网 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

完成服务器端的配置

在正确安装 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

[Peer]
# Mac at home
PublicKey = (Mac public key here)
AllowedIPs = 192.0.2.2/32, 192.168.1.0/24

[Peer]
# iPhone
PublicKey = (iPhone public key here)
AllowedIPs = 192.0.2.3/32

在 WireGuard 中,你需要手动给各个设备分配 IP,并确保每个设备都有唯一的 IP。Interface 包含了当前设备的设置,对于 “服务端” 来说,ListenPort 是必须的。下面的每一个 Peer 段代表了能连接到本设备的一个其他设备。

配置文件保存后,我们可以使用 wg-quick up wg0 来启用配置文件。wg-quick 会自动配置路由表,无需我们手动设置。

记得放行 51820 UDP 端口。

家中 Mac 端的配置

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

[Interface]
PrivateKey = (private key of Mac)
Address = 192.0.2.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = (public key of server)
AllowedIPs = 192.0.2.0/24
Endpoint = (server ip address):51820
PersistentKeepalive = 10

更多内容请访问:https://i.nickyam.com/article/wireguard-vpn

  • 海报
  • 打赏
  • 分享
如何通过 WireGuard 搭建 VPN 访问家里内网插图1
如何通过 WireGuard 搭建 VPN 访问家里内网插图3
海报图正在生成中...
分享到...
如何通过 WireGuard 搭建 VPN 访问家里内网插图1
请选择打赏方式
如何通过 WireGuard 搭建 VPN 访问家里内网插图1
如何通过 WireGuard 搭建 VPN 访问家里内网插图4 如何通过 WireGuard 搭建 VPN 访问家里内网插图5
  • 微信
  • 支付宝

标签

发表评论