点击展开更新日志
源起
原本想要直接使用 OpenVPN 的,但是没有找到绿色版的软件(笔记本限制装不了软件),于是换到了 Wire
Guard。
服务端安装配置
服务端安装 WireGuard
1 2 3 4 5 6 7 8 sudo apt install wireguard -ysudo yum install epel-release -ysudo yum install wireguard-tools -ysudo yum install wireguard-dkms -y
生成密钥对并开启IP转发
WireGuard使用公钥/私钥对进行身份验证,每个设备都需要生成一对。
1 2 3 4 5 6 7 8 9 10 chmod 700 /etc/wireguard/cd /etc/wireguard/wg genkey | tee server_private.key | wg pubkey > server_public.key echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.confsudo sysctl -p /etc/sysctl.d/99-wireguard.conf
创建服务端配置文件
1 2 vim /etc/wireguard/wg0.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [Interface] PrivateKey = <SERVER_PRIVATE_KEY>Address = 10.0 .0.1 /24 ListenPort = 51820 PostUp = iptables -A FORWARD -i %i -j ACCEPTPostDown = iptables -D FORWARD -i %i -j ACCEPT[Peer] PublicKey = <CLIENT_PUBLIC_KEY>AllowedIPs = 10.0 .0.2 /32
启动服务
1 2 3 4 5 6 root@VM-0-3-ubuntu:/etc/wireguard# wg-quick up wg0 [#] ip link add wg0 type wireguard [#] wg setconf wg0 /dev/fd/63 [#] ip -4 address add 10.0.0.1/24 dev wg0 [#] ip link set mtu 1420 up dev wg0 [#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
客户端配置
客户端配置
我们将家中的设备配置为WireGuard的Spoke(节点),它会主动连接腾讯云服务器,建立加密隧道。
安装 WireGuard 并生成密钥:
Linux
1 2 3 cd /etc/wireguardumask 077wg genkey | tee client_private.key | wg pubkey > client_public.key
Windows
安装客户端之后,选择【新建隧道-新建空隧道】,系统会自动生成一对密钥,并显示公钥和私钥。复制这个公钥,稍后需要填入腾讯云服务器的配置文件中。
创建客户端配置文件
Linux
1 2 sudo vim /etc/wireguard/wg0.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [Interface] PrivateKey = <CLIENT_PRIVATE_KEY>Address = 10.0 .0.2 /24 DNS = 8.8 .8.8 , 8.8 .4.4 [Peer] PublicKey = <SERVER_PUBLIC_KEY>Endpoint = <腾讯云服务器公网IP>:51820 AllowedIPs = 10.0 .0.1 /32 , 192.168 .1.0 /24 PersistentKeepalive = 25
启动服务
启动服务。