• 【iptables 实战】05 iptables设置网络转发实验


    一、网络架构

    实验效果,通过机器B的转发功能,将机器A的报文转发到机器C
    本实验准备三台机器分别配置如下网络
    机器A ip:192.168.56.104
    机器C ip:10.1.0.10
    机器B 两张网卡,分别的ip是192.168.56.106和10.1.0.11
    如图所示
    在这里插入图片描述

    如下图所示

    二、虚拟机网卡设置

    设置两个host-Only的局域网

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    机器A的网络设置如下:
    在这里插入图片描述

    机器B的网络设置如下:
    两个网卡,分别连接两个局域网

    在这里插入图片描述
    在这里插入图片描述

    机器C的网卡配置如下:
    在这里插入图片描述

    三、虚拟机网络设置

    A机器
    在这里插入图片描述

    B机器
    在这里插入图片描述

    连接局域网的,实际上有两个网卡
    enp0s8网卡设置如下:
    在这里插入图片描述

    enp0s9网卡设置如下:
    在这里插入图片描述

    C机器
    在这里插入图片描述

    配置好三台机器的网络以后,
    尝试A(192.168.56.104) ping B(192.168.56.106)

    [root@localhost network-scripts]# ping 192.168.56.106
    PING 192.168.56.106 (192.168.56.106) 56(84) bytes of data.
    64 bytes from 192.168.56.106: icmp_seq=1 ttl=64 time=1.12 ms
    64 bytes from 192.168.56.106: icmp_seq=2 ttl=64 time=0.861 ms
    
    • 1
    • 2
    • 3
    • 4

    C(10.1.0.10) ping B(10.1.0.11)

    [root@localhost ~]# ping 10.1.0.11
    PING 10.1.0.11 (10.1.0.11) 56(84) bytes of data.
    64 bytes from 10.1.0.11: icmp_seq=1 ttl=64 time=0.933 ms
    64 bytes from 10.1.0.11: icmp_seq=2 ttl=64 time=0.899 ms
    
    • 1
    • 2
    • 3
    • 4

    可以发现 A和B ,C和B都是互通的
    但是此时A和C不能互通
    因此需要下面的步骤开启机器B的转发功能

    四、开启作为路由机B的转发功能

    /etc/sysctl.conf设置如下配置:net.ipv4.ip_forward=1

    [root@localhost ~]# cat /etc/sysctl.conf
    net.ipv4.ip_forward=1
    
    • 1
    • 2

    重启B机器

    五、机器A和机器C的路由设置

    通过手动添加路由规则,将A与机器C(10.1.0.10)的报文,都通过网关B进行处理
    机器A(192.168.56.104)
    注意route 设置,只在当前运行时有效,重启后就没有该路由规则了

    [root@localhost network-scripts]# route add -net 10.1.0.0/16 gw 192.168.56.106
    [root@localhost network-scripts]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.56.100  0.0.0.0         UG    100    0        0 enp0s8
    10.1.0.0        192.168.56.106  255.255.0.0     UG    0      0        0 enp0s8
    172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
    192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s8
    192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    同样的,机器C(10.1.0.10)想连机器A(192.168.56.104)也得设置一下路由

    [root@localhost network-scripts]# route add -net 192.168.56.0/24 gw 10.1.0.11
    [root@localhost ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.1.0.2        0.0.0.0         UG    100    0        0 enp0s8
    10.1.0.0        0.0.0.0         255.255.0.0     U     100    0        0 enp0s8
    172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
    192.168.56.0    10.1.0.11       255.255.255.0   UG    0      0        0 enp0s8
    192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    机器A(192.168.56.104)ping 机器C(10.1.0.10)

    [root@localhost network-scripts]# ping 10.1.0.10
    PING 10.1.0.10 (10.1.0.10) 56(84) bytes of data.
    64 bytes from 10.1.0.10: icmp_seq=1 ttl=63 time=1.51 ms
    64 bytes from 10.1.0.10: icmp_seq=2 ttl=63 time=1.61 ms
    
    • 1
    • 2
    • 3
    • 4

    机器C(10.1.0.10)ping机器A(192.168.56.104)

    [root@localhost ~]# ping 192.168.56.104
    PING 192.168.56.104 (192.168.56.104) 56(84) bytes of data.
    64 bytes from 192.168.56.104: icmp_seq=1 ttl=63 time=1.62 ms
    64 bytes from 192.168.56.104: icmp_seq=2 ttl=63 time=1.75 ms
    
    • 1
    • 2
    • 3
    • 4

    现在,A和C机器,就可以互通了

    总结

    要想一台主机作为转发功能,需要做下面的工作

    • 1、B主机设置两张网卡,分别连接个局域网,和两个局域网的机器都互相能ping通。
    • 2、开启B机器的转发功能:net.ipv4.ip_forward=1
    • 3、两个不同局域网的主机A和C,想要通过B的转发,要配置路由规则,如:route add -net 192.168.56.0/24 gw 10.1.0.11
  • 相关阅读:
    Java版CRM客户管理系统源码 CRM小程序源码
    ARMv7-A 那些事 - 7.栈回溯浅析
    【Docker】uwsgi的测试test.py显示hello-world -20220802
    java压缩pdf体积,图片体积
    System.ArgumentException: 已添加了具有相同键的项
    【进阶C语言】进阶指针--学会所有指针类型
    国际知名商学院复旦大学EMBA荣登全球第8位,中文项目国内居首
    高速电路逻辑电平转换设计
    18.(开发工具篇Gitlab)Git如何回退到指定版本
    shiro721反序列化漏洞(CVE-2019-12422)原理与漏洞复现和利用(保姆级的详细教程)
  • 原文地址:https://blog.csdn.net/suyuaidan/article/details/133500654