内网无权限机器通过可上网权限Centos上网

拓扑如上图,在core这个网络中的机器网段都是10.57.0.0/16 ,在10.57.22.1下只有10.57.22.7有上网权限,其它机器无上网权限。控制是做在出口防火墙,如何让其它10.57.22.X的机器可以上网呢?

思路:需要做的是把10.57.22.7 变成一个路由器,把其它22网段的地址转成22.7。

实现:

10.57.22.7的配置

#禁用系统自带FIREWALLD
systemctl stop firewalld
systemctl disable firewalld

#安装IPTABLES
yum -y install iptables-services

#启用IP转发
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

#关源路由验证
sysctl -w net.ipv4.conf.eth0.rp_filter=0

#做源地址转换
iptables -t nat -A POSTROUTING -s 10.57.22.0/24 -d 0.0.0.0/0 -j SNAT --to 10.57.22.7
service iptables save
service iptables restart
#查看
iptables -t nat -nL

10.57.22.X的配置

#为保证内网走原网关,增加本地路由。保证从我的电脑SSH不断
route add -net 10.57.0.0/16 gw 10.57.22.1

#增加默认路由
route add -net 0.0.0.0/0 gw 10.57.22.7route add -net 0.0.0.0/0 gw 10.57.22.7

#删除到原网关的默认路由
 route del default gw 10.57.22.1

#查看当前路由
[root@10-57-22-134 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.57.22.7         0.0.0.0         UG    0      0        0 eth0
10.57.0.0       10.57.22.1      255.255.0.0     UG    0      0        0 eth0
10.57.22.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0

#测试OK
[root@10-57-22-134 ~]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=52 time=29.3 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=52 time=31.2 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=52 time=31.0 ms

以上路由操作是直接在命令中实现,重启会消失,需要永久生效请写在配置文件中。

vi /etc/sysconfig/network-scripts/route-eth0
#格式如下
10.57.0.0 /16 via 10.57.22.1 dev eth0
0.0.0.0 /16 via 10.57.22.7 dev eth0

#windows系统操作路由的命令差不多如下:
route add 10.57.0.0 mask 255.255.0.0 10.57.22.1
route delete 10.57.0.0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容