目录
2.4SNAT转换2:非固定的公网IP地址(共享动态IP地址)
一、SNAT策略概述
SNAT(Source Network Address Translation,源地址转换)是Linux防火墙的一种地址转换操作,也是iptables命令中的一种数据包控制类型,其作用是根据特定的条件修改数据包的源IP地址。
1.1 SNAT应用环境
局域网主机共享单个公网IP地址的Internet(私有IP不能在Internet中正常路由)
1.2 SNAT原理
修改数据包源地址
1.3 SNAT转换的前提条件
局域网各主机已正确设置IP地址、子网掩码、默认网关地址
Linux网关开启IP路由转发
二、SNAT策略的应用
2.1临时打开
echo 1 >/proc/sys/net/ipv4/ip_forward
或
sysctl -w net.ipv4.ip forward=1
2.2 永久打开
vim /etc/ sysctl. conf
net. ipv4.ip_ forward = 1 #将此行写入配置文件
sysctl -P #读取修改后的配置
2.3SNAT转换1:固定的公网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或地址池
2.4SNAT转换2:非固定的公网IP地址(共享动态IP地址)
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
三、SNAT案例:
实验准备
web服务器ip:10.0.0.12(Vmnet3)、关闭防火墙和selinux、开启http服务
网关服务器内网ip:192.168.100.100(Vmnet2);外网ip:10.0.0.1(Vmnet3)、关闭防火墙和selinux、开启http服务
win7客户端ip:192.168.100.110(Vmnet2)
VMware的虚拟网络编辑器中默认Vmne1模式网段:192.168.100.0,Vmnet2模式网段:10.0.0.0
配置网关服务器(192.168.100.100/10.0.0.1)的相关配置
复制并修改ens37网卡
修改ens33
Systemctl restart network
开启SNAT
[root@localhost network-scripts]#
vim /etc/ sysctl. conf
net. ipv4.ip_ forward = 1 #将此行写入配置文件
[root@localhost network-scripts]#sysctl -P #读取修改后的配置
配置iptables规则
iptables -nL #查看规则
iptables -nL -t nat #查看规则
iptables -F #清除iptables的规则
iptables -F -t nat #清除iptables的规则
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j SNAT --to 12.0.0.1
//可换成单独的ip 出栈 外网网卡 外网ip
配置外网服务器(10.0.0.12)的相关配置
配置网卡ens33
重启网络
ping网关测试
重启网卡、关闭防火墙和增强、开启http服务
systemctl stop firewalld.service
setenforce 0
systemctl start httpd
配置客户机(192.168.100.110)
配置IP地址 验证结果:局域网主机192.168.100.110能够访问外网的web服务器10.0.0.12
四、DNAT策略与应用
4.1DNAT应用环境
在Internet中发布位于局域网内的服务器
4.2DNAT原理
修改数据包的目的地址
4.3 DNAT转换前提条件
局域网的服务器能够访问Internet
网关的外网地址有正确的DNS解析记录
Linux网关开启IP路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysct1 -p
4.4DNAT转换1∶ 发布内网的Web服务
#把从ens33进来的要访问web服务的数据包目的地址转换为 192.168.100.102
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp--dport 80 -j DNAT --to 192.168.100.102
或
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp--dport 80-j DNAT --to-destination 192.168.100.102
入站|外网网卡 | 外网ip 内网服务器ip
4.5DNAT转换2∶ 发布时修改目标端口
#发布局域网内部的OpenSSH服务器, 外网主机需使用250端口进行连接
iptables-t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp--dport 250-jDNAT --to 192.168.100.102:22
4.6在内网上配置
#在内网上安装httpd并开启服务
[root@localhost yum.repos.d]# yum install -y httpd
[root@localhost yum.repos.d]# systemctl start httpd
#关闭防火墙和selinux
[root@localhost yum.repos.d]# systemctl stop firewalld.service
[root@localhost yum.repos.d]# setenforce 0
4.7在网关服务器添加iptables规则
#先清空规则
[root@localhost yum.repos.d]#iptables -F -t nat
#添加规则
[root@localhost yum.repos.d]#iptables -t nat -A PREROUTING -i ens38 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.100.102
#查看规则
[root@localhost yum.repos.d]#iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 12.0.0.1 tcp dpt:80 to:192.168.100.102
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
4.8测试外网是否能访问内网
#在外网服务器上
[root@localhost ~]# curl 12.0.0.1
#在内网服务器上
[root@localhost yum.repos.d]# tail /etc/httpd/logs/access_log
127.0.0.1 - - [02/Nov/2021:18:05:31 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
12.0.0.100 - - [02/Nov/2021:18:19:45 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
五.tcpdump—Linux抓包
tcpdump tcp-i ens33 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap
(1)tcp∶ ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型
(2)-i ens33 ∶只抓经过接口ens33的包
(3)-t ∶不显示时间戳
(4)-s 0 ∶ 抓取数据包时默认抓取长度为68字节。加上-s 0 后可以抓到完整的数据包
(5)-c 100 ∶只抓取100个数据包
(6)dst port ! 22 ∶不抓取目标端口是22的数据包
(7)src net 192.168.1.0/24 ∶数据包的源网络地址为192.168.1.0/24。Net:网段,host:主机
(8)-w ./target.cap ∶ 保存成cap文件,方便用ethereal (即wireshark)分析
六.总结
PREROUTING: 位于 nat 表,用于修改目的地址(DNAT)(上一节说的是数据包作路由选择前应用此链中的规则 记住!所有的数据包进来的时侯都先由这个链处理)
POSTROUTING:位于 nat 表,用于修改源地址 (SNAT)(上一节说的是对数据包作路由选择后应用此链中的规则,所有的数据包出来的时侯都先由这个链处理)
SNAT(针对客户端) | 源地址转换数据包从内网发送至公网时,SNAT会把数据包的源IP由私网IP转换成公网IP。当响应的数据包从公网发送内容时,会把数据包的目的IP由公网IP转为私网IP |
DNAT(针对服务器端) | 将公网IP映射为局域网内对应的私网IP的服务器 |