序言
最近将所有服务器更换为Debian,并在其上安装WireGuard
首先访问WireGuard官方网站获取安装信息:https://www.wireguard.com/install/#installation
安装WireGuard
- 首先,确保系统是最新的
apt update
- 接着,安装WireGuard
apt install wireguard
配置WireGuard服务器
- 创建WireGuard配置目录并更改权限:
cd /etc/wireguard/
umask 077
- 生成密钥对
wg genkey > server.key
wg pubkey < server.key > server.key.pub
- 创建服务器配置文件:
vim /etc/wireguard/wg0.conf
- 编辑配置文件,将生成的PrivateKey替换到
wg0.conf
文件中的PrivateKey
字段。
[Interface]
Address = 192.168.0.1/16
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
ListenPort = 51820
PrivateKey = {Server_PrivateKey}
[Peer]
PublicKey = {Client1_PublicKey}
AllowedIPs = 192.168.0.2/32
PersistentKeepalive = 25
[Peer]
PublicKey = {Client2_PublicKey}
AllowedIPs = 192.168.0.3/32
PersistentKeepalive = 25
- 启动并检查WireGuard服务器:
sudo wg-quick up wg0
sudo wg-quick down wg0
sudo wg show
配置WireGuard客户端
- 创建客户端配置文件:
vim /etc/wireguard/wg0.conf
- 生成密钥对
wg genkey > client.key
wg pubkey < client.key > client.key.pub
- 编辑配置文件
[Interface]
PrivateKey = {Client_PrivateKey}
Address = 192.168.0.2/16
[Peer]
PublicKey = {Server_PublicKey}
Endpoint = {Server_IP}:51820
AllowedIPs = 192.168.0.0/16
PersistentKeepalive = 25
进阶
利用wireguard实现远程访问家里网络内任意设备
我是在openwrt
上安装的wireguard
,你也可以在家里其他路由器上安装
- 按上述组好网络,openwrt打开防火墙,允许 WireGuard 流量通过
- 查看是否开启IP转发功能
sysctl net.ipv4.ip_forward
如果该命令返回 net.ipv4.ip_forward = 1,那么 IP 转发功能已经启用了。如果没有,需要修改/etc/sysctl.conf
,在该文件中添加
net.ipv4.ip_forward=1
然后,运行sysctl -p
命令应用更改。
- /32 表示单个IP地址,例如:192.168.1.1/32 表示只包括IP地址192.168.1.1。
- /24 表示一个子网,有256个IP地址,例如:192.168.1.0/24 包括从192.168.1.0到192.168.1.255的所有IP地址。
- /16 表示一个更大的子网,有65536个IP地址,例如:192.168.0.0/16 包括从192.168.0.0到192.168.255.255的所有IP地址。
- /8 表示一个更大的子网,有16777216个IP地址,例如:10.0.0.0/8 包括从10.0.0.0到10.255.255.255的所有IP地址。
K8S组网
待完善
参考文章
- 填坑指南
- WireGuard 安装与使用
- 如何在Debian 11安装WireGuard VPN
- 使用 WireGuard 搭建 VPN 访问家庭内网
- CentOS 7 安装 WireGuard 详细教程
- https://blog.csdn.net/qq_42766492/article/details/122159479
- https://juejin.cn/post/7080010903256563748#heading-2
- https://blog.csdn.net/x_mm_c/article/details/117999495
- https://dev.admirable.pro/using-wireguard/
- https://outti.me/use_google_one_vpn