Linux--iptables防火墙SNAT、DNAT策略配置

前言

NAT包含DNAT和SNAT

DNAT:目标地址转换(Destination Network Address Translation)是Linux防火墙的一种地址转换操作,是iptables命令中的一种数据包控制类型,其作用是根据指定条件修改数据包的目标IP和目标端口

SNAT:源地址转换(Source Network Address Translation)也是Linux防火墙的一种地址转换操作,也是iptables命令中的一种数据包控制类型,其作用是根据指定条件修改数据包的源IP地址

一:实验环境

1.1:环境准备

VMware软件
Windows虚拟机作为客户端,IP地址为 12.0.0.12
一台centos7虚拟机作为防火墙,IP地址为12.0.0.1和192.168.10.1
一台centos7虚拟机作为局域网web服务器,IP地址为192.168.10.10
关闭所有主机的Firewalld服务,安装iptables-server,开启iptables防火墙,清空所有规则,内网和外网web服务器安装httpd服务并开启
开启防火墙服务器的路由转发功能:

二:实验过程

2.1:Windows客户机设置

在这里插入图片描述
在这里插入图片描述

2.2:web服务器设置

网卡为NAT模式,先安装httpd服务

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd

修改为仅主机模式
在这里插入图片描述
编辑网卡,设置IP地址

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
...省略内容
BOOTPROTO="static"	将dhcp修改为static
...省略内容,末尾添加下面内容
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
[root@localhost ~]# systemctl restart network
[root@localhost ~]# ifconfig

清空防火墙规则

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -F

2.3:防火墙设置

2.3.1:配置网卡

添加一张网卡,两张网卡都改为仅主机模式
在这里插入图片描述
修改网卡IP地址

[root@firewall ~]# cd /etc/sysconfig/network-scripts/
[root@firewall network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@firewall network-scripts]# vim ifcfg-ens33
...省略内容
BOOTPROTO="static"	将dhcp修改为static
...省略内容。添加以下内容
IPADDR=192.168.10.1
NETMASK=255.255.255.0
[root@firewall network-scripts]# vim ifcfg-ens36
...省略内容
BOOTPROTO="static" 将dhcp修改为static
...省略内容
NAME="ens36"	将ens33修改为ens36
    		     删除UUID
DEVICE="ens36"	将ens33修改为ens36
添加以下内容
IPADDR=12.0.0.1
NETMASK=255.255.255.0
[root@firewall network-scripts]# systemctl restart network
[root@firewall network-scripts]# ifconfig

2.3.2:关闭Linux虚拟机的防火墙,开启路由转发功能

[root@firewall network-scripts]# iptables -F	   清空防火墙规则
[root@firewall network-scripts]# iptables -t nat -F	  清空DNAT和SNAT规则
[root@firewall network-scripts]# vim /etc/sysctl.conf 
..省略内容,末行添加下段内容
net.ipv4.ip_forward=1	  开启路由转发功能
[root@firewall network-scripts]# sysctl -p	  刷新sysctl.conf配置
net.ipv4.ip_forward = 1

2.3.3:设置DNAT和SNAT地址映射

[root@firewall ~]# iptables -t nat -I PREROUTING -d 12.0.0.1 -p tcp --dport 80 -i ens36 -j DNAT --to-destination 192.168.10.10
[root@firewall ~]# iptables -t nat -I POSTROUTING -s 192.168.10.10/24 -o ens36 -j SNAT --to-source 12.0.0.1

三:实验结果验证与总结

3.1:实验结果验证

客户端访问
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47151650/article/details/107851247