《云计算》-基础配置-配置firewalld,实现端口转发

配置firewalld防火墙
3.1 问题

本例要求为两个虚拟机 server0、desktop0配置防火墙策略:

允许从172.25.0.0/24网段的客户机访问 server0、desktop0 的任何服务
禁止从my133t.org域(172.34.0.0/24网段)的客户机访问 server0、desktop0 的任何服务
在172.25.0.0/24网络中的系统,访问 server0 的本地端口5423将被转发到80
上述设置必须永久有效

    
    
  • 1
  • 2
  • 3
  • 4

3.2 方案

RHEL7的防火墙体系根据所在的网络场所区分,提供了预设的安全区域:

public:仅允许访问本机的sshd等少数几个服务
trusted:允许任何访问
block:阻塞任何来访请求
drop:丢弃任何来访的数据包
……

    
    
  • 1
  • 2
  • 3
  • 4
  • 5

新增防火墙规则的位置包括:

运行时(runtime):仅当前有效,重载防火墙后失效
永久(permanent):静态配置,需要重载防火墙才能生效

    
    
  • 1
  • 2

本地端口转发(端口1 --> 端口2):

从客户机访问防火墙主机的 端口1 时,与访问防火墙的 端口 2 时等效
真正的网络应用服务其实在 端口2 提供监听

    
    
  • 1
  • 2

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:采取“默认全允许,仅拒绝个别”的防护策略

1)启用防火墙服务

[root@server0 ~]# systemctl  restart  firewalld
[root@server0 ~]# systemctl  enable  firewalld

    
    
  • 1
  • 2

2)将默认区域设置为trusted

[root@server0 ~]# firewall-cmd  --get-default-zone                 //修改前
public
[root@server0 ~]# firewall-cmd  --set-default-zone=trusted         //修改操作
success
[root@server0 ~]# firewall-cmd  --get-default-zone              //修改后
trusted

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

步骤二:封锁指定的IP网段

1)添加永久配置“阻塞来自网段172.34.0.0/24的任何访问”

[root@server0 ~]# firewall-cmd  --permanent  --zone=block  --add-source=172.34.0.0/24
success

    
    
  • 1
  • 2

2)重载防火墙

[root@server0 ~]# firewall-cmd  --reload
success

    
    
  • 1
  • 2

3)检查运行时规则

[root@server0 ~]# firewall-cmd  --list-all  --zone=block
block
  interfaces: 
  sources: 172.34.0.0/24
  services: 
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules:

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

步骤三:实现5423–>80端口转发

1)针对80端口部署测试应用

快速搭建一个测试网站:

[root@server0 ~]# yum  -y  install  httpd                    //装包
.. ..
[root@server0 ~]# vim  /var/www/html/index.html              //部署测试网页
test site.
[root@server0 ~]# systemctl  restart  httpd                   //起服务

    
    
  • 1
  • 2
  • 3
  • 4
  • 5

从客户端访问,确认测试网页:

[root@desktop0 ~]# yum  -y  install  elinks
.. ..
[root@desktop0 ~]# elinks  -dump  http://server0.example.com/
   test site.

    
    
  • 1
  • 2
  • 3
  • 4

2)配置5423–>80端口转发策略

[root@server0 ~]# firewall-cmd  --permanent  --zone=trusted  --add-forward-port=port=5423:proto=tcp:toport=80                              //添加永久配置
success
[root@server0 ~]# firewall-cmd  --reload                      //重载服务
Success
[root@server0 ~]# firewall-cmd  --list-all                  //确认运行时规则
trusted (default, active)
  interfaces: eth1 eth2 eth0 team0
  sources: 
  services: 
  ports: 
  masquerade: no
  forward-ports: port=5423:proto=tcp:toport=80:toaddr=
  icmp-blocks: 
  rich rules:

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3)验证端口转发策略

从desktop0上访问server0的5423端口,与访问server0的80端口效果一样:

[root@desktop0 ~]# elinks  -dump  http://server0.example.com:5423/
   test site.
[root@desktop0 ~]# elinks  -dump  http://server0.example.com/
   test site.

    
    
  • 1
  • 2
  • 3
  • 4
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值