文章目录
前言
- 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
1.2:实验拓扑图
1.3:实验目的
- 通过NAT转换,客户机和web服务器能够互相访问
二:实验过程
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:配置网卡
-
添加一张网卡,两张网卡都改为仅主机模式
扫描二维码关注公众号,回复: 8512580 查看本文章 -
修改网卡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.2:设置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:实验结果验证
-
客户端访问
-
web服务器查看日志访问记录
[root@localhost httpd]# cd /var/log/httpd [root@localhost httpd]# cat access_log
3.2:实验总结
-
DNAT:在路由前转换地址,称为PREROUTING
SNAT:在路由后转换地址,称为POSTROUTING
-
生产环境中,不仅要设置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
f
…省略内容,末行添加下段内容
net.ipv4.ip_forward=1 ‘//开启路由转发功能’
[root@firewall network-scripts]# sysctl -p ‘//刷新sysctl.conf配置’
net.ipv4.ip_forward = 1