zoukankan      html  css  js  c++  java
  • centos7 firewall-cmd 详解

    防火墙

    查看防火墙状态
    firewall-cmd --state
    
    停止firewall
    systemctl stop firewalld.service
    
    开启
    systemctl start firewalld.service
    
    禁止firewall开机启动
    systemctl disable firewalld.service 
    

    端口

    查看对外开放的端口状态
    netstat -anp
    
    对外开发端口
    查看想开的端口是否已开:
    firewall-cmd --query-port=80/tcp
    
    添加指定需要开放的端口:
    firewall-cmd --add-port=80/tcp --permanent
    
    重载入添加的端口:
    firewall-cmd --reload
    
    查询指定端口是否开启成功:
    firewall-cmd --query-port=80/tcp
    
    只允许某个特定的的ip访问80端口
    Linux防火墙Iptable如何设置只允许某个ip访问80端口,只允许特定ip访问某端口?参考下面命令,只允许46.166.150.22访问本机的80端口。如果要设置其他ip或端口,改改即可。
    iptables -I INPUT -p TCP --dport 80 -j DROP
    iptables -I INPUT -s 46.166.150.22 -p TCP --dport 80 -j ACCEPT
    在root用户下执行上面2行命令后,重启iptables, service iptables restart
    
    在centos7中使用firewall-cmd指定只允许192.168.0.1的ip访问80端口
    firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.0.1/2 port port=80 protocol=tcp accept'
    
    查看iptables是否生效:
    [root@www.ctohome.com]# iptables -L
    Chain INPUT (policy ACCEPT)
    target           prot opt source               destination         
    ACCEPT     tcp  --  46.166.150.22    anywhere            tcp dpt:http 
    DROP         tcp  --  anywhere             anywhere            tcp dpt:http 
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination        
    
    上面命令是针对整个服务器(全部ip)禁止80端口,如果只是需要禁止服务器上某个ip地址的80端口,怎么办?
    下面的命令是只允许来自174.140.3.190的ip访问服务器上216.99.1.216的80端口
    iptables -A FORWARD -s 174.140.3.190 -d 216.99.1.216 -p tcp -m tcp --dport 80 -j ACCEPT 
    iptables -A FORWARD -d 216.99.1.216 -p tcp -m tcp --dport 80 -j DROP
    

    禁止ip登录

    centos7用的是firewall 添加单个黑名单只需要把ip添加到 /etc/hosts.deny
    格式  sshd:$IP:deny
    vim /etc/hosts.deny   添加你要禁止的ip就可以了
    sshd:192.168.1.147:deny
    
    这是允许的 /etc/hosts.allow
    sshd:19.16.18.1:allow
    sshd:19.16.18.2:allow
    
    • 多次失败登录即封掉IP,防止暴力破解的脚本 超过5次的就加到黑名单
    1、防爆破脚本
    vim /usr/local/bin/secure_ssh.sh
    #! /bin/bash
    cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.txt
    for i in `cat  /usr/local/bin/black.txt`
    do
        IP=`echo $i |awk -F= '{print $1}'`
        NUM=`echo $i|awk -F= '{print $2}'`
        if [ $NUM -gt 5 ]
        then
            grep $IP /etc/hosts.deny > /dev/null
            if [ $? -gt 0 ]
            then 
                echo "sshd:$IP:deny" >> /etc/hosts.deny
            fi
        fi
    done
    
    2、创建记录登录失败次数的文件
    touch /usr/local/bin/black.txt
    
    3、添加定时 5分钟执行一次
    */5 * * * *  sh /usr/local/bin/secure_ssh.sh
    
    4、测试 ssh登录147
    ssh 192.168.1.147
    

    5、查看黑名单列表是否记录

    cat /usr/local/bin/black.txt

    6、查看黑名单列表看是否添加进去了
    cat /etc/hosts.deny

  • 相关阅读:
    Kotlin基本语法笔记3之定义类、继承及创建实例
    Kotlin基本语法笔记2之类型检测及自动类型转换、循环
    Kotlin基本语法笔记之函数、变量的定义及null检测
    C++笔记之外部类访问内部类的私有成员
    正则表达式之不区分大小写的匹配
    springMVC之helloworld
    数组学习
    反射reflect
    JSP学习
    自己做的菜
  • 原文地址:https://www.cnblogs.com/jiangyatao/p/11094060.html
Copyright © 2011-2022 走看看