LInux--firewalld{NAT地址转换实验(SNAT和DNAT以及路由转发功能)},防火墙必看篇
- 前言
- 一:NAT实验
- 1.1:实验环境
- 1.2:实验目的
- 1.3:实验分析图
- 1.4::实验步骤
- 1.4.1:linux防火墙
- 1.4.2:web虚拟机配置
- 1.4.3:客户机配置
- 1.5:实验结果
- 1.6:实验总结
前言
- NAT包含DNAT和SNAT
- DNAT:目标地址转换(Destination Network Address Translation)是Linux防火墙的一种地址转换操作,是iptables命令中的一种数据包控制类型,其作用是根据指定条件修改数据包的目标IP和目标端口
- SNAT:源地址转换(Source Network Address Translation)也是Linux防火墙的一种地址转换操作,也是iptables命令中的一种数据包控制类型,其作用是根据指定条件修改数据包的源IP地址
一:NAT实验
1.1:实验环境
- VMware软件
- Windows 10 虚拟机作为客户端,IP地址为 12.0.0.12
- 一台centos 7.6 虚拟机作为防火墙,IP地址为12.0.0.1和192.168.10.1
- 一台centos 7.6 虚拟机作为局域网web服务器,IP地址为192.168.10.10
1.2:实验目的
通过NAT,实现外网和内网之间相互访问
1.3:实验分析图
1.4::实验步骤
1.4.1:linux防火墙
- 添加网卡,并选择仅主机
- 修改网卡信息
[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 改IP地址
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 修改IP地址
NETMASK=255.255.255.0
[root@firewall network-scripts]# systemctl restart network
[root@firewall network-scripts]# ifconfig
- 关闭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 ..省略内容 shirt+g到末行添加以下内容net.ipv4.ip_forward=1 开启路由转发功能[root@firewall network-scripts]# sysctl -p 刷新sysctl.conf配置net.ipv4.ip_forward = 1
- 设置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
1.4.2:web虚拟机配置
- 网卡为NAT模式,先安装httpd服务
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd
- 修改为仅主机模式
- 修改网卡信息
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33...省略内容BOOTPROTO="static" 将dhcp修改为static...省略内容IPADDR=192.168.10.10 修改IP信息NETMASK=255.255.255.0GATEWAY=192.168.10.1[root@localhost ~]# systemctl restart network 重启网卡信息[root@localhost ~]# ifconfig
- 清空防火墙规则
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -F 清空DNAT和SNAT规则
1.4.3:客户机配置
-
设置网卡为仅主机模式
-
修改IP地址,子网掩码以及网关
1.5:实验结果
-
客户端访问
-
web服务器查看日志访问记录
[root@localhost httpd]# cd /var/log/httpd
[root@localhost httpd]# cat access_log
1.6:实验总结
-
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 开启路由转发功能[root@firewall network-scripts]# sysctl -p 刷新sysctl.conf配置net.ipv4.ip_forward = 1