iptables之SNAT与DNAT

iptables之SNAT与DNAT

SNAT策略概述
SNAT应用环境

SNAT 应用环境:局域网主机共享单个公网IP地址接入Internet (私有IP不能在Internet中正常路由)

SNAT原理

源地址转换,根据指定条件修改数据包的源IP地址,通常被叫做源映谢

SNAT转换前提条件:

1.局域网各主机已正确设置IP地址、子网掩码、默认网关地址
2.Linux网关开启IP路由转发

Linux系统本身是没有转发功能,只能路由发送数据
临时打开:
echo 1 > /proc/sys/net/ipv4/ip_forward
或
sysctl -w net.ipv4.ip_forward=1
永久打开:
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1    #将此行写入配置文件
SNAT转换:固定的公网IP地址
#配置SNAT策略,实现snat功能,将所有192.168.100.0这个网段的ip的源ip改为10.0.0.1
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j SNAT --to 10.0.0.1
                                    可换成单独IP  出站 外网网卡            外网IP
或
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j SNAT --to-source 10.0.0.1-10.0.0.10
                                        内网IP   出站 外网网卡                    外网IP或地址池
SNAT转换2:非固定的公网IP地址(共享动态IP地址):
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
配置SNAT案例

image-20220714192152094

web服务器ip:192.168.100.102(nat1)、关闭防火墙和selinux、开启http服务

网关服务器内网ip:192.168.100.100(nat1);外网ip:12.0.0.1(nat2)、关闭防火墙和selinux、开启http服务

win7客户端ip:12.0.0.100(nat2)

VMware的虚拟网络编辑器中默认nat模式网段:192.168.100.0,nat2模式网段:12.0.0.0

网关服务器配置两个虚拟网卡 一个连接内网一个连接外网

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZZ2UVD6U-1658046380810)(C:\Users\sun\AppData\Roaming\Typora\typora-user-images\image-20220717161817612.png)]

image-20220717161828301

复制并修改ens38网卡

#切换至网卡配置文件所在目录
[root@localhost network-scripts]#cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]#cp ifcfg-ens33 ifcfg-ens38

image-20220717161901158

修改ens33网卡

image-20220717161946186

重启网络

配置内网服务器(192.168.100.102)相关配置

image-20220717162102471

修改ens33网卡

image-20220717162155034

重启网络服务

image-20220717162224203

ping网关测试

image-20220717162241692

配置外网服务器(12.0.0.100)的相关配置

image-20220717162314778

修改ens33网卡

image-20220717162338220

重启网络服务

ping网关测试

开启SNAT
[root@localhost network-scripts]#
vim /etc/ sysctl. conf
net. ipv4.ip_ forward = 1	#将此行写入配置文件
 
[root@localhost network-scripts]#sysctl -P				#读取修改后的配置
配置网关服务器的iptables规则

安装iptables,关闭防火墙和selinux,开启iptables

[root@localhost network-scripts]#yum install -y iptables*
@localhost network-scripts]#systemctl stop firewalld.service 
[root@localhost network-scripts]#setenforce 0
[root@localhost network-scripts]#systemctl start iptables.service

查看网关服务器的iptables规则并清除

iptables -nL			#查看规则
iptables -nL -t nat		#查看规则
iptables -F				#清除iptables的规则
iptables -F -t nat		#清除iptables的规则
添加 SNAT转换∶固定的公网IP地址
[root@localhost yum.repos.d]#iptables -F -t nat
[root@localhost yum.repos.d]#iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j SNAT --to-source 12.0.0.1
[root@localhost yum.repos.d]#iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens38 -j SNAT --to-source 12.0.0.1
[root@localhost yum.repos.d]#iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
 
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.100.0/24     0.0.0.0/0            to:12.0.0.1
in POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.100.0/24     0.0.0.0/0            to:12.0.0.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值