当前位置:   article > 正文

利用iptables的nat功能使内网主机上网_iptables -t nat -f导致网络不通

iptables -t nat -f导致网络不通

拓扑:
在这里插入图片描述

client:
eth0:1 192.168.10.2/24 gateway 192.168.10.1

PC1:
wlp5s0: 192.168.1.170/24 gateway: 192.168.1.1/24
enp9s0:1 192.168.10.1/24

192.168.10.2 -> baidu.com SNAT工具? 192.168.1.170 -> baidu.com

SNAT:选用iptables软件作为SNAT转换工具。

1、client设置
1)IP设置

sudo ip address add 192.168.10.2/24 dev eth0:1

    2)路由设置

    sudo ip route add default via 192.168.10.1

      3)dns设置

      l@l-HP-540:~$ cat /etc/resolv.conf
      # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
      #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
      nameserver 192.168.1.1
      • 1
      • 2
      • 3

      2、PC1设置
      1)IP及路由设置

      ip route add default via 192.168.1.1 dev wlp5s0
      ip address add 192.168.10.1/24 dev enp9s0:1
      • 1

      2)iptables设置

      iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 192.168.1.170

        iptables规则查询:

        iptables -t nat --list

          iptables规则清除:

          iptables -t nat -F

            3)防火墙设置

            systemctl stop firewalld

              (注:每次执行systemctl start firewalld命令,都会清除iptables规则,因此关闭比较好)

              3、测试
              1)client ping baidu.com

              l@l-HP-540:~$ ping baidu.com
              PING baidu.com (39.156.69.79) 56(84) bytes of data.
              64 bytes from 39.156.69.79: icmp_seq=1 ttl=52 time=26.1 ms
              64 bytes from 39.156.69.79: icmp_seq=2 ttl=52 time=26.7 ms
              64 bytes from 39.156.69.79: icmp_seq=3 ttl=52 time=26.7 ms
              64 bytes from 39.156.69.79: icmp_seq=4 ttl=52 time=26.8 ms
              64 bytes from 39.156.69.79: icmp_seq=5 ttl=52 time=27.1 ms
              64 bytes from 39.156.69.79: icmp_seq=6 ttl=52 time=26.8 ms
              • 1
              • 2
              • 3
              • 4
              • 5
              • 6
              • 7
              声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/777700
              推荐阅读
                

              闽ICP备14008679号