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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM