WireGuard从Linux 5.6版本开始内置于内核,代码量远小于OpenVPN和IPsec,握手更快、延迟更低。Ubuntu 26.04的APT仓库已直接提供该工具,无需额外源。
部署分三步:服务端配置、客户端配置、启用转发。
![]()
第一步:安装与密钥生成
更新索引后安装:
$ sudo apt update
$ sudo apt install wireguard -y
生成服务端密钥对,私钥权限设为600:
$ wg genkey | sudo tee /etc/wireguard/server_private.key
$ sudo chmod 600 /etc/wireguard/server_private.key
$ sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
查看默认网卡名称(通常为eth0或ens3):
$ ip route | grep default
第二步:服务端配置
创建wg0.conf,写入以下内容:
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <粘贴服务端私钥>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
eth0需替换为实际网卡名。PostUp/PostDown规则负责NAT转发和清理。
第三步:客户端配置
客户端同样生成密钥对:
$ wg genkey | tee client_private.key
$ cat client_private.key | wg pubkey | tee client_public.key
获取服务端公钥:
$ sudo cat /etc/wireguard/server_public.key
创建client.conf:
[Interface]
Address = 10.0.0.2/32
PrivateKey = <客户端私钥>
DNS = 8.8.8.8
[Peer]
PublicKey = <服务端公钥>
Endpoint = 你的服务器IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
AllowedIPs设为0.0.0.0/0表示全局流量走隧道,PersistentKeepalive保持NAT穿透。
最后启用服务端IP转发并启动wg0接口,客户端导入配置即可建立加密隧道。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.