使用 WireGuard 异地组网

多个服务器组建内网

为什么需要组建内网

异地多台服务器之间,建立安全的通信通道,能免去一些权限校验、保护内网通信安全。
比如把位于广州的服务器的 Docker 的 Daemon socket 分享给位于深圳的服务器操作, 如果通过公网进行通信的话,需要配置tlsverify来保证通信的安全,详情: Protect the Docker daemon socket
但是如果是内网的话,则可以直接指定监听接口和端口-H tcp://x.x.x.x:2375,x.x.x.x替换为本机内网ip,详情: Daemon socket option

关于 WireGuard

WireGuard 是一种更快更简单的vpn,在熟练之后,使用 WireGuard 搭建内网,只需要几分钟
使用 WireGuard 搭建内网,需要每台机子都要配置其他的所有机器为 peer,例如 ABC 三台机器组建内网,A 需要启动一个interface,并配置B、C两台机器为 peer;B、C机器亦然。
所以,如果是少量机器的话,WireGuard是非常好的选择,但是机器多了的话,可能其他的方案更合适

开始

内核要求:5.x
安装 apt update && apt install wireguard-tools
进入配置文件夹 cd /etc/wireguard
生成本机公私钥 privatekey publickey

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

配置wg0网络,编辑文件vi wg0.conf

# 编辑时需要删除所有的注释
[Interface]
Address = 192.168.2.1/24 # 设置要组建的内网的本机的ip,及其网段
ListenPort = 1234 # 指定一个监听端口
PrivateKey = xxx # 本机的 privatekey

[Peer]
PublicKey = xxx # 192.168.2.2 的 publickey
AllowedIPs = 192.168.2.2/32 # 路由设置,指定本机访问哪些ip转发到该peer
Endpoint = xxx:1234 # 该peer的地址
PersistentKeepalive = 60 # 在nat中定时发送心跳,保持 NAT 网络的端口

[Peer]
PublicKey = xxx # 192.168.2.3 的 publickey
AllowedIPs = 192.168.2.3/32
Endpoint = xxx:1234
PersistentKeepalive = 60

启动 wg-quick up wg0
关闭 wg-quick down wg0

开机启动 systemctl enable wg-quick@wg0

官网 Quick Start
https://www.wireguard.com/quickstart/

Built with Hugo
Theme Stack designed by Jimmy