Linux 双网卡,tun0 如何共享 eth0 的网络

452 views
Skip to first unread message

naison

unread,
Jul 18, 2021, 4:44:13 AM7/18/21
to go-gost
博主好,我这里使用了 gost 的 tun 功能,创建了一个简单的 VPN 。gost创建的 tun 设备的网络是可以互通的 ,但是和tun处于同一台机器的eth0,所在的网络,使用tun0设备无法访问,请问有什么方法可以访问到吗?

远端:
Linux 服务器具有两个网卡
tun0(192.168.31.100/24):使用gost tun功能创建的虚拟网卡
eth0(172.20.0.1/24):Linux服务器自带的网卡

本地:
tun(192.168.31.101/24):然后使用 —F,传输层使用的是gost提供的ssh服务
同时设置路由规则,使得172.20.0.1/24网段的数据通过tun设备,发送到远端

示意图:

              local                                                                                      remote
-------------------------------------------                                         --------------------------------------------------
|       192.168.31.100/24      |.    gost -F ssh               |     tun0: 192.168.31.101/24     |---------
|               tun0                        |      -------------------------->   |                                                      |      |not ok
|       172.20.0.1/24               |                                        |     eth0 : 172.20.0.1/24             | <-----|
-------------------------------------------                                         | ----------------------------------------------- |


在本地ping远端
192.168.31.101    -->.   192.168.31.100/24.   ok
192.168.31.101.   -->.   172.20.0.1/24.            not ok
在远端ping本地
192.168.31.100    -->.  192.168.31.101           ok

从本地 ping server 的 172网段,日志可以看出,数据包已经发送到server的tun0设备,但是tun0到不了eth0,请问该如何配置尼?不胜感激

相关命令:
本地:
远端:
gost -L "ssh://:2222" -L "tun://:8421?net=192.168.31.100/24"


server路由
/root # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.20.0.1      0.0.0.0         UG    0      0        0 eth0
172.20.0.0      *               255.255.255.0   U     0      0        0 eth0
192.168.31.0   *               255.255.255.0 U     0      0        0 tun0

naison

unread,
Jul 18, 2021, 11:16:35 AM7/18/21
to go-gost

已经找到解决方法了,就是ip_forward
添加如下命令即可:
iptables -t nat -A POSTROUTING -s 192.168.31.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 172.20.0.0/24 -o eth0 -j MASQUERADE
Reply all
Reply to author
Forward
0 new messages