Linux學(xué)習(xí)第十一周


一、編寫腳本/root/bin/checkip.sh,每5分鐘檢查一次,如果發(fā)現(xiàn)通過(guò)ssh登錄失敗 次數(shù)超過(guò)10次,自動(dòng)將此遠(yuǎn)程IP放入Tcp Wrapper的黑名單中予以禁止防問(wèn)。

1、編寫腳本;
[root@localhost bin]# cat checkip.sh
#!/bin/bash
n=10
cat /var/log/secure |grep sshd|awk '/Failed/{print $(NF-3)}'|sort |uniq -c |sort >>ssherro.log
while read count ip;do
        if [ ${count} > ${n} ];then
                echo "sshd is from:${ip}" >>/etc/hosts.deny
        fi
done < ssherro.log

附上獲取ssh登錄失敗次數(shù)統(tǒng)計(jì)步驟(用其它機(jī)器模擬ssh失敗登錄):

[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
192.168.1.112
192.168.1.112
192.168.1.112
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}' | sort | uniq -c | sort -r
      6 192.168.1.111
      3 192.168.1.112

2、寫入定時(shí)任務(wù);

[root@localhost bin]# crontab -l
*/5 * * * * root sh /root/bin/checkip.sh &>/dev/null

二、配置magedu用戶的sudo權(quán)限,允許magedu用戶擁有root權(quán)限。

[root@localhost ~]# visudo

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL     ##在此處下面添加
magedu  ALL=(ALL)       ALL

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容