【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

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

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

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

/etc/sysctl.conf设置如下配置

[root@localhost ~]# cat /etc/sysctl.conf

net.ipv4.ip_forward=1
重启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

同样的,机器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

机器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

机器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

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

猜你喜欢

转载自blog.csdn.net/suyuaidan/article/details/133500654
今日推荐