WireGuard從Linux 5.6版本開始內(nèi)置于內(nèi)核,代碼量遠小于OpenVPN和IPsec,握手更快、延遲更低。Ubuntu 26.04的APT倉庫已直接提供該工具,無需額外源。
部署分三步:服務(wù)端配置、客戶端配置、啟用轉(zhuǎn)發(fā)。
![]()
第一步:安裝與密鑰生成
更新索引后安裝:
$ sudo apt update
$ sudo apt install wireguard -y
生成服務(wù)端密鑰對,私鑰權(quán)限設(shè)為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
查看默認網(wǎng)卡名稱(通常為eth0或ens3):
$ ip route | grep default
第二步:服務(wù)端配置
創(chuàng)建wg0.conf,寫入以下內(nèi)容:
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <粘貼服務(wù)端私鑰>
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需替換為實際網(wǎng)卡名。PostUp/PostDown規(guī)則負責NAT轉(zhuǎn)發(fā)和清理。
第三步:客戶端配置
客戶端同樣生成密鑰對:
$ wg genkey | tee client_private.key
$ cat client_private.key | wg pubkey | tee client_public.key
獲取服務(wù)端公鑰:
$ sudo cat /etc/wireguard/server_public.key
創(chuàng)建client.conf:
[Interface]
Address = 10.0.0.2/32
PrivateKey = <客戶端私鑰>
DNS = 8.8.8.8
[Peer]
PublicKey = <服務(wù)端公鑰>
Endpoint = 你的服務(wù)器IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
AllowedIPs設(shè)為0.0.0.0/0表示全局流量走隧道,PersistentKeepalive保持NAT穿透。
最后啟用服務(wù)端IP轉(zhuǎn)發(fā)并啟動wg0接口,客戶端導(dǎo)入配置即可建立加密隧道。
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務(wù)。
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.