Centos7内网服务器配置SNAT上外网
引言
有A和B 2台linux机器,A和B可以互通,但是A可以上百度,B不能上百度,那么如何才能让B也上百度呢???
环境介绍
环境:
服务器A可以上外网:内网192.168.6.22;外网172.17.254.100
服务器B只能上内网:内网192.168.6.23
需求:
如何配置,才可以让服务器B也可以上外网???
配置
配置步骤:
1、 在所有主机上关闭firewad
1 2 3 4 5 | # 安装防火墙组件 yum install -y firewalld iptables-services systemctl stop firewalld systemctl disable firewalld |
2、代理(服务器A)上打开内核转发
1 2 3 4 5 | # 开启ip_forward转发 echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf sysctl -p /etc/sysctl.conf sysctl -p |
3、 在服务器A上做SNAT源地址转换规则
1 2 3 4 5 6 7 8 | -- 在服务器A上做SNAT源地址转换规则 iptables -t nat -A POSTROUTING -s 192.168.6.0/24 -j SNAT --to-source 192.168.6.22 -- 查询NAT规则 iptables -t nat -nL --line-number -- 删除POSTROUTING第一条规则 iptables -t nat -D POSTROUTING 1 |
其它命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | iptables -I INPUT -j ACCEPT iptables -t nat -I POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 172.17.254.109 iptables -A FORWARD -s 129.10.25.0/24 -j ACCEPT iptables -A FORWARD -d 129.10.25.0/24 -j ACCEPT iptables -t nat -A POSTROUTING -s 129.10.25.0/24 -j SNAT --to-source 172.17.254.109 iptables -t nat -A POSTROUTING -j SNAT --to-source 129.10.25.24 iptables -t nat -A POSTROUTING -j SNAT --to-source 129.10.25.25-129.10.25.28 iptables -t nat -A POSTROUTING -s 129.10.25.0/24 -j MASQUERADE iptables -t nat -I POSTROUTING -j MASQUERADE iptables -t nat -A POSTROUTING -s 129.10.25.0/24 -j SNAT --to 172.17.254.109 |
4、在内网的机器(服务器B)上删除原网关,添加内网的网关
1 2 | route delete default gw 192.168.6.1 route add default gw 192.168.6.22 |
5、服务器B重启后失效,需要重新执行第4步
总结
1、其实如果你时间过一张网卡的情况时,那么你会发现只要开启了内核转发功能之后不需要配置iptables也是可以行的。
2、当两张网卡是情况,实体机,一张连接外网,一张内网连接交换机,再连接多个子PC,此时iptables就是必须的,且SNAT也必须为外网网卡IP。
参考
https://mp.weixin.qq.com/s/IKMV_4AaFU3gD3kh9NVlzw
https://blog.csdn.net/qq_42869878/article/details/115696712
https://www.cnblogs.com/weifeng1463/p/6795939.html
https://blog.csdn.net/qq_39677803/article/details/126849441