WireGuard安装

警告
本文最后更新于 2022-07-01,文中内容可能已过时。

WireGuard® is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.

官方文档

通过包管理器安装

WireGuard对内核有要求。内核不满足先进行内核版本升级。

以下三种方式,任选其一

Method 1: a signed module is available as built-in to CentOS’s kernel-plus:

1
2
3
4
5
yum -y install yum-utils epel-release
yum-config-manager --setopt=centosplus.includepkgs=kernel-plus --enablerepo=centosplus --save
sed -e 's/^DEFAULTKERNEL=kernel$/DEFAULTKERNEL=kernel-plus/' -i /etc/sysconfig/kernel
yum -y install kernel-plus wireguard-tools
reboot

Method 2: users wishing to stick with the standard kernel may use ELRepo’s pre-built module:

1
2
3
yum -y install epel-release elrepo-release
yum -y install yum-plugin-elrepo
yum -y install kmod-wireguard wireguard-tools

Method 3: users running non-standard kernels may wish to use the DKMS package instead:

1
2
3
yum -y 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 -y install wireguard-dkms wireguard-tools

其它系统安装请参考官方文档

此处我选择第二种方式

1
2
3
yum -y install epel-release elrepo-release
yum -y install yum-plugin-elrepo
yum -y install kmod-wireguard wireguard-tools
1
2
3
4
cd /etc/wireguard  # 如果目录不存在请手动创建:mkdir /etc/wireguard
# 开始生成密匙对(公匙+私匙)。
wg genkey | tee privatekey-server | wg pubkey > publickey-server # 生成服务端密钥对
wg genkey | tee privatekey-client | wg pubkey > publickey-client # 生成客户端密钥对
1
vi /etc/wireguard/wg0.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[Interface]
Address = 10.100.0.1/16  # 这里指的是使用 10.100.0.1,网段大小是 16 位
SaveConfig = true
ListenPort = 51820  # 监听的 UDP 端口
PrivateKey = < 这里填写 Server 上 privatekey 的内容 >
# 下面这两行规则允许访问服务器的内网,注意替换 eth0
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

# Client,可以有很多 Peer
[Peer]
PublicKey = < 这里填写 Client 上 publickey 的内容 >
# 允许客户端使用的IP段,对于peer的隧道IP, 建议使用/32的固定IP, 以避免和其他peer重复导致无法连接
AllowedIPs = 10.100.0.2/32  # 这个 Peer 只能是 10.100.0.2/32
# 如果想把所有流量都通过服务器的话,这样配置:
# AllowedIPs = 0.0.0.0/0, ::/0
1
2
3
4
5
6
7
8
systemctl enable wg-quick.target
systemctl restart wg-quick@wg0.service
systemctl reload wg-quick@wg0.service
# 使用下发命令也可
wg-quick up wg0    #启动服务端
wg-quick down wg0  #停止服务端
wg #查看节点列表
wg syncconf wg0 <(wg-quick strip wg0) #重载配置文件,不影响已有连接.
1
vi client.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[Interface]
PrivateKey = < 这里填写 Client 上 privatekey 的内容 >
Address = 10.100.0.2/32
DNS = 8.8.8.8  # 连接后使用的 DNS, 如果要防止 DNS 泄露,建议使用内网的 DNS 服务器

[Peer]
PublicKey = < 这里填写 Server 上 publickey 的内容 >
Endpoint = 服务器端的公网IP:51820  # 服务端公网暴露地址,51280 是上面指定的
AllowedIPs = 10.100.0.0/16,172.17.0.11/20  # 指定要访问的服务端网段,或者设置0.0.0.0/0来进行全局代理.
PersistentKeepalive = 25

然后复制文件内容到各客户端即可.

  • 客户端下载地址:https://www.wireguard.com/install/

还是执行 wg genkey | tee privatekey-client | wg pubkey > publickey-client 生成新的客户端私钥和公钥。注意文件名不要重复

然后在/etc/wireguard/wg0.conf中添加新的 [Peer]

1
2
3
[Peer]
PublicKey = < 这里填写 Client 上 publickey 的内容 >
AllowedIPs = 10.100.0.3/32  # 这个 Peer 只能是 10.100.0.3

其中 Interface 就是自身的接口配置,比如说对于服务器来说,需要配置 ListenPort,对于服务端和客户端来说都需要配置自己的 Address。Privatekey 填写自己的私钥内容。

Peer 这部分就比较有意思了,Peer 指的是 WireGuard 连接的另一端,对于服务器来说,Peer 就是客户端,而对于客户端来说,Peer 自然就是服务器。一个配置文件中可以有多个 Peer,这样的话,服务端就可以有多个客户端连接了。Peer 中可以指定 AllowedIPs,也就是允许连接的 IP。比如说,客户端指定了 10.100.0.0/16,172.17.0.11/20,那么就只有这部分 IP 的流量会通过虚拟专用网路由,而如果指定了 0.0.0.0/0,那么就是所有的流量通过虚拟专用网了。所以如果你只是想访问内网,也就是远程办公的话,那么可以选择前一种方法,如果是想通过国外主机访问某些受限的信息的话,当然要使用后一种了。Publickey 填写对方的公钥内容。

1.无法访问服务端内网

1
2
3
4
# 开启内核转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

一键安装

参考:WireGuard 一键安装脚本