步骤一:调整路由转发环境
本案例用到了192.168.4.0/24、174.16.16.0/24两个网段,若希望将Linux网关作为路由器使用,使两个网段互通,路由方面首先要解决以下两个问题:
为192.168.4.0/24网段的客户机添加到174.16.16.0/24网段的路由
为174.16.16.0/24网段的客户机添加到192.168.4.0/24网段的路由
其中,内网测试机svr5已经设置好默认网关,因此第一个问题解决;而第二个问题,只要为外网测试机pc120也添加默认网关(或者具体的静态路由)即可:
[root@pc205 ~]# route add default gw 174.16.16.1
[root@pc205 ~]# route -n | grep UG
0.0.0.0 174.16.16.1 0.0.0.0 UG 0 0 0 eth0
步骤二:利用ip_forward机制实现Linux路由/网关功能
1) 开启路由之前,内外网无法互通
在主机svr5上ping主机pc120,丢包率100%:
[root@svr5 ~]# ping -c4 -W2174.16.16.120
ping: bad linger time.
[root@svr5 ~]# ping -c4 -W2 174.16.16.120
PING 174.16.16.120 (174.16.16.120) 56(84) bytes of data.
--- 174.16.16.120 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 5002ms
[root@svr5 ~]#
在主机pc120上ping主机svr5,丢包率100%:
[root@pc205 ~]# ping -c4 -W2 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
--- 192.168.4.5 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 5002ms
[root@pc205 ~]#
2)开启Linux网关的路由转发功能
使用sysctl可以直接修改运行中的ip_forward参数:
[root@gw1 ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0 //默认未开启
[root@gw1 ~]# sysctl -w net.ipv4.ip_forward=1 //开启转发功能
net.ipv4.ip_forward = 1
若希望固定此配置,推荐修改/etc/sysctl.conf配置文件:
[root@svr5 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 //查找更改为此行
[root@svr5 ~]# sysctl -p //更新配置
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter &#