iptables的SNAT与DNAT应用


一、SNAT策略及应用

1、SNAT策略概述

1.SNAT 应用环境

  • 局域网主机共享单个公网IP地址接入Internet (私有IP不能在Internet中正常路由
  • 2.SNAT转换前提条件

  • 源地址转换
  • 修改数据包的源地址
  • 3.SNAT转换前提条件

    1. 局域网各主机已正确设置IP地址、子网掩码、默认网关地址
    2. 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实验示例

    在这里插入图片描述

    1. web服务器ip:192.168.121.30(nat1)、关闭防火墙和selinux、开启http服务
    2. 网关服务器内网ip:192.168.121.20(nat1);外网ip:12.0.0.254(nat2)、关闭防火墙和selinux、开启http服务
    3. win7客户端ip:12.0.0.100(nat2)
    4. 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”

    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值