iptables之forward转发

1、网络防火墙

  • 网络防火墙处于网络入口的边缘,针对网络入口进行防护,针对整个网络入口后面的局域网。

作用:
当外部网络主机与内部网络主机互相进行通讯时,都要经过iptables所在的主机,由iptables所在的主机进行 “过滤并转发”,这就是防火墙的主要工作

但是这又跟forward链有什么关系呢?

网络防火墙的主要职责是"过滤并转发",在五链中,只有INPUT,OUTPUT和FORWARD有filter(过滤)功能,要想实现转发,报文必须经过FORWARD链,因此,iptables的角色变为"网络防火墙"时,规则只能定义在FORWARD链。

2、iptables之FORWARD转发实例

实验环境:

外网主机ip地址:192.168.119.5 网关为:192.168.119.2
防火墙主机eth0 :192.168.119.132 网关为: 192.168.119.2 eth1:192.168.116.132 网关为:192.168.116.1
内网主机ip地址:192.168.116.7 网关为:192.168.116.132

注意:

  • 内网的网关必须为防火墙内网的地址
  • 外网和防火墙的eth0网卡设置为nat模式,
    内网和防火墙的eth1设置为仅主机模式

具体关系如下图所示:

在这里插入图片描述

1、在外网上新增一条路由,让外网访问192.168.116.0网段都发送给防火墙的eth0网卡上

[root@li ~]# route add -net 192.168.116.0/24 gw 192.168.119.132
[root@li ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.116.0   192.168.119.132 255.255.255.0   UG    0      0        0 eth0
192.168.119.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.119.2   0.0.0.0         UG    0      0        0 eth0

2、外网可以ping通防火墙的两块网卡,但还是ping不通内网

[root@li ~]# ping 192.168.119.132
PING 192.168.119.132 (192.168.119.132) 56(84) bytes of data.
64 bytes from 192.168.119.132: icmp_seq=1 ttl=64 time=0.330 ms
64 bytes from 192.168.119.132: icmp_seq=2 ttl=64 time=0.491 ms
^C
          #可以ping通防火墙的eth0网卡
[root@li ~]# ping 192.168.116.132
PING 192.168.116.132 (192.168.116.132) 56(84) bytes of data.
64 bytes from 192.168.116.132: icmp_seq=1 ttl=64 time=0.528 ms
64 bytes from 192.168.116.132: icmp_seq=2 ttl=64 time=0.831 ms
^C
          #可以ping通防火墙的eth1网卡
[root@li ~]# ping 192.168.116.7
PING 192.168.116.7 (192.168.116.7) 56(84) bytes of data.
^C
--- 192.168.116.7 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4997m
           #内网的IP地址不能ping通

3、为什么已经设置路由了,但还是ping不通呢?

我们在这里设置了静态路由,凡是192.168.116.0的网络通通下一跳给了192.168.119.132,当我们ping192.168.116.132的时候,防火墙收到了这个报文发现这个IP地址是自己的,给了外网回应,所以可以ping通。
(这是新增路由的作用,192.168.119.132把报文给了192.168.116.132)

那为什么192.168.116.7无法ping通呢,防火墙在收到这个报文的时候,发现这不是自己的地址,没有回应。

这里就需要打开防火墙的转发功能,才可以让报文转发。

4、打开防火墙转发功能

  • 临时打开转发功能
[root@liyu ~]# cat /proc/sys/net/ipv4/ip_forward
0     #0表示转发功能关闭
[root@liyu ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@liyu ~]# cat /proc/sys/net/ipv4/ip_forward
1     #将0改为1,打开转发功能


  • 永久修改
#Centos6 :
vi /etc/sysctl.conf
将net.ipv4.ip_forward = 0修改为net.ipv4.ip_forward= 1
#Centos7 :
vi /usr/lib/sysctl.d/00-system.conf 
加入net.ipv4.ip_forward=1
  • 此时用外网ping内网,可以ping通
  • 用内网ping外网也可以ping通
    注意:在此步骤前要删除之前所有的iptables规则,以防出错
[root@li ~]# ping 192.168.116.7
PING 192.168.116.7 (192.168.116.7) 56(84) bytes of data.
64 bytes from 192.168.116.7: icmp_seq=1 ttl=63 time=0.969 ms
64 bytes from 192.168.116.7: icmp_seq=2 ttl=63 time=1.63 ms
^C
     #此为外网ping内网
[root@localhost ~]# ping 192.168.119.5
PING 192.168.119.5 (192.168.119.5) 56(84) bytes of data.
64 bytes from 192.168.119.5: icmp_seq=1 ttl=63 time=1.11 ms
64 bytes from 192.168.119.5: icmp_seq=2 ttl=63 time=2.62 ms
64 bytes from 192.168.119.5: icmp_seq=3 ttl=63 time=1.71 ms
    #此为内网ping外网
  • 可以相互ping通,实现了防火墙的转发功能

3、iptables之FORWARD过滤实例

iptables作为网络防火墙时负责“过滤和转发”,过滤是在filter表中,要转发需要配置FORWARD链。

#在防火墙上配置FORWARD链,拒绝任何报文通过,此时内外网都无法访问
[root@li~]# iptables -A FORWARD -j REJECT

#打开内外网的apache服务
外网:
[root@li~]# echo "OUTSIDE SERVER" > index.html
[root@li~]# cat index.html
OUTSIDE SERVER
[root@li~]# service httpd status
httpd (pid 9433) is running...

内网:
[root@localhost ~]]# echo "INSIDE SERVER" > /var/www/html/index.html
[root@localhost ~]]# cat /var/www/html/index.html
INSIDE SERVER
[root@localhost ~]# service httpd status
httpd (pid 3238) is running...


#配置访问规则,允许内网可以访问外网的web服务
[root@li ~]# iptables -I FORWARD -s 192.168.116.0/24 -p tcp --dport 80 -j ACCEPT
[root@li ~]# iptables -I FORWARD -d 192.168.116.0/24 -p tcp --sport 80 -j ACCEPT
#配置访问规则,允许外网可以访问内网的web服务
[root@li ~]# iptables -I FORWARD -s 192.168.119.0/24 -p tcp --dport 80 -j ACCEPT
[root@li ~]# iptables -I FORWARD -d 192.168.119.0/24 -p tcp --sport 80 -j ACCEPT


#此时查看防火墙的规则
[root@liyu ~]# iptables -nL
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            192.168.119.0/24    tcp spt:80
ACCEPT     tcp  --  192.168.119.0/24     0.0.0.0/0           tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            192.168.116.0/24    tcp spt:80
ACCEPT     tcp  --  192.168.116.0/24     0.0.0.0/0           tcp dpt:80
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-port-unreachable


隐藏问题:如何安装apache?

#yum install httpd    安装
#service httpd restart      重启
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值