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。

测试效果:

相关推荐
水饺编程29 分钟前
第2章:Linux 基础,切换用户
linux·c语言·汇编
Lsetea1 小时前
curl指定--tlsv1.2仍连接TLS1.3:最低版本与tls-max排查
linux·https·ssl证书·curl·tls
Lsetea1 小时前
curl访问HTTPS报47 Too many redirects:重定向循环与max-redirs排查
linux·http·https·ssl证书·curl
阿文和她的Key1 小时前
PHP2Shell事故四层拆解:AI找洞是小时级,披露协调还是邮件级
网络·人工智能·架构·php
9624563 小时前
一次 Dell PowerEdge R750xs 启动故障复盘:从 iDRAC 带外管理到 Linux 多内核回退与 initramfs 重建
linux·运维·服务器
QZ166560951593 小时前
数据库监测的主动防御:实时感知,看见即掌控
网络·安全·网络安全
Zevalin爱灰灰3 小时前
Linux入门第一部分(Ubuntu入门篇) 第二章 浅尝Ubuntu(二)
linux·嵌入式·1024程序员节
北京中科新远科技4 小时前
高速接口兼容排查实战:OSFP、QSFP112与QSFP56如何逐层验证
linux·运维·网络
Ruiery4 小时前
Linux 6.6内核 CPU 深度解析(零):x86 内核引导协议 — 从 bootloader 到 main()
linux·运维·服务器
Ruiery4 小时前
Linux 6.6内核 CPU 深度解析(十六):x86 保护机制 — 从 ring 到页表的权限防线
linux·运维·服务器