通过云服务器实现异地组网 部署WireGuard
可以方便不同局域网联机玩一些老游戏或者用于办公
linux部署服务端(需要有公网ip的服务器,比如云服务器),windows部署客户端(当然也可以是linux)
默认使用root的权限来写,如果非root提权或者加上sudo,下面不再赘述
一、服务端配置
1.安装
apt-get update
apt install -y wireguard
2.生成密钥对
创建会话的临时掩码-提高安全性,可以不执行这一条
umask 077
创建密钥,分多次执行下面的代码,用于创建密钥
wg genkey | tee server_private.key | wg pubkey > server_public.key
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
wg genkey | tee client2_private.key | wg pubkey > client2_public.key
创建完成后可以看到
3.编辑配置文件
vi /etc/wireguard/wg0.conf
服务器私钥为server_private.key内容
client1 公钥为client1_public.key内容
client2 公钥为client2_public.key内容
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <服务器私钥># 客户端1(你)
[Peer]
PublicKey = <client1 公钥>
AllowedIPs = 10.0.0.2/32# 客户端2(朋友)
[Peer]
PublicKey = <client2 公钥>
AllowedIPs = 10.0.0.3/32
4.开机自启动
不要质疑这个名称,我一开始也觉得谁会这样命名软件包,实际上就是这样
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0
5.开启ip转发
echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
应用系统配置
sysctl -p
二、客户端配置
使用配置文件,如果在windows下,就是建立一个记事本文件,然后写完把后缀改了就行
1.配置文件
第一个客户端配置文件:client-1.conf
<client1 私钥> client1_private.key
<server 公钥> server_public.key
<服务端ip> 为服务端的公网ip
[Interface]
PrivateKey =<client1 私钥>
Address = 10.0.0.2/24
DNS = 114.114.114.114[Peer]
PublicKey = <server 公钥>
Endpoint = <服务端ip>:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25
第一个客户端配置文件:client-2.conf
<client2 私钥> client2_private.key
<server 公钥> server_public.key
<服务端ip> 为服务端的公网ip
[Interface]
PrivateKey = <client2 私钥>
Address = 10.0.0.3/24
DNS = 114.114.114.114[Peer]
PublicKey = <server 公钥>
Endpoint = <服务端ip>:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25
2.使用
通过配置文件导入隧道即可
三、其它
(一)windows下无法ping通
1.
需要windows防火墙添加允许同时开启
更改设置—》允许其它应用—》添加
2.开启icmp
高级安全windows Defender开启
右键点击并选择启用