1.服务器安装好wg服务,然后添加一个客户端
[Interface]
PrivateKey = oO/2hYO4oh6X1vpjiaVws88a2fdOGYjtp0cp1LsdfVM=
Address = 10.77.0.5/24
DNS = 8.8.8.8
MTU = 1420
[Peer]
PublicKey = a81fdMFF9x7kVQC/RwcF5B8J94RvvkDsbdqA9dU17mU=
AllowedIPs = 0.0.0.0/0
Endpoint = 123.10.11.13:5214
PersistentKeepalive = 25
以下都在客户端上配置
客户端:ip: 192.168.8.18/24 网关:192.168.8.2(后面会用到)
2.更新系统并安装wg软件
apt update
apt upgrade
apt install wireguard
3.配置客户端文件
进入Wireguard目录
cd /etc/wireguard/
需要手动添加一个wg0.conf 的网卡配置文件,
vim /etc/wireguard/wg0.conf
进入后 根据自己服务器wireguad的配置情况来设置自己的客户端配置文件。(注意第一步骤服务器给过来的文件需要修改与添加)
[Interface]
ListenPort = 5214 #服务器端口
PrivateKey = oO/2hYO4oh6X1vpjiaVws88a2fdOGYjtp0cp1LsfVM= #客户端私钥
[Peer]
PublicKey = a81fdMFF9x7kVQC/RwcF5B8J94RvvkDsbdqA9dU17mU= #服务器公钥
AllowedIPs = 0.0.0.0/0
Endpoint = 123.10.11.13:5214 #服务器地址与端口
PersistentKeepalive = 25
4.配置挂在虚拟网卡wg0
ip link add dev wg0 type wireguard # 挂载wg0虚拟网卡
ip address add dev wg0 10.77.0.5/24 # Wireguard内网地址及网段
ip link set wg0 up # 启动 wg0 网卡
wg setconf wg0 /etc/wireguard/wg0.conf # wg0网卡配置信息链路
ip route add 123.10.11.13 via 192.168.8.2 # VPS的路由到客户端网关路由
ip route del default
ip route add default dev wg0
完成后ping一下wg网关
root@test:~# ping 10.77.0.1
PING 10.77.0.1 (10.77.0.1) 56(84) bytes of data.
64 bytes from 10.77.0.1: icmp_seq=1 ttl=64 time=243 ms
64 bytes from 10.77.0.1: icmp_seq=2 ttl=64 time=267 ms
64 bytes from 10.77.0.1: icmp_seq=3 ttl=64 time=288 ms
64 bytes from 10.77.0.1: icmp_seq=4 ttl=64 time=309 ms
^C
--- 10.77.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 243.647/277.137/309.711/24.496 ms
5.上外网,需要修改DNS,将DNS改成 8.8.8.8
vim /etc/systemd/resolved.conf
1 # This file is part of systemd.
2 #
3 # systemd is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU Lesser General Public License as published by
5 # the Free Software Foundation; either version 2.1 of the License, or
6 # (at your option) any later version.
7 #
8 # Entries in this file show the compile time defaults.
9 # You can change settings by editing this file.
10 # Defaults can be restored by simply deleting this file.
11 #
12 # See resolved.conf(5) for details
13
14 [Resolve]
15 DNS=8.8.8.8
16 #FallbackDNS=
17 #Domains=
重启一下DNS
systemctl restart systemd-resolved.service
root@test:~# ping google.com
PING google.com (142.251.32.46) 56(84) bytes of data.
64 bytes from sfo03s26-in-f14.1e100.net (142.251.32.46): icmp_seq=1 ttl=58 time=305 ms
64 bytes from sfo03s26-in-f14.1e100.net (142.251.32.46): icmp_seq=2 ttl=58 time=228 ms
64 bytes from sfo03s26-in-f14.1e100.net (142.251.32.46): icmp_seq=3 ttl=58 time=248 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3005ms
rtt min/avg/max/mdev = 228.284/260.914/305.971/32.912 ms
6.想要断开服务需要通过命令
ip link del dev wg0
ip route del 123.10.11.13 via 192.168.8.2
ip route del default
ip route add default via 192.168.8.2