交流評論、關注點贊

  • Facebook Icon臉書專頁
  • telegram Icon翻牆交流電報群
  • telegram Icon電報頻道
  • RSS訂閱禁聞RSS/FEED訂閱

在 Ubuntu 部署 VPN 隧道 WireGuard

2018年08月09日 6:40 PDF版 分享轉發

來源: https://steemit.com/cn/@curl/ubuntu--wireguard
內容修訂:https://github.com/aturl/awesome-anti-

關於 WireGuard

WireGuard 是簡單、快速、高效並且安全的開源 VPN 軟體,它採用先進的加密協議,基於 Linux 內核實現。

WireGuard 項目官方網站 https://www.wireguard.com/

WireGuard 源代碼由開發者自我託管,代碼倉庫 https://git.zx2c4.com/WireGuard/

WireGuard 源代碼在 GitHub 的鏡像倉庫 https://github.com/WireGuard

在伺服器端部署 WireGuard

WireGuard 跨平台,參照官方給出的快速安裝指南 https://www.wireguard.com/install/

伺服器環境以 Ubuntu 系統為例

生成公鑰、私鑰、共享密鑰

sudo mkdir -p /etc/wireguard && sudo chmod 0777 /etc/wireguard && cd /etc/wireguard umask 077 wg genkey | tee privatekey | wg pubkey > publickey | wg genpsk > presharedkey 

列印輸出私鑰

Ad:美好不容錯過,和家人朋友一起享受愉快時光,現在就訂票
cat privatekey +Cr59JzbCKz9rESqimHGi5C2QfIRYZ5xVMssiTAEqV4= 

列印輸出公鑰

cat publickey bco6xIgfp++iFBj6vmDr27tAXfgYsppn/wyUJRcFgUc= 

列印輸出共享密鑰(可以不生成,配置文件中不是必須的)

cat presharedkey Vv0MdBNolqbnsBPQPf0ttJecOw2QC8QqWBVieNtvoIo= 

編輯並保存 wg0 配置文件 wg0.conf

sudo vi wg0.conf 

wg0.conf 配置文件內容如下:

ListenPort = 監聽埠 1194

PrivateKey = 伺服器私鑰

PrivateKey = 連接節點公鑰(由客戶端生成)

AllowedIPs = VPN 隧道的內網 IP 段

[Interface] ListenPort = 1194 PrivateKey = +Cr59JzbCKz9rESqimHGi5C2QfIRYZ5xVMssiTAEqV4= [Peer] PublicKey = 1lq23n/oNwYosnf0xMadtrIcC+droND9fg/wiS0aEhw= AllowedIPs = 10.100.0.1/24 

設置伺服器的 NAT 流量轉發

sudo vi /etc/sysctl.conf 

找到這一行 #net.ipv4.ip_forward = 1 去掉註釋符 「#」

net.ipv4.ip_forward = 1 

執行 sysctl 並生效

sudo sysctl -p 

在伺服器端添加虛擬網卡 wg0,設置隧道 IP 和 iptables 規則

sudo ip link add dev wg0 type wireguard sudo ip address add dev wg0 10.100.0.1/24 sudo ip link set wg0 up sudo wg setconf wg0 /etc/wireguard/wg0.conf sudo iptables -A FORWARD -i wg0 -j ACCEPT sudo iptables -A FORWARD -o wg0 -j ACCEPT sudo iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE sudo iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE 

檢查 wg 設置是否正常

sudo wg show 

正常情況應該輸出以下內容:

interface: wg0 public key: 1lq23n/oNwYosnf0xMadtrIcC+droND9fg/wiS0aEhw= private key: (hidden) listening port: 1194 peer: 8+9jGlxuwyGUWtUk8/NZMAl1Ax577KAvnXJY0EeuNkQ= endpoint: 12.34.56.78:61353 allowed ips: 10.100.0.0/24 latest handshake: 0 days, 8 minutes, 19 seconds ago transfer: 0.60 GiB received, 0.93 GiB sent 

伺服器端設置完成。


設置客戶端

客戶端系統 Ubuntu/Debian/ArchLinux 參照官方給出的快速安裝指南 https://www.wireguard.com/install/

sudo mkdir -p /etc/wireguard && sudo chmod 077 /etc/wireguard && cd /etc/wireguard umask 077 wg genkey | tee privatekey | wg pubkey > publickey 

列印輸出私鑰

cat privatekey SHBjWA3uAYAZc+TUvr6PcTA5SVQnt+aSVkdlZhlg1Hk= 

列印輸出公鑰

cat publickey 8+9jGlxuwyGUWtUk8/NZMAl1Ax577KAvnXJY0EeuNkQ= 

編輯並保存 wg0 配置文件 wg0.conf

sudo vi wg0.conf 

wg0.conf 配置文件內容如下:

ListenPort = 監聽埠 1194

PrivateKey = 本地客戶端私鑰

PrivateKey = 伺服器端公鑰(由伺服器端生成)

AllowedIPs = VPN 隧道的內網 IP 段

Endpoint = 遠程伺服器公網 IP 和埠

PersistentKeepalive = 連接心跳包的喚醒間隔

[Interface] ListenPort = 1194 PrivateKey = SHBjWA3uAYAZc+TUvr6PcTA5SVQnt+aSVkdlZhlg1Hk= [Peer] PublicKey = bco6xIgfp++iFBj6vmDr27tAXfgYsppn/wyUJRcFgUc= AllowedIPs = 0.0.0.0/0 Endpoint = 12.34.56.78:1194 PersistentKeepalive = 25 

在本地客戶端

在客戶端,添加虛擬網卡 wg0 並設置 IP 和路由

sudo ip link add dev wg0 type wireguard sudo ip address add dev wg0 10.100.0.101/24 sudo ip link set wg0 up sudo wg setconf wg0 /etc/wireguard/wg0.conf sudo ip route add 12.34.56.78 via 192.168.1.1 sudo ip route del default sudo ip route add default dev wg0 

客戶端設置完成,Ping 測試 VPN 隧道

ping 10.100.0.1 

ping 輸出如下

PING 10.100.0.1 (10.100.0.1) 56(84) bytes of data. 64 bytes from 10.100.0.1: icmp_seq=1 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=2 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=3 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=4 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=5 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=6 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=7 ttl=44 time=103.50 ms 64 bytes from 10.100.0.1: icmp_seq=8 ttl=44 time=103.50 ms ^C --- 10.100.0.1 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 828ms rtt min/avg/max/mdev = 103.50/103.50/103.50/0.00 ms 

說明 VPN 隧道已通

用 curl 命令測試隧道的流量轉髮狀態

curl ifconfig.me 

顯示 IP 為伺服器的公網 IP

12.34.56.78 

curl 獲取到了伺服器的公網 IP,流量轉發成功

Disable WireGuard,禁用 WireGuard

刪除添加的虛擬網卡、IP 和路由,或者重啟系統,將恢復默認設置

伺服器端

sudo ip link del dev wg0 

本地客戶端

sudo ip link del dev wg0 sudo ip route del 12.34.56.78 via 192.168.1.1 sudo ip route del default sudo ip route add default via 192.168.1.1 

其他

WireGuard 設計原理和使用方法參考 https://www.wireguard.com/

WireGuard 支持各種 Linux 發行版,目前 和 macOS 客戶端(Go 實現)以及 Android 應用都在開發中。

WireGuard 在數字權利遭受強權政府日益侵害的國家,能有效捍衛網路用戶的數字權利。

WireGuard 在中國、伊朗等網路審查嚴重的國家,能有效,突破 GFW 封鎖,加密訪問被封鎖的網站,自由使用

喜歡、支持,請轉發分享↓Follow Us 責任編輯:林遠翔