开启转发

1
2
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

安装官方 WireGuard

官方链接

1
2
3
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
curl -o /etc/yum.repos.d/jdoss-wireguard-epel-7.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
yum install wireguard-dkms wireguard-tools

服务端安装

创建公钥私钥

WireGuard的服务端或客户端都必须用单独的公钥私钥来进行鉴权,所以参考以下命令格式,为各设备创建对应的公钥私钥。**其中预共享密钥属于可选配置**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /etc/wireguard/
umask 077
# 设置目录默认权限
wg genpsk > sharekey
# 可选,生成预共享密钥
wg genkey > server.key
wg pubkey < server.key > server.key.pub
# 服务器的公钥私钥
wg genkey > iphone.key
wg pubkey < iphone.key > iphone.key.pub
# iPhone 的公钥私钥
wg genkey > ipad.key
wg pubkey < ipad.key > ipad.key.pub
# iPad 的公钥私钥
wg genkey > macos.key
wg pubkey < macos.key > macos.key.pub
# MacBook 的公钥私钥

其中.key 为私钥,.key.pub 为公钥,可以使用 cat server.key 命令获取。

配置服务端

执行以下命令获取当前服务器默认网卡的名称,用于配置 iptabls 的流量转发。一般情况下网卡名称有可能为 eth0,ens192,ens3 等等。本文示例为 ens192。

1
2
ifconfig
# 获取网卡信息
创建wg0.conf服务器配置文件
1
2
3
# 依旧在 /etc/wireguard 目录下执行
vi wg0.conf
# 复制以下参考配置
以下参考配置可以自行修改
  • [Interface] 区域为服务器端。
  • [Interface] - PrivateKey 为上文创建的服务器端私钥,即server.key
  • Address 为服务端的内网 IP,可以自行选择例如192.168.0.110.0.0.1,但是需注意请勿和服务器所在内网网段冲突,例如博主家内网网段为 192.168.1.X,这台 CentOS 7 服务器的内网 IP 为 192.168.1.15,则不能使用 192.168.1.X 该网段。
  • ListenPort 为服务端口,需在防火墙开放此端口的UDP访问,或进行路由器端口映射。
  • DNS 服务器自行选择。
  • PostUp 为启动命令的 iptabls 配置,注意需要修改 ens192 为你的服务器网卡名。
  • PostDown 为停止命令的 iptabls 配置,注意需要修改 ens192 为你的服务器网卡名。
  • [Peer] 为客户端配置,必须为每个客户端分别配置。
  • [Peer] - PublicKey 为上文创建的客户端公钥,即macos.key.pub
  • AllowedIPs 为该客户端指定 IP,注意不要和其他客户端冲突。
  • PresharedKey 为预共享密钥,根据上文可选设置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 服务端配置
[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Address = 10.0.0.1
ListenPort = 45678
DNS = 114.114.114.114
MTU = 1420

PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens192 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens192 -j MASQUERADE


########### 以下为各个客户端信息 ###########
# macOS
[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 10.0.0.2/32
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PersistentKeepalive = 25

# iPhone
[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 10.0.0.3/32
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PersistentKeepalive = 25

# iPad
[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 10.0.0.4/32
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PersistentKeepalive = 25

启动服务

执行以下命令启动服务,并设置开机自动启动
1
2
3
4
5
6
systemctl enable wg-quick@wg0
# 设置开机自动启动
wg-quick up wg0
# 启动
wg-quick down wg0
# 停止

客户端安装

客户端的配置文件其实相对简单,但是最重要是的AllowedIPs这一项路由信息需要根据你实际需求来设置,以下以iPhone客户端的配置为例:
  • 由于此配置为客户端iPhone使用,[Interface]则为iPhone的相关配置,[Peer]则为服务端也就是CentOS 7的服务器相关信息。
  • Address 为该iPhone的内网 IP,对应上述服务端配置,此处iPhone的 IP 应为10.0.0.3
  • [Interface] - PrivateKey 为上文创建的iPhone 的私钥,即iphone.key
  • DNS 服务器自行选择。
  • [Peer] 为服务端的配置信息
  • [Peer] - PublicKey 为服务端公钥,即server.key.pub
  • AllowedIPs - 为路由信息,本文0.0.0.0/0则代表**全局代理**转发。下文具体讲解
  • PresharedKey 为预共享密钥,根据上文可选设置。
  • Endpoint = 为服务端的 IP 和端口,也可以使用域名加端口。
1
2
3
4
5
6
7
8
9
10
11
12
13
# 客户端配置 示例 iPhone
[Interface]
Address = 10.0.0.3/32
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DNS = 114.114.114.114

# 服务端配置
[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 0.0.0.0/0
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Endpoint = xxx.xxx.xxx.xxx:45678
PersistentKeepalive = 25

AllowedIPs

AllowedIPs 其实可以理解为路由,根据需求不同设置也不同。

全局代理 / iPhone / iPad

由于 iPhone / iPad 在使用 WireGuard 时是**无法使用其他代理软件**,如果家中的网络环境已经可以科学上网,或者 WireGuard 部署在境外公网服务器上,则可以设置AllowedIPs = 0.0.0.0/0进行**全局代理**网络,此时该 iPhone / iPad 可以访问包括家庭内网、WireGuard 的内网、以及其他任何国内和国外网站,并且都是以 WireGuard 这台服务器的 IP 访问。

内网代理 / macOS

如果在家中 CentOS 7 部署了 WireGuard,希望通过 WireGuard 回家访问内网设备,而其他互联网访问,科学访问可以使用 macOS 本身的网络和软件实现,那么 AllowedIPs 则只需要设置家庭内网 IP 网段。 例如博主家为192.168.1.X网段,WireGuard 的网段为上文设置的10.0.0.X,AllowedIPs 可以设置为 AllowedIPs = 192.168.1.0/24, 10.0.0.0/24,此时将可以访问家庭内网的NAS等设备,以及WireGuard自身的10.0.0.X网段,例如另外一台Windows电脑连入这个WireGuard也可以被访问。然而其他网段和互联网,科学上网都将由 macOS 当前所在的网络配合 Surge 等其他代理软件去访问。