Linux 高级路由策略控制配置:两个不同路由子网间通信

场景:

Linux 路由服务器,设备上存在两个子网路由段

1、eth0 192.168.0.0/24 网卡IP为:192.168.0.8

2、ppp 10.0.0.0/24 网卡IP为:10.0.0.244

现期望,192.168.0.0子网接入设备(如:192.168.0.24),可以通过该 Linux 路由器跨域访问 10.0.0.251。

方案:

1、修改 /etc/sysctl.conf 文件

bash 复制代码
net.ipv4.conf.all.route_localnet=1
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
net.ipv4.conf.default.forwarding=1

net.ipv6.conf.all.forwarding = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.lo.forwarding = 1
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.lo.disable_ipv6 = 0

net.ipv6.conf.all.accept_ra = 2
net.ipv6.conf.default.accept_ra = 2

net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.all.rp_filter=0

添加上述的内核选项配置,并且通过 sysctl -p 命令生效路由转发。

2、配置 iptables SNAT路由转发配置。

192.168.0.0/24 为 eth0 网卡输入的报文,将其转发给 ppp 网卡并修改 SourceIP 为

10.0.0.1(网关IP)或 10.0.0.244(网卡接口IP)

bash 复制代码
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ppp -j SNAT --to-source 10.0.0.1

若设置为网关IP,必须设置 -o 具体输出网卡,-i 为输入网卡。

替代方案:

bash 复制代码
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 10.0.0.244

3、如果不采用,第二项指出的路由转发配置,可以使用以下的替代方案。

bash 复制代码
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ppp -j MASQUERADE

上述命令行强制设置输出网卡接口,该命令会在内核中自动使用网卡的接口SNAT。

bash 复制代码
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE

上述命令行没有强制输出网卡接口,该命令会在内核中自动根据路由表设置SNAT。

测试效果:

相关推荐
sniper_fandc4 分钟前
网络编程—TCP/IP模型(UDP协议与自定义协议)
网络·tcp/ip·udp·javaee
小小鸭程序员9 分钟前
零基础教程:Windows电脑安装Linux系统(双系统/虚拟机)全攻略
linux·运维·服务器·mysql·spring
会飞的土拨鼠呀13 分钟前
开源酷炫的Linux监控工具:sampler
linux·运维·开源
sniper_fandc1 小时前
网络编程—TCP/IP模型(IP协议)
网络·tcp/ip·ip
_midnight1 小时前
Python 并发编程指南:协程 vs 多线程及其他模型比较
linux·python
CYRUS_STUDIO1 小时前
Frida 调用 kill 命令挂起&恢复 Android 线程
android·linux·逆向
GalaxyPokemon1 小时前
MySQL基础 [六] - 内置函数+复合查询+表的内连和外连
linux·运维·数据库·mysql·ubuntu
Linux运维老纪2 小时前
Linux 命令清单(Linux Command List)
linux·运维·服务器·数据库·mysql·云计算·运维开发
艾醒(AiXing-w)2 小时前
Linux系统管理(十九)——欧拉系统硬盘挂载、网络配置以及Docker环境安装
linux·网络·docker
参.商.2 小时前
【RH124】第六章 管理本地用户和组
linux·运维