WireGuard 入门
WireGuard是一种相对较新的 VPN 隧道协议,旨在实现快速且易于设置。它严格遵循Unix 哲学,只做一件事(创建安全的 VPN 隧道),并且做得很好。
如果您曾经设置过 VPN 服务(例如OpenVPN),那么您就会知道它可能会非常复杂,因为您必须完成许多步骤,例如生成证书颁发机构、颁发服务器和客户端密钥及证书、设置多个配置文件、配置防火墙规则、设置路由流量转发等等,这些步骤可能令人恐惧,甚至令人望而生畏。WireGuard 正在改变这一切,它简化了快速启动和运行的过程,并允许轻松配置以连接多个客户端(对等体)。
为什么要使用 WireGuard?
- VPN 可帮助您免受中间人攻击。
- 保护您的隐私,防止 ISP 窥探您的流量。
- 绕过各国的互联网审查。
WireGuard 相对于其他 VPN 的优势:
- 它是基于内核的;性能有所提高。
- 在不到 100 毫秒的时间内建立连接。
- 占用空间小;几乎可以在任何设备(即嵌入式设备)中运行。
- 易于配置和部署为 SSH;由于复杂性较低,因此减少了攻击面。
- 使用现代和改进的加密标准。
- 每隔几分钟进行一次简单的握手以确保连接的保密性。
- 支持 IP 漫游,这意味着您可以更换 Wi-Fi 网络,或断开 Wi-Fi 或蜂窝网络,VPN 隧道连接也不会丢失。一切正常!
我们将讨论的内容
这篇文章假设您之前从未安装过 VPN 服务,并且我们将使用 Ubuntu 机器,因为它是最流行的发行版。
这篇文章相当冗长!但如果你已经熟悉这些概念,你可以跳到TLDR;查看最终使用的脚本和配置文件。
本文概述的步骤如下:
- 设置服务器
- 在服务器上安装 WireGuard
- 生成服务器密钥
- 创建服务器配置文件
- 在服务器上启用 IP 转发
- 在客户端安装 WireGuard
- 生成客户端密钥
- 创建客户端配置文件
- 在服务器配置上设置客户端信息
- 在服务器上启动 WireGuard 服务
- 在客户端启动 WireGuard 服务
- 将移动客户端连接到服务器
请注意,在 WireGuard 领域中,不存在传统意义上的“服务器”和“客户端”。相反,相互连接的计算机和设备被称为“对等体”。为了简单起见,我们将使用“服务器”来表示将所有流量转发到的托管服务器,并使用“客户端”来表示将所有流量转发到服务器的家用计算机。
设置服务器
我将使用 AWS 的免费 EC2 微型实例进行示例(之后会将其拆除)。如果您有 AWS 账户,可以通过以下方式启动新实例:
EC2 → 启动实例 → Ubuntu 上的 t2.micro → 审核并启动 → 启动
在这个例子中,我运行的是 Ubuntu 18.04(Bionic Beaver)。
在服务器上安装 WireGuard
要在 Ubuntu <19.04 上安装 wireguard,请运行以下命令:
sudo add-apt-repository ppa:wireguard/wireguard
sudo apt-get update
sudo apt-get install wireguard
如果您的服务器使用不同的发行版,请查看 WireGuard安装说明。
ubuntu@ip-172-30-0-233:~$ sudo 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.
<truncated>
Fetched 18.5 MB in 4s (4840 kB/s)
ubuntu@ip-172-30-0-233:~$ sudo apt-get update
Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:5 http://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic InRelease
Reading package lists... Done
ubuntu@ip-172-30-0-233:~$ sudo apt-get install wireguard
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
<truncated>
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Setting up build-essential (12.4ubuntu1) ...
Setting up wireguard-dkms (1.0.20200401-1ubuntu1~18.04) ...
Loading new wireguard-1.0.20200401 DKMS files...
Building for 4.15.0-1057-aws
Building initial module for 4.15.0-1057-aws
Done.
wireguard:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/4.15.0-1057-aws/updates/dkms/
depmod...
DKMS: install completed.
Setting up wireguard (1.0.20200319-0ppa1~18.04) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
让我们像下面这样启动一个 shell,root
以sudo -s
避免从现在开始每次都输入 sudo:
ubuntu@ip-172-30-0-233:~$ sudo -s
root@ip-172-30-0-233:~#
运行wg
检查安装是否成功,如果一切正常,则不会输出任何内容:
root@ip-172-30-0-233:/etc/wireguard/keys# wg
root@ip-172-30-0-233:/etc/wireguard/keys#
我们将使用的两个 WireGuard 命令是:
wg
用于配置 WireGuard 接口。wg-quick
用于启动和停止 WireGuard VPN 隧道。
生成服务器密钥
WireGuard 配置文件将位于下面,/etc/wireguard/
因此让我们创建一个名为keys
该目录的目录来存储我们将生成的密钥:
root@ip-172-30-0-233:~# mkdir /etc/wireguard/keys
进入/etc/wireguard/keys/
目录:
root@ip-172-30-0-233:~# cd /etc/wireguard/keys
077
通过运行 ,将目录用户掩码设置为umask 077
。umask 为 077 时,文件所有者(本例中为 root)将获得读、写和执行权限,但其他所有人则被禁止读、写和执行,以确保凭证不会在竞争条件下泄露:
root@ip-172-30-0-233:/etc/wireguard/keys# umask 077
WireGuard 使用非对称公钥/私钥 Curve25519 密钥对进行客户端和服务器之间的身份验证。
使用wg genkey
命令生成私钥。我们可以通过将私钥输出通过管道传输到tee
文件来同时生成私钥和公钥,也可以将私钥转发到wg publickey
从私钥派生公钥的文件中,并将其保存到文件中。
因此运行的命令是wg genkey | tee privatekey | wg pubkey > publickey
立即生成密钥对:
root@ip-172-30-0-233:/etc/wireguard/keys# wg genkey | tee privatekey | wg pubkey > publickey
如果我们这样做,ls
我们会看到有一个privatekey
和publickey
文件:
root@ip-172-30-0-233:/etc/wireguard/keys# ls
privatekey publickey
输出私钥文件的内容向我们展示了它以 base64 格式生成的随机密钥:
root@ip-172-30-0-233:/etc/wireguard/keys# cat privatekey
wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
同样,从私钥派生出的公钥也是 base64 格式:
root@ip-172-30-0-233:/etc/wireguard/keys# cat publickey
H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=
我们需要 WireGuard 服务器配置的私钥和客户端配置的公钥。
创建服务器配置文件
进入/etc/wireguard/
目录并创建一个新文件wg0.conf
。WireGuard 将创建一个与文件名同名的新网络接口,因此通常将第一个 WireGuard 网络接口表示为wg0
上下文:
root@ip-172-30-0-233:/etc/wireguard# touch /etc/wireguard/wg0.conf
在您喜欢的编辑器中打开服务器配置文件/etc/wireguard/wg0.conf
:
root@ip-172-30-0-233:/etc/wireguard# vim /etc/wireguard/wg0.conf
将以下配置粘贴到新的配置文件中:
[Interface]
PrivateKey = <server private key>
Address = 10.0.0.1/24
ListenPort = 51820
配置文件采用标准INI格式。
将值替换PrivateKey
为您之前生成的私钥内容:
[Interface]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
Address = 10.0.0.1/24
ListenPort = 51820
选择该地址10.0.0.1
是因为它是服务器上可用的私有子网。如果您的服务器已使用该 IP 范围,请选择其他地址以192.168.2.1
避免冲突。
总而言之,服务器[Interface]
部分用于配置我们正在创建的新 WireGuard 接口。
PrivateKey
是您的服务器的私钥。Address
是我们为该网络接口分配的私有网络 IP 地址范围。ListenPort
是运行服务的主机端口。此端口需要公开访问。该端口51820
是默认端口。
确保启用端口51820
以进行UDP
流量控制。如果使用 EC2,则应在 EC2 实例的安全组下允许该端口。
EC2 实例 → 安全组 → 单击安全组 → 编辑入站规则 → 添加规则 → 自定义 UDP → 端口范围:51820 → 来源:任何地方 → 保存规则
本规则立即生效。
如果您的服务器位于 NAT 后面,则所有流量都需要从默认接口转发到 WireGuard 接口。
要找出默认接口的名称,请运行ip route
:
root@ip-172-30-0-233:/etc/wireguard/keys# ip route | grep default | awk '{print $5}'
eth0
PostUp
现在使用和配置设置在服务器配置文件中添加转发规则,PostDown
其中PostUp
在 WireGuard 服务启动时运行 value 命令,PostDown
在服务关闭时运行 value 命令。
[Interface]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
这三个 iptable 规则是:
iptables -A FORWARD -i %i -j ACCEPT
用于允许接口接收入站流量。iptables -A FORWARD -o %i -j ACCEPT
允许从接口出站流量。iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
用于使用默认接口的外部 IP 地址来掩盖接口的私有 IP 地址。
在服务器上启用 IP 转发
默认情况下,IP 转发是禁用的,这意味着如果接口收到非预期的数据包,它将被拒绝。由于我们需要将数据包从一个接口传递到另一个接口,因此我们需要允许 IP 转发。
打开文件/etc/sysctl.conf
进行编辑:
root@ip-172-30-0-233:/etc/wireguard# vim /etc/sysctl.conf
通过取消注释第 28 行附近的行来允许转发 IP 数据包net.ipv4.ip_forward=1
:
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
运行sysctl -p
以使更改生效,无需重新启动:
root@ip-172-30-0-233:/etc/wireguard# sysctl -p
net.ipv4.ip_forward = 1
/proc/sys/net/ipv4/ip_forward
通过输出应返回的内容来确认 IP 转发已启用1
:
root@ip-172-30-0-233:/etc/wireguard# cat /proc/sys/net/ipv4/ip_forward
1
服务器几乎已完全配置完毕。现在只缺少客户端的信息,所以接下来让我们设置客户端。
在客户端安装 WireGuard
返回到您的客户端计算机并安装 WireGuard。我的客户端计算机运行的是 Arch Linux,但大多数 Linux 发行版的安装过程都相同。如果您在客户端上运行的是 Ubuntu,请执行与上述服务器上相同的安装步骤,或者查看官方的 WireGuard安装说明。
Ubuntu WireGuard 安装说明:
$ sudo add-apt-repository ppa:wireguard/wireguard
$ sudo apt-get update
$ sudo apt-get install wireguard
如果像我一样运行 Arch,那么这些是 WireGuard 安装说明:
$ sudo pacman -S wireguard-tools wireguard-dkms
让我们像下面这样启动一个 shell,root
以sudo -s
避免从现在开始每次都输入 sudo:
$ sudo -s
[root@archlinux ~]#
生成客户端密钥
在客户端生成 WireGuard 密钥的过程与在服务器上生成的过程相同。创建目录/etc/wireguard/keys
并将用户掩码设置为077
。
[root@archlinux ~]# mkdir /etc/wireguard/keys
[root@archlinux ~]# cd /etc/wireguard/keys
[root@archlinux keys]# umask 077
使用与在服务器上相同的命令为客户端生成私钥和公钥对:
[root@archlinux keys]# wg genkey | tee privatekey | wg pubkey > publickey
输出我们配置文件中即将需要的关键内容:
[root@archlinux keys]# cat privatekey
cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=
[root@archlinux keys]# cat publickey
vi4TCAo8TNRkpf4ZpiMsp3YHaOLrcouSDkrm4wJxezw=
创建客户端配置文件
在客户端创建配置/etc/wireguard/wg0.conf
:
[root@archlinux keys]# vim /etc/wireguard/wg0.conf
将配置粘贴到您的客户端配置文件中:
[Interface]
Address = 10.0.0.2/32
PrivateKey = <client private key>
PrivateKey
用客户端的私钥替换该值:
[Interface]
Address = 10.0.0.2/32
PrivateKey = cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=
您也可以选择设置要使用的 DNS 解析器。我们将 DNS 解析器 IP 设置为 Cloudflare 的公共 DNS 解析器,1.1.1.1
该解析器快速且安全:
[Interface]
Address = 10.0.0.2/32
PrivateKey = cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=
DNS = 1.1.1.1
总而言之,客户端[Interface]
部分用于配置我们正在创建的新 WireGuard 接口。
Address
是我们为该网络接口分配的私有网络 IP 地址范围。PrivateKey
是您的客户的私钥。DNS
是要使用的 DNS 解析。
在客户端配置上设置服务器对等体
下一步是在客户端配置文件的以下[Peer]
部分中设置有关服务器的信息:
[Interface]
Address = 10.0.0.2/32
PrivateKey = cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=
DNS = 1.1.1.1
[Peer]
PublicKey = <server public key>
Endpoint = <server public ip>:51820
AllowedIPs = 0.0.0.0/0
将值替换PublicKey
为您的服务器的公钥,并将其设置Endpoint
为您的服务器的公共 IP 地址:
[Interface]
Address = 10.0.0.2/32
PrivateKey = cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=
DNS = 1.1.1.1
[Peer]
PublicKey = H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=
Endpoint = 54.225.123.18:51820
AllowedIPs = 0.0.0.0/0
如果您的服务器位于 NAT 后面,无法通过公网 IP 访问,则需要在 peer 部分进行设置PersistentKeepalive
以保持连接。务必仅PersistentKeepalive
在服务器为内部服务器时进行设置,否则您将毫无意义地浪费带宽和电池寿命。
PersistentKeepalive = 25
总而言之,客户端[Peer]
部分用于配置与其连接的对等方的信息,在本例中它是与服务器的客户端连接。
PublicKey
是服务器的公钥。Endpoint
是您的服务器的公共 IP 和服务器接口正在监听的端口,ListenPort
在服务器的配置中配置。AllowedIPs
是允许转发的 IP 范围。设置为 ,则0.0.0.0/0
所有流量都将通过隧道转发。PersistentKeepalive
是定期向服务器发送保持连接数据包的间隔。
如果您不确定服务器的公共地址是什么,您可以通过向以下地址发出 DNS 查询请求来进行 IP 查找myip.opendns.com
:
root@ip-172-30-0-233:/etc/wireguard# dig +short myip.opendns.com @resolver1.opendns.com
54.225.123.18
如果您的服务器是 EC2 实例,您可以查询元数据端点以获取公共 IP 地址:
root@ip-172-30-0-233:/etc/wireguard# curl http://169.254.169.254/latest/meta-data/public-ipv4
54.225.123.18
在服务器配置上设置客户端对等体
返回服务器并编辑配置。我们将添加有关客户端的信息,以便服务器和客户端可以相互验证。
root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0.conf
将该[Peer]
部分添加到服务器配置:
[Interface]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = <client public key>
AllowedIPs = 10.0.0.2/32
PublicKey
用您的客户端的公钥替换该值:
[Interface]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = vi4TCAo8TNRkpf4ZpiMsp3YHaOLrcouSDkrm4wJxezw=
AllowedIPs = 10.0.0.2/32
总而言之,服务器[Peer]
部分用于配置与其连接的对等方的信息,在本例中它是服务器与客户端的连接。
PublicKey
是客户端的公钥。AllowedIPs
允许客户端 IP 地址。
在服务器上启动 WireGuard 服务
现在服务器有了客户端对等信息,我们可以wg-quick up wg0
在服务器上启动 WireGuard 服务:
root@ip-172-30-0-233:/etc/wireguard/keys# wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.0.0.1/24 dev wg0
[#] ip link set mtu 8921 up dev wg0
[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
要在重启后启动 WireGuard,您需要通过运行以下命令启用该服务以将其添加到 systemd init 系统systemctl enable wg-quick@wg0.service
:
root@ip-172-30-0-233:/etc/wireguard/keys# systemctl enable wg-quick@wg0.service
Created symlink /etc/systemd/system/multi-user.target.wants/wg-quick@wg0.service → /lib/systemd/system/wg-quick@.service.
通过运行检查状态systemctl status wg-quick@wg0.service
,如果看到Active: active (exited),则说明到目前为止一切正常:
root@ip-172-30-0-233:/etc/wireguard/keys# systemctl status wg-quick@wg0.service
● wg-quick@wg0.service - WireGuard via wg-quick(8) for wg0
Loaded: loaded (/lib/systemd/system/wg-quick@.service; indirect; vendor preset: enabled)
Active: active (exited) since Thu 2020-04-02 06:35:22 UTC; 1s 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: 10730 ExecStart=/usr/bin/wg-quick up wg0 (code=exited, status=0/SUCCESS)
Main PID: 10730 (code=exited, status=0/SUCCESS)
Apr 02 06:35:21 ip-172-30-0-233 systemd[1]: Starting WireGuard via wg-quick(8) for wg0...
Apr 02 06:35:22 ip-172-30-0-233 wg-quick[10730]: [#] ip link add wg0 type wireguard
Apr 02 06:35:22 ip-172-30-0-233 wg-quick[10730]: [#] wg setconf wg0 /dev/fd/63
Apr 02 06:35:22 ip-172-30-0-233 wg-quick[10730]: [#] ip -4 address add 10.0.0.1/24 dev wg0
Apr 02 06:35:22 ip-172-30-0-233 wg-quick[10730]: [#] ip link set mtu 8921 up dev wg0
Apr 02 06:35:22 ip-172-30-0-233 wg-quick[10730]: [#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; ipt
Apr 02 06:35:22 ip-172-30-0-233 systemd[1]: Started WireGuard via wg-quick(8) for wg0.
使用以下命令验证是否已应用新的 iproute 规则iptables -L -n
:
root@ip-172-30-0-233:/etc/wireguard# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
运行该命令显示具有我们指定的内部 IP 地址的ifconfig
新网络接口:wg0
10.0.0.1
root@ip-172-30-0-233:/etc/wireguard# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9001
inet 172.30.0.233 netmask 255.255.255.0 broadcast 172.30.0.255
inet6 fe80::1097:5bff:fe30:d57 prefixlen 64 scopeid 0x20<link>
ether 12:97:5b:30:0d:57 txqueuelen 1000 (Ethernet)
RX packets 1151269 bytes 524679242 (524.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1022229 bytes 345390292 (345.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 526 bytes 48787 (48.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 526 bytes 48787 (48.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP> mtu 8921
inet 10.0.0.1 netmask 255.255.255.0 destination 10.0.0.1
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
在客户端启动 WireGuard 服务
首先记下客户端计算机上的当前公共 IP 地址:
[root@archlinux wireguard]# dig +short myip.opendns.com @resolver1.opendns.com
65.88.88.4
当我们在客户端启动 WireGuard 服务后,公网 IP 地址将被解析为服务器的公网 IP 地址。
wg-quick
像我们之前在服务器上所做的那样启动 WireGuard 服务:
[root@archlinux wireguard]# wg-quick up wg0
现在 WireGuard 正在运行,再次检查客户端的公共 IP 地址,它现在应该是服务器的公共 IP 地址:
[root@archlinux wireguard]# dig +short myip.opendns.com @resolver1.opendns.com
54.225.123.18
成功! WireGuard 已正确配置,并且对等体已连接。
将移动客户端连接到服务器
在您的设备上下载适用于iOS或Android 的WireGuard 应用程序。
在此示例中,我们将创建第二个客户端(iPhone)来连接到 WireGuard 服务器。设置第一个客户端时,需要遵循相同的步骤。
我们可以直接在设备上生成密钥并手动设置配置,但这既不快捷也不理想。我们可以在服务器上生成密钥和配置,然后将信息安全地传输到 WireGuard 应用中。
运行wg genkey
但这次指定不同的文件名以将它们与服务器密钥区分开来:
root@ip-172-30-0-233:/etc/wireguard# cd /etc/wireguard/keys
root@ip-172-30-0-233:/etc/wireguard/keys# wg genkey | tee iphone_privatekey | wg pubkey > iphone_publickey
root@ip-172-30-0-233:/etc/wireguard/keys# cat iphone_privatekey
kFnMqMSiAluwb/xWgemXhjLh/II/sb92OoYCbh7yaWw=
root@ip-172-30-0-233:/etc/wireguard/keys# cat iphone_publickey
cKIxzfp5ESpdM34vT2Qk/S7yvprOff6Le4YnyOTI4B8=
在您最喜欢的编辑器中打开服务器配置/etc/wireguard/wg0.conf
:
root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0.conf
添加第二个 peer 部分并包含客户端的公钥和 IP 地址:
[Interface]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PrivateKey = wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = cKIxzfp5ESpdM34vT2Qk/S7yvprOff6Le4YnyOTI4B8=
AllowedIPs = 10.0.0.3/32
在服务器上为 iPhone 客户端创建一个新的配置文件。我们将其命名为wgo-iphone.conf
:
root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0-iphone.conf
粘贴客户端配置,但记住使用与第一个客户端不同的私有 IP。
[Interface]
PrivateKey = kFnMqMSiAluwb/xWgemXhjLh/II/sb92OoYCbh7yaWw=
Address = 10.0.0.3/32
DNS = 1.1.1.1
[Peer]
PublicKey = H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=
Endpoint = 54.225.123.18:51820
AllowedIPs = 0.0.0.0/0
在服务器上安装qrencode ,以便从配置文件生成二维码。
您将在 WireGuard 应用中扫描此二维码来下载配置。这是一种更安全的凭证传输方式,因为无需压缩和移动密钥和配置文件。
使用以下命令在终端中生成基于文本的二维码图像qrencode -t ansiutf8 < wg0-iphone.conf
root@ip-172-30-0-233:/etc/wireguard# qrencode -t ansiutf8 < wg0-iphone.conf
<qrcode image>
在 WireGuard 应用程序中,转到:添加隧道 → 从 QRCode 创建
扫描终端中生成的二维码,并确保在设置弹出窗口中允许VPN 配置。
通过切换开关启用 VPN。
在浏览器中访问ipchicken.com来验证公共 iP 地址是否已更改。
生成靓号地址
由于密钥看起来都像随机字符串,因此很容易混淆哪些密钥属于哪些设备。为了更轻松地将密钥与设备关联,您可以使用这个虚荣地址生成器来生成包含自定义字符数组的公钥。
例如,我们将生成一个密钥对,其中公钥以“iPho”开头,以表示它是在 iPhone 客户端上使用的密钥对。
首先使用cargo安装虚荣地址生成器:
$ cargo install wireguard-vanity-address
现在只需指定您想要在公钥 base64 输出中的字符列表:
$ wireguard-vanity-address ipho
searching for 'ipho' in pubkey[0..10], one of every 149796 keys should match
one trial takes 83.6 us, CPU cores available: 8
est yield: 1.6 seconds per key, 638.83e-3 keys/s
hit Ctrl-C to stop
private YPpudjAoVCnaPUJdcEVhj5Ttedq7WP1ozL+ZdtuTC1g= public cHklbipHoMS9CA8XlRdKMBOOIfQC28Ut8SVyYsqmox0=
private kD6FSIZehv1DKJ28MKJQcmSDdd69U3s4s11ymtP1Ekc= public iPHoaaQye7+OJNq/TfOvXjMr99pq9ADDDlGynRQ6KQ8=
private aEJ33LXCeipouhiAoQjfMjtwrHPfZDvKLguE8XlawnY= public iPHoEoUy4WgkUXr4e47IkA06IZqVI/AqHNS2RZlGhHM=
^C
它会一直生成,直到您看到喜欢的密钥对时手动停止它。
自动化
Algo是一个可以自动执行 WireGuard VPN 设置过程的好工具。
Algo 是一组Ansible脚本,可帮助您从本地计算机在远程服务器上设置和配置 WireGuard。
首先,克隆 algo 存储库并安装 python 依赖项:
~$ git clone https://github.com/trailofbits/algo.git
~$ cd algo/
~/algo$ pip install virtualenv
~/algo$ python3 -m virtualenv --python="$(command -v python3)" .env && source .env/bin/activate && python3 -m pip install -U pip virtualenv && python3 -m pip install -r requirements.txt
现在运行 algo 可执行文件来开始将 Algo 服务器部署到云端的演练:
(.env) ~/algo$ ./algo
[Cloud prompt]
What provider would you like to use?
1. DigitalOcean
2. Amazon Lightsail
3. Amazon EC2
4. Microsoft Azure
5. Google Compute Engine
6. Hetzner Cloud
7. Vultr
8. Scaleway
9. OpenStack (DreamCompute optimised)
10. CloudStack (Exoscale optimised)
11. Install to existing Ubuntu 18.04 or 19.10 server (for more advanced users)
Enter the number of your desired provider
:
<truncated>
故障排除
以下是用户报告的在出现以下错误后需要采取的额外步骤。
如果您尝试wg-quick up wg0
并收到以下内容:
root@ubuntu:/etc/wireguard# wg-quick up wg0
[#] ip link add wg0 type wireguard
RTNETLINK answers: Operation not supported
Unable to access interface: Protocol not supported
[#] ip link delete dev wg0
Cannot find device "wg0"
尝试启动 wg0 接口时出现错误“RNETLINK 答案:不支持操作”可能意味着您需要安装所需的其他内核模块。
要在 Ubuntu 上安装所需的 wireguard 内核模块,请运行:
root@ubuntu:/etc/wireguard# apt-get install wireguard-dkms wireguard-tools linux-headers-$(uname -r)
如果内核模块安装成功,则运行不modprobe wireguard
应该输出任何内容:
root@ubuntu:/etc/wireguard# modprobe wireguard
如果您看到错误,则可能是安全启动功能阻止了未签名的 WireGuard 内核模块。要解决此问题,您需要禁用安全启动或对 WireGuard 模块进行签名,以便内核信任它。请参阅此答案以获取说明。
重新启动后尝试使用以下命令加载界面ip link
:
root@ubuntu:/etc/wireguard# ip link add dev wg0 type wireguard
如果成功则不应有输出。
如果您尝试wg-quick up wg0
并收到以下内容:
root@ubuntu:/etc/wireguard# wg-quick up wg0
[#] ip link add configfile type wireguard
[#] wg setconf configfile /dev/fd/63
[#] ip address add 10.0.0.1/24 dev configfile
[#] ip link set mtu 1420 dev configfile
[#] ip link set configfile up
[#] resolvconf -a configfile -m 0 -x
/usr/bin/wg-quick: line 31: resolvconf: command not found
[#] ip link delete dev configfile
这可能意味着resolveconf
您的机器上没有安装该软件包,因此您需要安装该openresolv
软件包:
root@ubuntu:/etc/wireguard# apt-get install openresolv
请参阅此Github 问题线程以获取更多背景信息。
TLDR;
以下是本文中使用的服务器和客户端配置和命令的摘要:
服务器
服务器命令:
sudo -s
apt-get install wireguard
mkdir -p /etc/wireguard/keys
cd /etc/wireguard/keys
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
vim /etc/wireguard/wg0.conf # see server config below
vim /etc/sysctl.conf # uncomment line "net.ipv4.ip_forward=1"
sysctl -p
wg-quick up wg0
systemctl enable wg-quick@wg0.service
服务器配置/etc/wireguard/wg0.conf
:
[Interface]
PrivateKey = <server private key>
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = <client public key>
AllowedIPs = 10.0.0.2/32
客户
客户端命令:
sudo -s
apt-get install wireguard
mkdir -p /etc/wireguard/keys
cd /etc/wireguard/keys
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
vim /etc/wireguard/wg0.conf # see client config below
wg-quick up wg0
客户端配置/etc/wireguard/wg0.conf
:
[Interface]
PrivateKey = <client private key>
Address = 10.0.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = <server public key>
Endpoint = <server public ip>:51820
AllowedIPs = 0.0.0.0/0
资源
- WireGuard 网站
- WireGuard 虚荣地址生成器
- 适用于 iOS 的 WireGuard
- 适用于 Android 的 WireGuard
- 适用于 macOS 的 WireGuard
- 适用于 Windows 的 WireGuard
- libqrencode
- 算法