
要准备的
- 一台有公网 IP 的 Linux 服务器,能用 SSH 登录。
- 一台自己的电脑,装好 WireGuard。
- 十五分钟。
两边各生成一对密钥
服务器
sudo apt install wireguard
cd /etc/wireguard && umask 077
wg genkey | tee server.key | wg pubkey > server.pub
cat server.key server.pub电脑上点新建空隧道,窗口里会自动给出一对密钥。记住一条:私钥写在自己的 [Interface] 里,公钥写在对方的 [Peer] 里。
服务器的配置
/etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = 服务器的私钥
[Peer]
PublicKey = 电脑的公钥
AllowedIPs = 10.8.0.2/32电脑的配置
贴进新建的隧道里
[Interface]
PrivateKey = 电脑的私钥
Address = 10.8.0.2/32
[Peer]
PublicKey = 服务器的公钥
Endpoint = 服务器公网IP:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25放行端口,启动
服务器
sudo ufw allow 51820/udp
sudo systemctl enable --now wg-quick@wg0云服务器还要在控制台的安全组里放行 UDP 51820,很多人放成了 TCP。然后在电脑上启动隧道。
确认
- 看握手服务器
sudo wg show出现 latest handshake - ping 一下电脑上
ping 10.8.0.1有回应 - 看流量transfer 两个方向都不是 0
一定要用 51820 吗?
不用,任何 UDP 端口都行,两边写一致。
嫌手写麻烦?
用配置生成器,服务器和设备的配置一次出齐,密钥只在你的浏览器里产生。
下一封手机扫码