zoukankan      html  css  js  c++  java
  • centos7搭建集群必知:centos7已经无iptables,只有firewall

    1.防火墙概述
    centos7搭建集群,发现没有iptables,需要安装。防火墙为firewalle
    CentOS7默认的防火墙不是iptables,而是firewalle.

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
    firewall:
    systemctl start firewalld.service#启动firewall
    systemctl stop firewalld.service#停止firewall
    systemctl disable firewalld.service#禁止firewall开机启动


    查看防火墙状态

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    systemctl status firewalld


    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    firewalld.service - firewalld - dynamic firewall daemon
        Loaded: loaded ( /usr/lib/systemd/system/firewalld .service; enabled)
        Active: inactive (dead) since 浜.2016-03-04 06:18:04 CST; 1min 45s ago
       Process: 11571 ExecStart= /usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0 /SUCCESS )
      Main PID: 11571 (code=exited, status=0 /SUCCESS )
     
    3?.04 06:17:54 master systemd[1]: Started firewalld - dynamic firewall daemon.
    3?.04 06:18:03 master systemd[1]: Stopping firewalld - dynamic firewall daemon...
    3?.04 06:18:04 master systemd[1]: Stopped firewalld - dynamic firewall daemon.




    2.防火墙详述



    官方文档地址:https://access.redhat.com/docume ... uction_to_firewalld

    1、firewalld简介
    firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务;第二个就是加入了防火墙的“zone”概念

    firewalld有图形界面和工具界面,由于我在服务器上使用,图形界面请参照官方文档,本文以字符界面做介绍

    firewalld的字符界面管理工具是 firewall-cmd

    firewalld默认配置文件有两个:/usr/lib/firewalld/ (系统配置,尽量不要修改)和 /etc/firewalld/ (用户配置地址)

    zone概念:
    硬件防火墙默认一般有三个区,firewalld引入这一概念系统默认存在以下区域(根据文档自己理解,如果有误请指正):
    drop:默认丢弃所有包
    block:拒绝所有外部连接,允许内部发起的连接
    public:指定外部连接可以进入
    external:这个不太明白,功能上和上面相同,允许指定的外部连接
    dmz:和硬件防火墙一样,受限制的公共连接可以进入
    work:工作区,概念和workgoup一样,也是指定的外部连接允许
    home:类似家庭组
    internal:信任所有连接
    对防火墙不算太熟悉,还没想明白public、external、dmz、work、home从功能上都需要自定义允许连接,具体使用上的区别还需高人指点

    2、安装firewalld
    root执行 # yum install firewalld firewall-config

    3、运行、停止、禁用firewalld
    启动:# systemctl start  firewalld
    查看状态:# systemctl status firewalld 或者 firewall-cmd --state
    停止:# systemctl disable firewalld
    禁用:# systemctl stop firewalld

    4、配置firewalld
    查看版本:$ firewall-cmd --version
    查看帮助:$ firewall-cmd --help
    查看设置:
                    显示状态:$ firewall-cmd --state
                    查看区域信息: $ firewall-cmd --get-active-zones
                    查看指定接口所属区域:$ firewall-cmd --get-zone-of-interface=eth0
    拒绝所有包:# firewall-cmd --panic-on
    取消拒绝状态:# firewall-cmd --panic-off
    查看是否拒绝:$ firewall-cmd --query-panic

    更新防火墙规则:# firewall-cmd --reload
                                # firewall-cmd --complete-reload
        两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务

    将接口添加到区域,默认接口都在public
    # firewall-cmd --zone=public --add-interface=eth0
    永久生效再加上 --permanent 然后reload防火墙

    设置默认接口区域
    # firewall-cmd --set-default-zone=public
    立即生效无需重启

    打开端口(貌似这个才最常用)
    查看所有打开的端口:
    # firewall-cmd --zone=dmz --list-ports
    加入一个端口到区域:
    # firewall-cmd --zone=dmz --add-port=8080/tcp
    若要永久生效方法同上

    打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档
    # firewall-cmd --zone=work --add-service=smtp

    移除服务
    # firewall-cmd --zone=work --remove-service=smtp

    还有端口转发功能、自定义复杂规则功能、lockdown,由于还没用到,以后再学习


    #####################################
    3.安装iptable iptable-service

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    #先检查是否安装了iptables
    service iptables status
    #安装iptables
    yum install -y iptables
    #升级iptables
    yum update iptables
    #安装iptables-services
    yum install iptables-services



    禁用/停止自带的firewalld服务

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    4
    #停止firewalld服务
    systemctl stop firewalld
    #禁用firewalld服务
    systemctl mask firewalld




    设置现有规则

    [Bash shell] 纯文本查看 复制代码
    ?
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #查看iptables现有规则
    iptables -L -n
    #先允许所有,不然有可能会杯具
    iptables -P INPUT ACCEPT
    #清空所有默认规则
    iptables -F
    #清空所有自定义规则
    iptables -X
    #所有计数器归0
    iptables -Z
    #允许来自于lo接口的数据包(本地访问)
    iptables -A INPUT -i lo -j ACCEPT
    #开放22端口
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    #开放21端口(FTP)
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    #开放80端口(HTTP)
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    #开放443端口(HTTPS)
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    #允许ping
    iptables -A INPUT -p icmp --icmp- type 8 -j ACCEPT
    #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
    iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
    #其他入站一律丢弃
    iptables -P INPUT DROP
    #所有出站一律绿灯
    iptables -P OUTPUT ACCEPT
    #所有转发一律丢弃
    iptables -P FORWARD DROP



    其他规则设定

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    #如果要添加内网ip信任(接受其所有TCP请求)
    iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
    #过滤所有非以上规则的请求
    iptables -P INPUT DROP
    #要封停一个IP,使用下面这条命令:
    iptables -I INPUT -s ***.***.***.*** -j DROP
    #要解封一个IP,使用下面这条命令:
    iptables -D INPUT -s ***.***.***.*** -j DROP




    保存规则设定

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    #保存上述规则
    service iptables save




    开启iptables服务

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    4
    5
    6
    7
    #注册iptables服务
    #相当于以前的chkconfig iptables on
    systemctl enable iptables.service
    #开启服务
    systemctl start iptables.service
    #查看状态
    systemctl status iptables.service



    解决vsftpd在iptables开启后,无法使用被动模式的问题

    1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    2
    3
    #添加以下内容,注意顺序不能调换
    IPTABLES_MODULES= "ip_conntrack_ftp"
    IPTABLES_MODULES= "ip_nat_ftp"


    2.重新设置iptables设置

    [Bash shell] 纯文本查看 复制代码
    ?
    1
    iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT




    以下为完整设置脚本

    [Bash shell] 纯文本查看 复制代码
    ?

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    #!/bin/sh
    iptables -P INPUT ACCEPT
    iptables -F
    iptables -X
    iptables -Z
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    iptables -A INPUT -p icmp --icmp- type 8 -j ACCEPT
    iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -P INPUT DROP
    iptables -P OUTPUT ACCEPT
    iptables -P FORWARD DROP
    service iptables save
    systemctl restart iptables.service
  • 相关阅读:
    python3 多线程的基本用法
    ASP.NET MVC下的四种验证编程方式
    漫步ASP.NET MVC的处理管线
    asp.net mvc源码分析-Action篇 IModelBinder
    在 Asp.NET MVC 中使用 SignalR 实现推送功能
    说说Python 中的文件操作 和 目录操作
    Python 用 os.walk 遍历目录
    我来说说MVC过滤器
    学习Python必须要知道的常用模块
    Python中的并发编程
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15454451.html
Copyright © 2011-2022 走看看