基于树莓派的WireGuard安装配置与使用
基于樹莓派的WireGuard安裝配置與使用
關于WireGuard
簡介
WireGuard 是新一代的虛擬專用網協議,相比于 IPSec 和 OpenVPN 等軟件,特點是簡單、安全、高效,源碼總共不到四千行代碼。由于過于優秀,已經被吸收進了 Linux 5.6+ 的內核中。
工作原理
WireGuard 以 UDP 實現,但是運行在第三層 —— IP 層。每個 Peer 都會生成一個 wg0 虛擬網卡,同時服務端會在物理網卡上監聽 UDP 51820 端口。應用程序的包發送到內核以后,如果地址是虛擬專用網內部的,那么就會交給 wg0 設備,WireGuard 就會把這個 IP 包封裝成 WireGuard 的包,然后在 UDP 中發送出去,對方的 Peer 的內核收到這個 UDP 包后再反向操作,解包成為 IP 包,然后交給對應的應用程序。
WireGuard 項目官方網站 https://www.wireguard.com/
在服務器端部署WireGuard(本例為基于Oracle Cloud的Ubuntu 20.04)
WireGuard 跨平臺參照官方給出的快速安裝指南
https://www.wireguard.com/install/
注:如果安裝失敗請嘗試一下升級更新
sudo apt-get update sudo apt-get upgrade sudo apt-get install wireguard服務器端環境以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打印輸出私鑰
cat privatekey +Cr59JzbCKz9rESqimHGi5C2QfIRYZ5xVMssiTAEqV4=打印輸出公鑰
cat publickey bco6xIgfp++iFBj6vmDr27tAXfgYsppn/wyUJRcFgUc=編輯并保存 wg0 配置文件 wg0.conf
sudo vi wg0.confwg0.conf 配置文件內容如下:
Address = 10.200.200.1(可根據需要自行設定)
ListenPort = 監聽端口 51820(根據自己需求設定)
PrivateKey = 服務器私鑰
PublicKey = 連接節點公鑰(由客戶端生成)
AllowedIPs = VPN 隧道的內網 IP 段(必須與服務器自行設定的Address在同一網絡段內)
[Interface] Address = 10.200.200.1 ListenPort = 51820 PrivateKey = <服務端私鑰> ? [Peer] PublicKey = <客戶端公鑰> AllowedIPs = 10.200.200.2/32#如有多個客戶端,則只需要如上填寫對應連接節點公鑰與VPN隧道的內網IP段即可 [Peer] ……設置服務器的 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 //10.200.200.1為wg0.conf中服務器端自行設定的Address段,根據你的設定對應修改 sudo ip address add dev wg0 10.200.200.1/24 sudo ip link set wg0 up sudo wg setconf wg0 /etc/wireguard/wg0.conf //以上四條命令也可直接用'sudo wg-quick up wg0'代替執行 #設置IP和iptables規則 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 //'eth0'為服務器本地網卡名稱,可用'ifconfig'命令查看 sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE注:iptables設置不完全僅包含以上指令,需執行'sudo iptables -L'查看當前防火墻狀態,如有對應端口未打開或拒絕icmp報文訪問等規則,需要自行打開或刪除,否則wireguard將不能正常使用,現將本例使用的服務器防火墻INPUT和FORWARD鏈粘貼如下
ubuntu@instance-20210821-wg:/etc/wireguard$ sudo iptables -L -n --line-number Chain INPUT (policy ACCEPT) num target ? ? prot opt source ? ? ? ? ? ? ? destination ? ? ? ? 1 ? ACCEPT ? ? icmp -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 2 ? ACCEPT ? ? all -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 3 ? ACCEPT ? ? udp -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? udp spt:123 4 ? ACCEPT ? ? tcp -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:22 5 ? ACCEPT ? ? udp -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? udp dpt:51820 ctstate NEW ? Chain FORWARD (policy ACCEPT) num target ? ? prot opt source ? ? ? ? ? ? ? destination ? ? ? ? 1 ? ACCEPT ? ? all -- 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ?另:服務器使用的端口一般還需在后臺打開;阿里云及騰訊云的國內服務器也可能會出現不能正常使用wireguard的情況
配置完畢后,可用以下指令保存防火墻設置
apt-get install iptables-persistent systemctl enable netfilter-persistent systemctl start netfilter-persistent netfilter-persistent save檢查wg設置是否正常
sudo wg show正常情況下應該輸出以下內容
interface: wg0public key: <服務端公鑰>private key: (hidden)listening port: 51820 ? peer: <客戶端公鑰>//此為連接節點的IP及端口,不一定與下述情況一致endpoint: 12.34.56.78:61353allowed ips: 10.200.200.2/32//客服端啟動完畢后才會出現以下兩條信息latest handshake: 0 days, 8 minutes, 19 seconds agotransfer: 0.60 GiB received, 0.93 GiB sent服務器端設置完成
另:可執行以下命令,設置為開機自動啟動
sudo systemctl enable wg-quick@wg0.service在客戶端部署WireGuard
生成公鑰、私鑰
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.conf 配置文件內容如下
ListenPort = 監聽端口 51820
PrivateKey = 本地客戶端私鑰
PublicKey = 服務器端公鑰(由服務器端生成)
AllowedIPs = VPN 隧道的內網 IP 段
Endpoint = 遠程服務器的公網 IP(即ssh連接服務器時用的IP地址) 和端口
PersistentKeepalive = 連接心跳包的喚醒間隔(可不設置)
[Interface] Address = 10.200.200.2/32 ListenPort = 51820 PrivateKey = <客戶端私鑰> ? [Peer] PublicKey = <服務端公鑰> AllowedIPs = 0.0.0.0/0 Endpoint = 150.136.105.97:51820 PersistentKeepalive = 25在客戶端,添加虛擬網卡 wg0
sudo ip link add dev wg0 type wireguard //10.200.200.2為wg0.conf中客戶端自行設定的Address段,根據你的設定對應修改 sudo ip address add dev wg0 10.200.200.2/32 sudo ip link set wg0 up sudo wg setconf wg0 /etc/wireguard/wg0.conf //以上命令也可由sudo wg-quick up wg0代替設置 IP 和路由
這一部分根據各自路由情況的不同,執行的命令也有所不同,但是一般來說默認的路由表是可以直接使用的,不需要配置;如不能正常使用,則使用'ip route'+'add或del',自行更改至可使用的狀態即可,以下粘貼出本例使用到的樹莓派(IP為192.168.3.242)的路由情況
pi@raspberrypi:~ $ route -n Kernel IP routing table Destination ? ? Gateway ? ? ? ? Genmask ? ? ? ? Flags Metric Ref ? Use Iface 0.0.0.0 ? ? ? ? 0.0.0.0 ? ? ? ? 0.0.0.0 ? ? ? ? U ? ? 0 ? ? 0 ? ? ? 0 wg0 150.136.105.97 192.168.3.1 ? ? 255.255.255.255 UGH ? 0 ? ? 0 ? ? ? 0 eth0 192.168.3.0 ? ? 0.0.0.0 ? ? ? ? 255.255.255.0 ? U ? ? 202 ? 0 ? ? ? 0 eth0注:150.136.105.97為服務器的公網IP
客戶端設置完成,Ping 測試 VPN 隧道
ping 10.200.200.1ping輸出如下
PING 10.200.200.1 (10.200.200.1) 56(84) bytes of data. 64 bytes from 10.200.200.1: icmp_seq=1 ttl=64 time=253 ms 64 bytes from 10.200.200.1: icmp_seq=2 ttl=64 time=253 ms 64 bytes from 10.200.200.1: icmp_seq=3 ttl=64 time=252 ms ^C --- 10.200.200.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 5ms rtt min/avg/max/mdev = 251.782/252.699/253.364/0.886 ms說明 VPN 隧道已通
用 curl 命令測試隧道的流量轉發狀態
curl ifconfig.me顯示 IP 為服務器的公網 IP
150.136.105.97curl 獲取到了服務器的公網 IP,流量轉發成功。
參考在 Ubuntu 部署 VPN 隧道 WireGuard — Steemit
有任何問題歡迎留言,本人看到后會第一時間幫忙解決,非常感謝觀看,祝配置順利。另外轉載請注明出處,謝謝。
總結
以上是生活随笔為你收集整理的基于树莓派的WireGuard安装配置与使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吾爱破解160个crackme之006
- 下一篇: css preserve-3d 使用