本文通过虚拟机部署了三台CentOS6.10的主机来实现,其中一台主机使用了双网卡
注:下文中将实现路由转换的主机称为NAT主机,设定为仅主机模式的主机称为A主机,设定为NAT模式的主机称为B主机
#如果此项不为1,则修改为1 [root@mail ~]# cat /proc/sys/net/ipv4/ip_forward 1 #永久修改ip_forward,将net.ipv4.ip_forward参数设置为1 [root@mail ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 #重新载入一下sysctl参数 [root@mail ~]# sysctl -p net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 #查看网卡1的配置 [root@mail ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.88.135 PREFIX=24 GATEWAY=192.168.88.2 DNS1=114.114.114.114 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" #查看网卡2的配置 [root@mail ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=172.16.100.7 PREFIX=24 GATEWAY=172.16.100.1 DNS1=114.114.114.114 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth1" #注:如出现网卡启动不了,删除/etc/udev/rules.d/70-persistent-net.rules中的内容,重新启动服务即可
#修改网卡的配置文件 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=172.16.100.9 PREFIX=24 GATEWAY=172.16.100.7 DNS1=114.114.114.114 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0"
修改完成后,ping一下NAT主机,连接正常
#查看网卡配置文件 [root@lotus ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.88.137 PREFIX=24 GATEWAY=192.168.88.135 DNS1=114.114.114.114 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" #查看pingNAT主机的结果 [root@lotus ~]# ping 192.168.88.135 PING 192.168.88.135 (192.168.88.135) 56(84) bytes of data. 64 bytes from 192.168.88.135: icmp_seq=1 ttl=64 time=0.389 ms 64 bytes from 192.168.88.135: icmp_seq=2 ttl=64 time=0.256 ms 64 bytes from 192.168.88.135: icmp_seq=3 ttl=64 time=0.350 ms ... ... #ping一下A主机的IP地址,连接成功 [root@lotus ~]# ping 172.16.100.9 PING 172.16.100.9 (172.16.100.9) 56(84) bytes of data. 64 bytes from 172.16.100.9: icmp_seq=1 ttl=63 time=0.398 ms 64 bytes from 172.16.100.9: icmp_seq=2 ttl=63 time=0.791 ms 64 bytes from 172.16.100.9: icmp_seq=3 ttl=63 time=0.526 ms 64 bytes from 172.16.100.9: icmp_seq=4 ttl=63 time=0.522 ms