目录
一、SNAT策略及应用
1、SNAT策略概述
1.SNAT 应用环境
- 局域网主机共享单个公网IP地址接入Internet (私有IP不能在Internet中正常路由
2.SNAT转换前提条件
- 源地址转换
- 修改数据包的源地址
3.SNAT转换前提条件
- 局域网各主机已正确设置IP地址、子网掩码、默认网关地址
- Linux网关开启IP路由转发
2、开启SNAT的命令
(1)临时打开
echo 1 >/proc/sys/net/ipv4/ip_forward 或者
sysctl -w net.ipv4.ip forward=1(2)永久打开
vim /etc/ sysctl. conf
net. ipv4.ip_ forward = 1 #将此行写入配置文件
sysctl -P #读取修改后的配置3、SNAT转换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或地址池4、SNAT转换2:非固定的公网IP地址(共享动态IP地址)
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
5、SNAT实验示例
- web服务器ip:192.168.121.30(nat1)、关闭防火墙和selinux、开启http服务
- 网关服务器内网ip:192.168.121.20(nat1);外网ip:12.0.0.254(nat2)、关闭防火墙和selinux、开启http服务
- win7客户端ip:12.0.0.100(nat2)
- VMware的虚拟网络编辑器中默认nat模式网段:192.168.121.0,nat2模式网段:12.0.0.0
(1) 配置网关服务器(192.168.121.20/12.0.0.254)的相关配置
1.添加两块虚拟网卡,并自定义
2.复制并修改ens36网卡#切换至网卡配置文件所在目录 [root@localhost network-scripts]#cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]#cp ifcfg-ens33 ifcfg-ens38
3.修改ens33网卡
4.重启网络并查看是否修改成功[root@localhost network-scripts]# systemctl restart network [root@localhost network-scripts]# ifconfig
(2)配置内网服务器(192.168.121.30)相关配置
1.修改enss网卡模式为仅主机模式
2.修改ens33网卡
3.重启网ens33网络
4.ping网关测试(192.168.121.20)
(3) 配置外网服务器(12.0.0.100)的相关配置
1.修改enss网卡模式为仅主机模式
2.修改ens33网卡
3.重启网络
4.ping网关是否畅通
(4)开启SNAT,ip转发功能
[root@localhost network-scripts]#vim /etc/ sysctl. conf net. ipv4.ip_ forward = 1 #将此行写入配置文件 [root@localhost network-scripts]#sysctl -P #读取修改后的配置
(5)配置网关服务器的iptables规则
1.安装iptables,关闭防火墙和selinux,开启iptables
2.查看网关服务器的iptables规则并清除iptables -nL #查看规则
iptables -nL -t nat #查看规则
iptables -F #清除iptables的规则
iptables -F -t nat #清除iptables的规则(6) 添加 SNAT与DNAT转换∶固定的公网IP地址
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.121.0/24 -o ens33 -j SNAT --to-source 12.0.0.254 [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.121.0/24 -o ens36 -j SNAT --to-source 12.0.0.254 [root@localhost ~]# iptables -t nat -A PREROUTING -i ens36 -d 12.0.0.254 -p tcp --dport 8080 -j DNAT --to 192.168.121.20:80 [root@localhost ~]#
(7)进行内外网访问
在默认网页根目录下创建一个index.html文件
[root@localhost html]# cd /var/www/html/ [root@localhost html]# echo "it's a test web of the Class!!" >index.html
#########在外网服务器上
#安装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
[root@localhost yum.repos.d]# setenforce 0
[root@localhost httpd]# tail -f access_log
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /images/apache_pb.gif HTTP/1.1” 200 2326 “http://12.0.0.254:8080/” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /images/poweredby.png HTTP/1.1” 200 3956 “http://12.0.0.254:8080/” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1” 404 241 “http://12.0.0.254:8080/noindex/css/open-sans.css” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1” 404 239 “http://12.0.0.254:8080/noindex/css/open-sans.css” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1” 404 238 “http://12.0.0.254:8080/noindex/css/open-sans.css” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1” 404 240 “http://12.0.0.254:8080/noindex/css/open-sans.css” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /favicon.ico HTTP/1.1” 404 209 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:34:44 +0800] “GET /favicon.ico HTTP/1.1” 404 209 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:36:16 +0800] “GET / HTTP/1.1” 200 40 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”
12.0.0.100 - - [11/May/2022:03:37:11 +0800] “GET / HTTP/1.1” 200 36 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0”