快速、現代和安全的 VPN 隧道
目的
在本教程中,您將了解 Wireguard 是什麼、它的用途、如何安裝和配置它,以及如何明智地使用它。
什麼是線衛?
Wireguard 是開源、可靠和先進的 VPN 隧道軟件,您可以安裝和使用開箱即用的方式來創建與服務器的安全點對點連接。
注意:更新:Ubuntu 20.04 的創建者已決定在最新版本的 Ubuntu 中包含具有 WireGuard VPN 服務功能的自定義內核。 WireGuard 現在是 Universe 存儲庫的一部分,因此您不再需要第三方存儲庫來安裝 Wireguard。可以使用命令“apt-get install wireguard-dkms -y”添加此軟件。
先決條件
- 您必須以具有 sudo 權限的用戶身份登錄,或者在系統上以 root 身份安裝 Wireguard 軟件。
- Wireguard 軟件安裝在 Ubuntu 18.04 服務器上。
服務器安裝
首先,添加 ppa:wireguard/wireguard 存儲庫。
[email protected]:~# add-apt-repository ppa:wireguard/wireguard WireGuard is a novel VPN that runs inside the Linux Kernel. This is the Ubuntu packaging for WireGuard. More info may be found at its website, listed below.
More info: https://www.wireguard.com/ Packages: wireguard wireguard-tools wireguard-dkms
Install with: $ apt install wireguard More info: https://launchpad.net/~wireguard/+archive/ubuntu/wireguard
Press [ENTER] to continue or Ctrl-c to cancel adding it.
Get:1 https://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:2 https://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:3 https://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic InRelease [2126 B]
Hit:5 https://deb.nodesource.com/node_10.x bionic InRelease
Get:6 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic InRelease [15.9 kB]
Get:7 https://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:9 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic/main amd64 Packages [3202 B]
Hit:8 https://packages.cloud.google.com/apt kubernetes-xenial InRelease
Get:10 https://us.archive.ubuntu.com/ubuntu bionic-updates/main i386 Packages [662 kB]
Get:11 https://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [897 kB]
Get:12 https://us.archive.ubuntu.com/ubuntu bionic-updates/universe i386 Packages [1012 kB]
Get:13 https://us.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1061 kB]
Get:14 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main i386 Packages [984 B]
Hit:15 https://packages.cisofy.com/community/lynis/deb stable InRelease
Get:16 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main amd64 Packages [984 B]
Get:17 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main Translation-en [764 B]
Fetched 3909 kB in 2s (1978 kB/s)
Reading package lists... Done
[email protected]:~#
然後您需要運行更新以確保新存儲庫被識別並添加到數據庫中。
[email protected]:~# apt-get update
Hit:2 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic InRelease
Hit:3 https://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:4 https://us.archive.ubuntu.com/ubuntu bionic InRelease
Hit:5 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic InRelease
Hit:6 https://us.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:7 https://us.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:1 https://packages.cloud.google.com/apt kubernetes-xenial InRelease
Hit:8 https://deb.nodesource.com/node_10.x bionic InRelease
Hit:9 https://packages.cisofy.com/community/lynis/deb stable InRelease
Reading package lists... Done
[email protected]:~#
最後,使用 apt-get 安裝 Wireguard。
[email protected]:~# apt-get install wireguard
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dkms wireguard-dkms wireguard-tools
Suggested packages:
menu
The following NEW packages will be installed:
dkms wireguard wireguard-dkms wireguard-tools
0 upgraded, 4 newly installed, 0 to remove and 129 not upgraded.
Need to get 417 kB of archives.
After this operation, 2388 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 dkms all 2.3-3ubuntu9.7 [68.1 kB]
Get:2 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main amd64 wireguard-dkms all 1.0.20200330-1ubuntu1~18.04 [253 kB]
Get:3 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main amd64 wireguard-tools amd64 1.0.20200319-0ppa1~18.04 [88.2 kB]
Get:4 https://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic/main amd64 wireguard all 1.0.20200319-0ppa1~18.04 [7980 B]
Fetched 417 kB in 1s (325 kB/s)
Selecting previously unselected package dkms.
(Reading database ... 115256 files and directories currently installed.)
Preparing to unpack .../dkms_2.3-3ubuntu9.7_all.deb ...
Unpacking dkms (2.3-3ubuntu9.7) ...
Selecting previously unselected package wireguard-dkms.
Preparing to unpack .../wireguard-dkms_1.0.20200330-1ubuntu1~18.04_all.deb ...
Unpacking wireguard-dkms (1.0.20200330-1ubuntu1~18.04) ...
Selecting previously unselected package wireguard-tools.
Preparing to unpack .../wireguard-tools_1.0.20200319-0ppa1~18.04_amd64.deb ...
Unpacking wireguard-tools (1.0.20200319-0ppa1~18.04) ...
Selecting previously unselected package wireguard.
Preparing to unpack .../wireguard_1.0.20200319-0ppa1~18.04_all.deb ...
Unpacking wireguard (1.0.20200319-0ppa1~18.04) ...
Setting up wireguard-tools (1.0.20200319-0ppa1~18.04) ...
Setting up dkms (2.3-3ubuntu9.7) ...
Setting up wireguard-dkms (1.0.20200330-1ubuntu1~18.04) ...
Loading new wireguard-1.0.20200330 DKMS files...
Building for 4.15.0-72-generic
Building initial module for 4.15.0-72-generic
Building initial module for 4.15.0-72-generic
Done.
wireguard:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/4.15.0-72-generic/updates/dkms/
depmod...
DKMS: install completed.
Setting up wireguard (1.0.20200319-0ppa1~18.04) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
[email protected]:~#
服務器配置
啟用內核模塊
其次,Wireguard 作為內核模塊運行,必須使用 modprobe 命令啟用。這將在每次編譯新內核時更新 Wireguard 內核模塊。
[email protected]:~# modprobe wireguard
[email protected]:~#
您可以通過運行以下命令來驗證內核模塊是否處於活動狀態:
[email protected]:~# lsmod | grep wireguard
wireguard 221184 0
ip6_udp_tunnel 16384 1 wireguard
udp_tunnel 16384 1 wireguard
[email protected]:~#
創建密鑰
現在您需要創建一個公鑰/私鑰對來保護您的連接。首先,使用 umask 命令設置密鑰文件的默認權限。
順便說一下,這裡有一些關於 umask 命令的附加信息: umask 命令(或用戶文件創建模式掩碼)用於確定新創建文件的文件權限設置。它還可用於控制對新文件設置的基本或默認文件權限。通常以 4 位表示法或八進製表示。 我使用 umask 將新文件的權限設置為 077 ,所以我想概述一下權限設置是如何計算的。
看表
小數量 | 反對 | 文件權限 |
0 | 所有者 | 讀、寫、執行 |
7 | 團體 | 拒絕訪問 |
7 | 其他 | 拒絕訪問 |
Wireguard 還創建稱為 wg0 和 wg1 的標準網絡接口,它們的行為類似於 eth0 或 eth1。這允許您以與 ipconfig 和 ip 命令處理標準網絡接口相同的方式處理這些接口。
權限設置
首先,使用 umask 077 命令設置新密鑰的權限:
[email protected]:~ # umask 077
[email protected]:~ #
接下來,生成加密數據傳輸所需的公鑰和私鑰。
[email protected]:~ # wg genkey | tee privatekey | wg pubkey > publickey
這將創建一個新密鑰並將其保存在當前目錄中。在這種情況下,我們在 /root 文件夾中運行命令,因此密鑰保存在那裡。您可以使用 cat 命令查看密鑰。 (注意:不要在這裡與任何人共享這些密鑰,它們只是示例密鑰。)
[email protected]:~ # cat privatekey
YMJHJqjyQIuro9uVVp3rPoSvD9i92Dw2r2mAt121q3A=
[email protected]:~ # cat publickey
Y5HkgP1jRSM9+Ti9CfpKfF/ZSZlQ0GFrRo3tlv+35gc=
[email protected]:~ #
創建配置文件
現在我們需要為 wg0 接口創建一個配置文件。首先 cd 到 /etc/wireguard 文件夾並使用以下命令訪問該文件:
[email protected]:~# cd /etc/wireguard/
[email protected]:/etc/wireguard# ll
total 8
drwx------ 2 root root 4096 Mar 20 07:00 ./
drwxr-xr-x 102 root root 4096 Apr 1 14:08 ../
[email protected]:/etc/wireguard# touch wg0.conf
[email protected]:/etc/wireguard#
下一步是使用 vim 編輯 wg0.conf 文件。
[email protected]:/etc/wireguard# vim wg0.conf
現在您需要復制以下基本配置設置並將它們複製到您的 conf 文件中。然後添加接口使用的您選擇的 IP 地址並將其放入配置文件中。
[Interface]
PrivateKey = YMJHJqjyQIuro9uVVp3rPoSvD9i92Dw2r2mAt121q3A
Address = 10.10.0.1/24
Address = fd86:ea04:1111::1/64
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens3 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ens3 -j MASQUERADE
ListenPort = 51820
wg0.conf文件中的上述術語定義如下:
- 私鑰: 該字段包含上面生成的私鑰。
- 地址 :這兩個IP地址值定義了WireGuard服務器的私有IPv4和IPv6地址。 VPN 網絡中的所有對等點都必須具有此設置的唯一值。
- 保存配置 :與其他 VPN 服務器不同,此值意味著客戶端無需重新啟動或重新加載 Wireguard 即可連接。 只需使用 wg 命令添加對等點,對等點就可以直接連接。此設置是“內存中”操作,因此重新啟動服務器將刪除添加的對等點。這就是 SaveConfig 設置發揮作用的地方。此值告訴 wg-quick 命令自動保存添加到配置文件的新對等點。
- 方法 / 發布: 此設置定義了啟用或禁用 wg0 接口時必須執行的附加步驟。在這種情況下,iptables 命令用於表示建議的 IP 規則,供客戶端共享服務器的主要 IPv4 或 IPv6 地址。當隧道被丟棄時,這些規則將被清除。
- 監聽端口 :此設置指定 WireGuard 偵聽傳入連接的端口。
conf 文件,您可以通過運行以下 sysctl 命令來啟用端口轉發:
[email protected]:~# sysctl -w net.ipv4.ip_forward=1
現在我們需要更改 wg0.conf 文件中的權限設置。
[email protected]:~# chmod -v 600 /etc/wireguard/wg0.conf
mode of '/etc/wireguard/wg0.conf' changed from 0644 (rw-r--r--) to 0600 (rw-------)
[email protected]:~#
更新防火牆規則
接下來,我們需要允許 SSH 連接,打開 WireGuard VPN 端口,最後在服務器上啟用防火牆。
[email protected]:/etc/wireguard# ufw allow 22/tcp
Rules updated
Rules updated (v6)
[email protected]:/etc/wireguard# ufw allow 51820/udp
Rules updated
Rules updated (v6)
[email protected]:/etc/wireguard# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
[email protected]:/etc/wireguard#
要檢查您的設置,請運行 ufw status 命令。
[email protected]:/etc/wireguard# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip
To Action From
-- ------ ----
80,443/tcp (Nginx Full) ALLOW IN Anywhere
22/tcp ALLOW IN Anywhere
51820/udp ALLOW IN Anywhere
80,443/tcp (Nginx Full (v6)) ALLOW IN Anywhere (v6)
22/tcp (v6) ALLOW IN Anywhere (v6)
51820/udp (v6) ALLOW IN Anywhere (v6)
[email protected]:/etc/wireguard#
啟動 Wireguard 服務
現在您可以使用以下命令啟動 Wireguard 服務:
[email protected]:/etc/wireguard# cd
[email protected]:~# wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.10.0.1/24 dev wg0
[#] ip -6 address add fd86:ea04:1111::1/64 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
[email protected]:~#
注意:“wg-quick”命令是“wg”命令使用的一些標準任務的便捷命令包裝器。例如,您可以使用以下命令啟用或禁用 wg0 接口:
[email protected]:~# wg-quick down wg0
[email protected]:~# wg-quick up wg0
接下來,啟用 Wireguard 服務以在服務器啟動時自動重啟。
[email protected]:~# systemctl enable [email protected]
Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /lib/systemd/system/[email protected]
[email protected]:~#
現在您可以驗證 Wireguard 服務是否正在運行。
[email protected]:~# wg show
interface: wg0
public key: Y5HkgP1jRSM9+Ti9CfpKfF/ZSZlQ0GFrRo3tlv+35gc=
private key: (hidden)
listening port: 51820
[email protected]:~#
您還可以使用 ifconfig 命令進行檢查。
[email protected]:~# ifconfig wg0
wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP> mtu 1420
inet 10.10.0.1 netmask 255.255.255.0 destination 10.10.0.1
inet6 fd86:ea04:1111::1 prefixlen 64 scopeid 0x0<global>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 1000 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[email protected]:~#
客戶端安裝
設置 Wireguard 客戶端與設置服務器非常相似,唯一的區別是配置文件。 有幾種方法可以將對等點(客戶端)添加到 Wireguard。
安裝 Ubuntu 客戶端
首先,您需要設置權限,然後在客戶端生成密鑰對。
[email protected]:~ # cd /etc/wireguard
[email protected]:~ # umask 077
[email protected]:~ # wg genkey | tee privatekey | wg pubkey > publickey
創建配置文件
現在我們需要為 wg0 接口創建一個配置文件。首先 cd 到 /etc/wireguard 文件夾並使用以下命令訪問該文件:
[email protected]:/etc/wireguard# ll
total 8
drwx------ 2 root root 4096 Mar 20 07:00 ./
drwxr-xr-x 102 root root 4096 Apr 1 14:08 ../
[email protected]:/etc/wireguard# touch wg0-client.conf
[email protected]:/etc/wireguard#
下一步是使用 vim 編輯本地 wg0-client.conf 文件。
[email protected]:/etc/wireguard# vim wg0-client.conf
使用 vim 打開文件後,您需要將以下基本配置設置複製並粘貼到新的 wg0-client.conf 文件中。然後添加所選接口使用的 IP 地址。這些設置應包含服務器的 IP 地址和 DNS 值,而不是 ListenPort、PostUp、PostDown 和 SaveConfig 值。
[Interface]
Address = 10.200.200.2/32
PrivateKey = <insert client_private_key>
DNS = 10.200.200.1
[Peer]
PublicKey = <insert server_public_key>
Endpoint = <insert vpn_server_address>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 21
最後,我們要保護文件,所以使用 chmod 更改文件的權限。
[email protected]:/etc/wireguard # chmod 600 /etc/wireguard/wg0-client.conf
接下來,我們需要在客戶端計算機上啟用 Wireguard 服務。
[email protected]:/etc/wireguard# systemctl enable [email protected]
wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip address add 10.10.0.2/32 dev wg0
[#] ip address add fd86:ea04:1111::2/128 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -6 route add ::/0 dev wg0 table 51820
[#] ip -6 rule add not fwmark 51820 table 51820
[#] ip -6 rule add table main suppress_prefixlength 0
[#] ip -4 route add 0.0.0.0/0 dev wg0 table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
檢查連接
要檢查連接詳細信息,請運行 wg 命令,然後運行 wg-quick save wg0 命令將設置保存到配置文件。
[email protected]:/etc/wireguard# wg
interface: wg0
public key: YMJHJqjyQIuro9uVVp3rPoSvD9i92Dw2r2mAt121q3A=
private key: (hidden)
listening port: 51820
peer: Y5HkgP1jRSM9+Ti9CfpKfF/ZSZlQ0GFrRo3tlv+35gc=
endpoint: 10.0.0.2:51820
allowed ips: 10.0.0.2/24, fd86:ea04:1115::/64
[email protected]:/etc/wireguard# wg-quick save wg0
然後您可以通過 ping 服務器來測試連接。
ping 10.0.0.1
latest handshake: 1 minute, 37 seconds ago
transfer: 192.66 KiB received, 143.08 KiB sent
最後,您需要能夠在啟動時運行 Wireguard。要將其添加到服務中,請運行以下命令:
[email protected]:/etc/wireguard # systemctl enable [email protected]
最後,您始終可以使用以下命令檢查您的連接:
[email protected]:/etc/wireguard# wg show
Windows 客戶端安裝
這是電線保護鏈接 用於 Windows 的 msi 安裝程序.
注意:Wireguard 開發人員建議僅使用經過批准的 Windows 客戶端。這是因為在使用其他客戶端時無法保證安全連接。
其他安裝選項
Wireguard 還為在多個平台上安裝軟件提供了更多選項。我們可以看到其他 電線護罩安裝選項 這裡。
結論是
總體而言,Wireguard 是一種有效、穩定且有用的工具,用於保護從本地客戶端到 VPN 服務器的連接。您學習瞭如何在服務器上安裝和配置 Wireguard,以及如何在本地計算機上安裝和配置客戶端。
你能幫助我嗎?
我們的支持團隊由對網絡託管技術(尤其是本文中討論的技術)有深入了解的人員組成。如果您對按照此處列出的步驟感到不自在,電話、聊天或只是售票可以幫助您完成整個過程。 致電 800.580.4985 或 和…聊天 或聯繫我們知識淵博的解決方案或經驗豐富的託管顧問,了解如何利用這些技術。