0%

debian部署tinc组网

Tinc 是一个开源、跨平台的虚拟私有网络(VPN),支持TCP、UDP,IPV4、IPV6组网,相较于Wireguard、Tailscale等基于UDP的VPN,能有更好的连接表现!

主机A:有公网IP,做主节点,本机局域网IP为 192.168.2.111,内网网段 192.168.2.0/24,设置VPN主机名为master,VPN IP为 10.0.0.1

主机B:无公网IP,做子节点,本机局域网IP为 192.168.3.57,内网网段 192.168.3.0/24,设置VPN主机名为tinc1,VPN IP为 10.0.0.2

最终实现:

1、在主机A上可ping通 10.0.0.2,192.168.3.57 以及 192.168.3.0/24 网段所有IP

2、在主机B上可ping通 10.0.0.1,192.168.2.111 以及 192.168.2.0/24 网段所有IP

部署主节点

创建tinc.conf文件

1
2
3
4
5
cat > /etc/tinc/tinc_net/tinc.conf << EOF
Name = master
Interface = tinc
Mode = switch
EOF

创建tinc-up文件

tinc启动时运行的文件,用来创建接口,添加路由

10.0.0.1 为本机VPN IP,可修改

10.0.0.2 为对端机器VPN IP,根据需要修改

192.168.3.0 对端机器内网网段,根据需要修改

1
2
3
4
5
6
7
cat > /etc/tinc/tinc_net/tinc-up << EOF
#!/bin/sh
ip link set $INTERFACE up
ip addr add 10.0.0.1/24 dev $INTERFACE
ip route add 192.168.3.0/24 via 10.0.0.2 dev $INTERFACE src 10.0.0.1 onlink
EOF
chmod +x /etc/tinc/tinc_net/tinc-up

创建tinc-down文件

tinc关闭时运行的文件,用来删除路由,关闭接口

10.0.0.1 为本机VPN IP,可修改

192.168.3.0 对端机器内网网段,根据需要修改

1
2
3
4
5
6
7
cat > /etc/tinc/tinc_net/tinc-down << EOF
#!/bin/sh
ip route del 192.168.3.0/24
ip addr del 10.0.0.1/24 dev $INTERFACE
ip link set $INTERFACE down
EOF
chmod +x /etc/tinc/tinc_net/tinc-down

创建主机master文件

domain.com 为公网IP或者公网IP绑定的域名,可为ddns域名

10.0.0.1 为本机VPN IP,可修改

11391,监听端口,根据需要修改

1
2
3
4
5
cat > /etc/tinc/tinc_net/hosts/master << EOF
Address = domain.com
Subnet = 10.0.0.1/32
Port = 11391
EOF

创建密钥

需要两次回车

1
tincd -n tinc_net -K

部署子节点

创建tinc.conf文件

1
2
3
4
5
cat > /etc/tinc/tinc_net/tinc.conf << EOF
Name = tinc1
Interface = tinc
Mode = switch
EOF

创建tinc-up文件

tinc启动时运行的文件,用来创建接口,添加路由

10.0.0.2 为本机VPN IP,可修改

10.0.0.1 为对端机器VPN IP,根据需要修改

192.168.2.0 对端机器内网网段,根据需要修改

1
2
3
4
5
6
7
cat > /etc/tinc/tinc_net/tinc-up << EOF
#!/bin/sh
ip link set $INTERFACE up
ip addr add 10.0.0.2/24 dev $INTERFACE
ip route add 192.168.2.0/24 via 10.0.0.1 dev $INTERFACE src 10.0.0.2 onlink
EOF
chmod +x /etc/tinc/tinc_net/tinc-up

创建tinc-down文件

tinc关闭时运行的文件,用来删除路由,关闭接口

10.0.0.2 为本机VPN IP,可修改

192.168.2.0 对端机器内网网段,根据需要修改

1
2
3
4
5
6
7
cat > /etc/tinc/tinc_net/tinc-down << EOF
#!/bin/sh
ip route del 192.168.2.0/24
ip addr del 10.0.0./24 dev $INTERFACE
ip link set $INTERFACE down
EOF
chmod +x /etc/tinc/tinc_net/tinc-down

创建主机tinc1文件

10.0.0.2 为本机VPN IP,可修改

1
2
3
cat > /etc/tinc/tinc_net/hosts/tinc1 << EOF
Subnet = 10.0.0.2/32
EOF

创建密钥

需要两次回车

1
tincd -n tinc_net -K

交换公钥

公钥文件为:

1
/etc/tinc/tinc_net/hosts/

目录下的主机文件,将其相互复制到对应的主机即可!

自行通过各种方式将密钥,例如scp,winscp,ftp……

分别启动tuic

1
2
systemctl enable tinc@tinc_net
systemctl start tinc@tinc_net

防火墙实现IP动态伪装

必须有防火墙做IP动态伪装,否则无法联通对端内网所有IP

示例为debian网络出口为eth0的情况下

iptables

1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

nftables

  • 临时生效
1
2
3
nft add table ip nat
nft add chain ip nat postrouting { type nat hook postrouting priority 100 \; }
nft add rule ip nat postrouting oifname "eth0" masquerade
  • 长期有效

写入规则

1
2
3
4
5
6
7
8
cat >> /etc/nftables.conf << EOF
table ip nat {
chain postrouting {
type nat hook postrouting priority 100; policy accept;
oifname "eth0" masquerade;
}
}
EOF

加载使其生效

1
nft -f /etc/nftables.conf