linux使用snat转发的问题
source link: https://www.wencst.com/archives/1446
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
linux使用snat转发的问题
在多台主机的情况下,仅有一台主机能够访问外网。为了使很多主机都可以访问外部网络的情况下使用。
期初,我也认为这种场景几乎不可能出现的,除非有特殊的应用场景才可能。其实这种情况其实非常多见,比如我们使用的阿里云服务。为了节省成本,我们买了N多服务器,但只买了少量的IP。在阿里云中,当云主机不绑定IP时,云主机是无法访问外网的,于是就想到了使用SNAT来做转发。
登录到能够访问外网的服务器中
开启IP转发功能
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
sysctl -p
使之生效。iptables -I FORWARD -s 172.16.3.0/24 -j ACCEPT
添加SNAT转换
iptables -t nat -I POSTROUTING -s 172.16.3.0/24 -j SNAT --to-source 172.16.3.2
为其他机器配置转发
保存以上配置
命令:systemctl status iptables
yum install iptables-services
systemctl status iptables
systemctl enable iptables.service
systemctl start iptables.service
临时关闭命令:setenforce 0
把红框中SELINUX的修改为“SELINUX=disabled
”即可,保存并推出,即关闭成功。
6.保存iptables
service iptables save
iptables -F
service iptables restart
service iptables restart
iptables -t nat -nL
sed -i 's/net.ipv4.ip_forward = 1/net.ipv4.ip_forward = 0/g' /etc/sysctl.conf
执行sysctl -p
使之生效。
删除iptables方法
当配置错误,或者配置重复时,为了干净整洁,需要删除iptables配置。
命令:iptables -nL FORWARD --line-number
iptables -D FORWARD 1
iptables -t nat -D POSTROUTING 1
iptables -t nat -D POSTROUTING 7
iptables -t nat -A POSTROUTING -s 172.28.101.111/255.255.255.0 -o ens33 -j MASQUERADE
如果文章对您有用,扫一下支付宝的红包,不胜感激!
欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)
Leave a Reply Cancel reply
You must be logged in to post a comment.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK