如何在 Ubuntu 18 上安装 Wireguard

快速、现代且安全的 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 存储库。

  root@host:~# 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
 root@host:~# 

接下来,我们需要运行更新以确保我们的新存储库被识别并添加到数据库中。

root@host:~# 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
root@host:~#

最后,我们将使用 apt-get 安装 Wireguard。

root@host:~# 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) ...
root@host:~#

服务器配置

启用内核模块

接下来,由于 Wireguard 作为内核模块运行,我们需要使用 modprobe 命令启用。 这将使 Wireguard 内核模块能够在每次编译新内核时更新。

root@host:~# modprobe wireguard
root@host:~# 

我们现在可以通过运行以下命令来验证内核模块是否处于活动状态。

root@host:~# lsmod | grep wireguard
wireguard             221184 0
ip6_udp_tunnel         16384 1 wireguard
udp_tunnel             16384 1 wireguard
root@host:~#

密钥创建

现在,我们需要创建一个公钥和私钥对来保护连接。 我们将首先使用 umask 命令设置密钥文件的默认权限设置。

顺便说一句,这里有一些关于 umask 命令的附加信息。 umask 命令(或用户文件创建模式掩码)用于确定新创建文件的文件权限设置。 它还可用于控制对新文件设置的基本或默认文件权限。 它通常以四位符号或八进制值表示。 由于我们将使用 umask 将新文件的权限设置为 077,因此我们想概述如何计算权限设置。

查看表

少量针对文件权限
0 所有者 读、写和执行
7 团体 没有权限
7 其他 没有权限

Wireguard 还创建了一个名为 wg0 和 wg1 的标准网络接口,其功能与 eth0 或 eth1 类似。 这使我们可以像使用 ipconfig 和 ip 命令处理标准网络接口一样使用这些接口。

设置权限

首先,要设置新密钥的权限,我们将使用以下 umask 077 命令。

root@host:~  # umask 077  
root@host:~  #  

接下来,我们将生成加密数据传输所需的公钥和私钥。

 root@host:~  # wg genkey | tee privatekey | wg pubkey > publickey 

这将创建新密钥并将其保存到当前目录。 在我们的例子中,由于我们在 /root 文件夹中运行命令,所以我们的密钥存储在那里。 要查看密钥,我们可以使用 cat 命令。 (请注意:不要与我们在这里的任何人共享这些密钥。这些只是 example 键。)

root@host:~  # cat privatekey  
YMJHJqjyQIuro9uVVp3rPoSvD9i92Dw2r2mAt121q3A=  
root@host:~  # cat publickey  
Y5HkgP1jRSM9+Ti9CfpKfF/ZSZlQ0GFrRo3tlv+35gc=  
root@host:~  #

创建配置文件

现在,我们需要为 wg0 接口创建一个配置文件。 我们将首先 cd 进入 /etc/wireguard 文件夹,然后使用以下命令触摸该文件。

root@host:~# cd /etc/wireguard/  
root@host:/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 ../  
root@host:/etc/wireguard# touch wg0.conf  
root@host:/etc/wireguard#  

下一步是使用 vim 编辑 wg0.conf 文件。

root@host:/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 命令用于表示允许客户端共享服务器的主要 IPv4 或 IPv6 地址的 IP 规则。 一旦隧道被丢弃,这些规则就会被清除。
  • 监听端口 :此设置指定 WireGuard 将侦听传入连接的端口。

如果我们选择不在 conf 文件中包含防火墙规则,我们可以运行以下 sysctl 命令来启用端口转发。

root@host:~# sysctl -w net.ipv4.ip_forward=1 

现在,我们需要修改 wg0.conf 文件的权限设置。

root@host:~# chmod -v 600 /etc/wireguard/wg0.conf  
  mode of '/etc/wireguard/wg0.conf' changed from 0644 (rw-r--r--) to 0600 (rw-------)  
root@host:~# 

更新防火墙规则

接下来,我们需要允许 SSH 连接,打开 WireGuard VPN 端口,最后,在服务器上启用防火墙。

root@host:/etc/wireguard# ufw allow 22/tcp  
 Rules updated 
 Rules updated (v6) 
 root@host:/etc/wireguard# ufw allow 51820/udp 
 Rules updated 
 Rules updated (v6) 
 root@host:/etc/wireguard# ufw enable 
 Command may disrupt existing ssh connections. Proceed with operation (y|n)? y 
 Firewall is active and enabled on system startup 
root@host:/etc/wireguard# 

要验证我们的设置,我们可以运行 ufw status 命令。

root@host:/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) 
root@host:/etc/wireguard#

启动 Wireguard 服务

现在,我们可以使用以下命令启动 Wireguard 服务。

root@host:/etc/wireguard# cd 
root@host:~# 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 
root@host:~#  

注意:“wg-quick”命令是一个方便的命令包装器,用于与“wg”命令一起使用的几个标准任务。 为了 example,我们可以使用以下命令启用或禁用 wg0 接口。

root@host:~#  wg-quick down wg0  
root@host:~#  wg-quick up wg0 

接下来,我们将启用 Wireguard 服务以在服务器启动时自动重启。

root@host:~# systemctl enable wg-quick@wg0 
 Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /lib/systemd/system/[email protected]. 
root@host:~#  

现在,我们可以验证 Wireguard 服务是否正在运行。

root@host:~# wg show  
  interface: wg0  
    public key: Y5HkgP1jRSM9+Ti9CfpKfF/ZSZlQ0GFrRo3tlv+35gc=  
    private key: (hidden)  
    listening port: 51820  
root@host:~# 

此外,我们可以使用 ifconfig 命令进行检查。

root@host:~# 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  
root@host:~# 

客户端安装

Wireguard 客户端设置与服务器设置非常相似,唯一的区别在于配置文件。 有几种方法可以在 Wireguard 中添加对等点(客户端)。

Ubuntu 客户端安装

首先,我们需要设置权限,然后在客户端生成密钥对。

root@client:~  # cd /etc/wireguard 
root@client:~  # umask 077  
root@client:~  # wg genkey | tee privatekey | wg pubkey > publickey  

创建配置文件

现在,我们需要为 wg0 接口创建一个配置文件。 我们将首先 cd 进入 /etc/wireguard 文件夹,然后使用以下命令触摸该文件。

root@client:/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 ../  
root@client:/etc/wireguard# touch wg0-client.conf  
root@client:/etc/wireguard# 

下一步是使用 vim 来编辑我们本地的 wg0-client.conf 文件。

root@client:/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 修改文件的权限。

root@client:/etc/wireguard  #   chmod 600 /etc/wireguard/wg0-client.conf 

接下来,我们应该在客户端计算机上启用 Wireguard 服务

root@client:/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 命令将设置保存到我们的配置文件中。

root@client:/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 
root@client:/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 在启动时运行。 要将其添加到我们的服务中,我们将运行以下命令。

 root@client:/etc/wireguard  # systemctl enable wg-quick@wg0 

最后一点; 我们可以随时使用以下命令验证连接。

root@client:/etc/wireguard# wg show  

Windows 客户端安装

这是Wireguard的链接 用于 Windows 的 msi 安装程序.

注意:Wireguard 的开发人员建议仅使用经批准的 Windows 客户端,因为他们不保证使用任何其他客户端时连接的安全性。

其他安装选项

Wireguard 还提供了跨多个平台安装软件的更多选项。 我们可以查看其他 Wireguard 安装选项 这里。

结论

总体而言,Wireguard 是一种有效、稳定且有用的工具,可以保护从本地客户端到 VPN 服务器的连接。 我们学习了如何在服务器上安装和配置 Wireguard,以及如何在本地计算机上安装和配置客户端。

我们能帮你什么吗?

我们的支持团队充满了对网络托管技术有着深入了解的人才,尤其是本文中讨论的那些。 如果您对此处列出的步骤感到不舒服,我们只需一个电话、聊天或票证即可帮助您完成整个过程。 给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问之一交谈,了解您今天如何利用这些技术!