在 Rocky Linux/Ubuntu/Debian 上安裝 WireGuard VPN 客戶端


本指南將向您展示如何在 Rocky Linux/Ubuntu/Debian 上安裝 WireGuard VPN 客戶端。如果您使用 WireGuard 作為 VPN 服務器,我們建議將您的端點配置為 VPN 客戶端。

請按照以下指南了解如何在 Rocky Linux 8 上配置 WireGuard VPN 服務器。

在 Rocky Linux 上安裝 WireGuard VPN 服務器

在 Rocky Linux/Ubuntu/Debian 上安裝 WireGuard VPN 客戶端

在 Rocky Linux 8 上安裝 WireGuard VPN 客戶端

通過運行以下命令在 Rocky Linux 8 上安裝 WireGuard VPN 客戶端:

dnf install elrepo-release epel-release -y
dnf install kmod-wireguard wireguard-tools -y

在 Ubuntu 上安裝 WireGuard VPN 客戶端

apt update
apt install wireguard resolvconf -y

在 Debian 上安裝 WireGuard VPN 客戶端

echo 'deb https://deb.debian.org/debian buster-backports main contrib non-free' > /etc/apt/sources.list.d/buster-backports.list
apt update
apt upgrade
apt install wireguard resolveconf -y

在 Rocky Linux/Debian/Ubuntu 上配置 WireGuard VPN 客戶端

為 WireGuard VPN 客戶端生成私鑰和公鑰

在之前的指南中,我們為三個測試客戶端生成了密鑰。

所以只需將對應的key複製到對應的客戶端即可。

[[ -d /etc/wireguard/ ]] || mkdir /etc/wireguard/

將客戶端密碼複製到相應的客戶端。

在 Debian 客戶端上

ls -1 /etc/wireguard/
debian.key
debian.pub.key
wireguard.pub.key

在 Ubuntu 客戶端上

ls -1 /etc/wireguard/
ubuntu.key
ubuntu.pub.key
wireguard.pub.key

在 Rocky Linux 客戶端上;

ls -1 /etc/wireguard/
rocky8.key
rocky8.pub.key
wireguard.pub.key

創建 WireGuard VPN 客戶端配置

在每個客戶端系統上,創建一個配置文件。只需通過運行以下命令創建配置文件:

相應地替換客戶端 IP 地址和私鑰。

Ubuntu 客戶端

cat > /etc/wireguard/wg0.conf << 'EOL'
[Interface]
PrivateKey = qJ2Sczxh8QWO5ZHlN+zZ4IaaMzmnMtgITLfQ0cam82M=
Address = 10.8.0.10
DNS = 8.8.8.8

[Peer]
PublicKey = 60UScq0EQ7ZHXIdHcOnjFYK6N/TLtmtPGTBqLwLd0WY=
AllowedIPs = 10.8.0.0/24
Endpoint = 192.168.60.19:51820
PersistentKeepalive = 20
EOL

例如,對於其他客戶。

cat > /etc/wireguard/wg0.conf << 'EOL'
[Interface]
PrivateKey = UMXEH1lTn7OF+fgBswsdDJU6NAu7N5or43FPWP1EyWY=
Address = 10.8.0.20
DNS = 8.8.8.8

[Peer]
PublicKey = 60UScq0EQ7ZHXIdHcOnjFYK6N/TLtmtPGTBqLwLd0WY=
AllowedIPs = 10.8.0.0/24
Endpoint = 192.168.60.19:51820
PersistentKeepalive = 20
EOL
cat > /etc/wireguard/wg0.conf << 'EOL'
[Interface]
PrivateKey = kIn6rA7W9MbGdZxRtziFN1DCJsqCi/hAdwhyH76cyU4=
Address = 10.8.0.30
DNS = 8.8.8.8

[Peer]
PublicKey = 60UScq0EQ7ZHXIdHcOnjFYK6N/TLtmtPGTBqLwLd0WY=
AllowedIPs = 10.8.0.0/24
Endpoint = 192.168.60.19:51820
PersistentKeepalive = 20
EOL

配置 WireGuard VPN 服務器以允許 VPN 客戶端連接

WireGuard VPN 服務器必須為 VPN 客戶端啟用對等連接。

要啟用此功能,您需要獲取每個客戶端的公鑰。例如,在我的設置中,我有 3 個客戶端已經生成了公鑰。

這已在 WireGuard VPN 服務器設置中完成。檢查下面的鏈接。

配置 WireGuard VPN 服務器以允許 VPN 客戶端連接

運行 WireGuard VPN 客戶端服務

WireGuard VPN 客戶端現在可以啟動該服務。

systemctl start [email protected]

檢查狀態。

systemctl status [email protected]
● [email protected] - WireGuard via wg-quick(8) for wg0
     Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
     Active: active (exited) since Sun 2021-07-04 06:35:16 UTC; 56s ago
       Docs: man:wg-quick(8)
             man:wg(8)
             https://www.wireguard.com/
             https://www.wireguard.com/quickstart/
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
    Process: 2016 ExecStart=/usr/bin/wg-quick up wg0 (code=exited, status=0/SUCCESS)
   Main PID: 2016 (code=exited, status=0/SUCCESS)

Jul 04 06:35:16 ubuntu20 systemd[1]: Starting WireGuard via wg-quick(8) for wg0...
Jul 04 06:35:16 ubuntu20 wg-quick[2016]: [#] ip link add wg0 type wireguard
Jul 04 06:35:16 ubuntu20 wg-quick[2016]: [#] wg setconf wg0 /dev/fd/63
Jul 04 06:35:16 ubuntu20 wg-quick[2016]: [#] ip -4 address add 10.8.0.10 dev wg0
Jul 04 06:35:16 ubuntu20 wg-quick[2016]: [#] ip link set mtu 1420 up dev wg0
Jul 04 06:35:16 ubuntu20 wg-quick[2016]: [#] ip -4 route add 10.8.0.0/24 dev wg0
Jul 04 06:35:16 ubuntu20 systemd[1]: Finished WireGuard via wg-quick(8) for wg0.

在所有客戶端上完成後,檢查 IP 地址分配。

ip add show wg0

烏班圖;

6: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
    link/none 
    inet 10.8.0.10/32 scope global wg0
       valid_lft forever preferred_lft forever

德比安;

3: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
    link/none 
    inet 10.8.0.20/32 scope global wg0
       valid_lft forever preferred_lft forever

Rocky Linux 客戶端。

5: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
    link/none 
    inet 10.8.0.30/32 scope global wg0
       valid_lft forever preferred_lft forever

檢查 VPN 服務器和客戶端之間的互連

現在嘗試 ping vpn 服務器。

ping 10.8.0.1 -c 3
PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=2.29 ms
64 bytes from 10.8.0.1: icmp_seq=2 ttl=64 time=2.06 ms
64 bytes from 10.8.0.1: icmp_seq=3 ttl=64 time=2.26 ms

--- 10.8.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 53ms
rtt min/avg/max/mdev = 2.057/2.202/2.291/0.110 ms

從另一個客戶端 ping 每個客戶端。

ping 10.8.0.10 -c 4
PING 10.8.0.10 (10.8.0.10) 56(84) bytes of data.
64 bytes from 10.8.0.10: icmp_seq=1 ttl=63 time=3.41 ms
64 bytes from 10.8.0.10: icmp_seq=2 ttl=63 time=3.63 ms
64 bytes from 10.8.0.10: icmp_seq=3 ttl=63 time=3.69 ms
64 bytes from 10.8.0.10: icmp_seq=4 ttl=63 time=3.67 ms

--- 10.8.0.10 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 83ms
rtt min/avg/max/mdev = 3.405/3.596/3.686/0.120 ms

我們關於如何在 Rocky Linux/Ubuntu/Debian 上安裝 WireGuard VPN 客戶端的指南到此結束。

在 Ubuntu 20.04/Ubuntu 18.04 上安裝 FortiClient VPN 客戶端。

如何使用 openvpn-monitor 工具監控 OpenVPN 連接