wireguard的原理
安装wg之前,一定要理解wg的原理,wg是通过UDP协议在各个主机之间建立对等连接,所以理论上其实是没有服务器和客户端之说的,有时候我们很容器把
中续器理解为服务器,把其中一个主机理解为客户端,这为给我们的配置带来误解。这也是为什么在wg的配置文件里面没有client而只有peer的原因。
在 WireGuard 里,客户端和服务端基本是平等的,差别只是谁主动连接谁而已。双方都会监听一个 UDP 端口,谁主动连接,谁就是客户端。
主动连接的客户端需要指定对端的公网地址和端口,被动连接的服务端不需要指定其他对等节点的地址和端口。如果客户端和服务端都位于 NAT 后面,
需要加一个中继服务器,客户端和服务端都指定中继服务器作为对等节点,它们的通信流量会先进入中继服务器,然后再转发到对端。
下载脚本并安装wireguard
直接通过一键安装脚本进行安装,不过要注意的是wg使用的是UDP协议,如果你用的是阿里云的服务器,一定要在安全组上开放UDP协议的连接端口。
wget --no-check-certificate -O /opt/wireguard.sh https://raw.githubusercontent.com/teddysun/across/master/wireguard.sh
chmod 755 /opt/wireguard.sh
cd /opt
export VPN_SERVER_WG_PORT=11660 #指定wg的监听端口,如果不指定将会生成一个随机端口
sh wireguard.sh -r
出现如下提示,说明安装成功
[Mon Oct 4 10:59:35 CST 2021] WireGuard VPN Server installation completed
[Mon Oct 4 10:59:35 CST 2021]
[Mon Oct 4 10:59:35 CST 2021] WireGuard VPN default client file is below:
[Mon Oct 4 10:59:35 CST 2021] /etc/wireguard/wg0_client
[Mon Oct 4 10:59:35 CST 2021]
[Mon Oct 4 10:59:35 CST 2021] WireGuard VPN default client QR Code is below:
[Mon Oct 4 10:59:35 CST 2021] /etc/wireguard/wg0_client.png
[Mon Oct 4 10:59:35 CST 2021]
[Mon Oct 4 10:59:35 CST 2021] Download and scan this QR Code with your device
[Mon Oct 4 10:59:35 CST 2021] Welcome to visit: https://teddysun.com/554.html
[Mon Oct 4 10:59:35 CST 2021] Enjoy it
脚本命令列表如下:
Usage : wireguard.sh [Options]
Options:
-h, --help Print this help text and exit
-r, --repo Install WireGuard from repository
-s, --source Install WireGuard from source
-u, --update Upgrade WireGuard from source
-v, --version Print WireGuard version if installed
-a, --add Add a WireGuard client
-d, --del Delete a WireGuard client
-l, --list List all WireGuard client's IP
-n, --uninstall Uninstall WireGuard
安装后查看wg的状态
$ wg show
interface: wg0
public key: NuqTrBKdgO79f5oeGRrttKBpEy24eB*****
private key: (hidden)
listening port: 11660
peer: a0ruXecXSnY09Q8HCoJb9Xg55OK31uFCQ********
preshared key: (hidden)
allowed ips: 10.88.88.2/32
persistent keepalive: every 25 seconds
这时候说明阿里云服务器上的wg安装好了,这时候脚本自定给我们加个了peer,事实上这个peer是没有什么用的。但是这里wg0的public key要记住,
因为别的主机要与这台主机连接时,需要这个key。
systemctl enable wg-quick@wg0.service #开机自动启动wg
systemctl restart wg-quick@wg0.service #重启wg
mac本机配置
注意LEq开头的公钥要放到阿里云服务器的wg配置中,而这里的Peer PublicKey是阿里云服务器wg的publicKey
评论区