使用 tinc 设置 VPN
![]() 虽然这对一小部分用户可能有用,但这实际上与 Alpine Linux 没有直接关系,应该放在 tinc 文档中(讨论) |
这些说明将创建一个路由网状网络,每个节点后面有多个受保护的网络。虽然可以设置具有单独 VPN 名称的单独 tinc 守护进程,但我们将通过单个 tinc VPN “中继”所有流量。这些说明不会创建扩展的桥接 “以太网 LAN” - 它创建了一组路由网络。
网络拓扑
我们的示例网络拓扑如下所示。Example.com 有三个办公室:Aspen、Boulder 和 Carbondale。每个办公室有两个网络。Alpine Linux 用作每个办公室的防火墙/路由器/网关,tinc 将安装在网关上。
ASPEN [10.1.0.1] --------------\ | 192.168.10.0/24 | 192.160.110.0/24 [INTERNET]------------------ [10.3.0.1] CARBONDALE | | 192.168.30.0/24 BOULDER [10.2.0.1] --------------/ 192.168.130.0/24 192.168.20.0/24 192.168.120.0/24
Tinc VPN 本身将使用专用网络 192.168.0.0/29。
安装和配置通用 Tinc 设置
在所有三个路由器上
安装 Tinc
apk add tinc
加载 Tun 模块
modprobe tun
echo "tun" >> /etc/modules
创建 Tinc 配置的目录树
We need to create a name for our VPN. In this example, we will call it "mesh". A network interface will be created with the network name.
mkdir -p /etc/tinc/mesh/hosts
告诉 tinc 守护进程要加载的网络
echo NETWORK: mesh > /etc/conf.d/tinc.networks
安装和配置每个服务器的设置
在每个路由器上,创建一个 /etc/tinc/mesh/tinc.conf 文件。此示例适用于 Aspen
Name=aspen Device=/dev/net/tun
在其他服务器上将 Name 更改为 Boulder 和 Carbondale。
在每个路由器上,创建一个 /etc/tinc/mesh/tinc-up 脚本。同样适用于 Aspen
# This is for Aspen ip link set $INTERFACE up ip addr add 192.168.0.1/29 dev $INTERFACE # route TO Aspen (leave commented out on Aspen # uncomment on the other two) # ip route add 192.168.10.0/24 dev $INTERFACE # ip route add 192.168.110.0/24 dev $INTERFACE # route TO Boulder (leave commented out on Boulder # uncomment on the other two) ip route add 192.168.20.0/24 dev $INTERFACE ip route add 192.168.120.0/24 dev $INTERFACE # route TO Carbondale (leave commented out on Carbondale # uncomment on the other two) ip route add 192.168.30.0/24 dev $INTERFACE ip route add 192.168.130.0/24 dev $INTERFACE
ip route 语句告诉本地网关将发往其他两个园区的流量通过 tinc VPN 接口路由。
使脚本可执行
chmod a+x /etc/tinc/mesh/tinc-up
创建站点特定的配置文件
每个站点都有一个特定的配置文件,该文件将与所有其他站点共享。
Aspen
创建 /etc/tinc/mesh/hosts/aspen
Subnet = 192.168.0.1/32 Address = 10.1.0.1 ConnectTo = boulder ConnectTo = carbondale Subnet = 192.168.10.0/24 Subnet = 192.168.110.0/24
Boulder
创建 /etc/tinc/mesh/hosts/boulder
Subnet = 192.168.0.2/32 Address = 10.2.0.1 ConnectTo = aspen ConnectTo = carbondale Subnet = 192.168.20.0/24 Subnet = 192.168.120.0/24
Carbondale
创建 /etc/tinc/mesh/hosts/carbondale
Subnet = 192.168.0.3/32 Address = 10.3.0.1 ConnectTo = aspen ConnectTo = boulder
Subnet = 192.168.30.0/24 Subnet = 192.168.130.0/24
请注意,虽然在 tinc-up 脚本中我们指定了 /29 掩码(整个广播域),但主机文件包含 /32 掩码。这可能违反直觉,但它允许 tinc 守护进程知道哪些广播数据包用于此实例。
另请注意,虽然我们在 tinc-up 脚本中添加了所有其他网络的路由,但我们在主机文件中仅添加了此实例的子网。
ConnectTo 语句连接到其他两个节点。这将创建一个网状网络。如果所有节点之间都有显式的 ConnectTo 语句,那么例如,如果 Aspen 和 Carbondale 之间的连接丢失,流量将流经 Aspen->Boulder->Carbondale。
创建公钥和私钥
在每个节点上,运行
tincd -n mesh -K
它将生成公共和私有 RSA 密钥,并提示您是否可以将其放入
/etc/tinc/mesh/rsa_key.priv /etc/tinc/mesh/hosts/hostname
这是可以接受的。
将主机文件复制到其他主机
对于每个节点,使用 scp(或其他方式)将 /etc/tinc/mesh/hosts/hostname 文件复制到其他节点。最后,所有三个节点上的 hosts 目录将具有三个相同的文件。
运行中的 tinc 配置的目录树
/etc/tinc /etc/tinc/mesh /etc/tinc/mesh/rsa_key.priv <- unique to each host /etc/tinc/mesh/tinc.conf <- unique to each host /etc/tinc/mesh/tinc-up <- unique to each host /etc/tinc/mesh/hosts /etc/tinc/mesh/hosts/aspen <- same on all hosts /etc/tinc/mesh/hosts/boulder <- same on all hosts /etc/tinc/mesh/hosts/carbondale <- same on all hosts
启动 tincd
rc-update add tincd openrc lbu ci
如果网关转发 ipv4,并且站点之间没有其他防火墙规则,您应该能够从任何其他站点 ping 任何主机。