Linux用戶退出系統(tǒng)后限制IP登錄Shell的操作實例
Linux系統(tǒng)雖然比Windows系統(tǒng)安全很多,是很好的程序開發(fā)環(huán)境,但多少還是存在一些安全隱患的。在Linux用戶退出系統(tǒng)后,還是會有多個IP登錄的風(fēng)險,那么Linux退出后要如何限制IP登錄Shell腳本呢?下面小編介紹一些實例。
生產(chǎn)環(huán)境中,有可能會出現(xiàn)被其他管理員或使用者修改hosts.allow允許更多的ip登陸機(jī)器;為了方便統(tǒng)一管理,我們可以在用戶退出的時候統(tǒng)一修改allow和deny文件成默認(rèn)設(shè)置。同時也能自動獲取機(jī)器所配ip的整個ip段為允許登陸。
test -f /root/.bash_logout && chattr -i /root/.bash_logout
cat 》/root/.bash_logout 《《EOF
export TERM=xterm
/usr/bin/chattr -i /etc/hosts.allow /etc/hosts.deny
#for hosts.deny
/bin/awk ‘BEGIN{FS=“[=。]”;RS=“n”};/^IPADDR/{print “sshd:all EXCEPT ”$2“。”$3“。”$4“.0/255.255.255.0”}’ /etc/sysconfig/network-scripts/ifcfg-* | grep -v 127.0.0 | uniq 1》/etc/hosts.deny
/bin/cat 》》/etc/hosts.deny 《《 iEOF
sshd:all EXCEPT ip
iEOF
#for hosts.allow
/bin/awk ‘BEGIN{FS=“[=。]”;RS=“n”};/^IPADDR/{print “sshd:”$2“。”$3“。”$4“.0/255.255.255.0”}’ /etc/sysconfig/network-scripts/ifcfg-* | grep -v 127.0.0 | uniq 1》/etc/hosts.allow
/bin/cat 》》/etc/hosts.allow 《《 iEOF
sshd:ip(www.111cn.net)
iEOF
/usr/bin/chattr +i /etc/hosts.allow /etc/hosts.deny
clear
EOF
sh /root/.bash_logout 2》&1 》/dev/null
例子
這個只是檢測有非法ip登錄到服務(wù)器上就自動給運維報警,當(dāng)然也可以改成短信報警,前提是你有短信網(wǎng)關(guān)。
#!/bin/bash
#該腳本作用是檢測是否有惡意IP登陸服務(wù)器并郵件報警
#可以結(jié)合139郵箱以達(dá)到短信及時通知到手機(jī)的功能
#適用系統(tǒng)centos5
Ldate=`which date`
Lawk=`which awk`
Llast=`which last`
Lgrep=`which grep`
Lsendmail=`which sendmail`
Lifconfig=`which ifconfig`
serverip=`$Lifconfig eth0|$Lgrep inet|$Lawk -F : ‘{print $2}’|$Lawk ‘{print $1}’`
cutdate=`$Ldate |$Lawk ‘{print $1“ ”$2“ ”$3}’`
hackerip=`$Llast|$Lgrep “$cutdate”|$Lawk ‘{print $3}’|$Lgrep -v 192.168.1x.xx`
if [ -z $hackerip ]
then
exit
else
for logip in $hackerip
do
echo “hacker ip is $logip already login $serverip”|mail -s “SOS” rocdk890@139.com
done
fi
上面就是Linux用戶退出系統(tǒng)后限制IP登錄Shell腳本的實例介紹了,限制IP登錄也方便統(tǒng)一管理,有需要的朋友可以嘗試一下。
相關(guān)文章:
1. 統(tǒng)信UOS兼容轉(zhuǎn)安卓App 像使用手機(jī)一樣使用電腦2. Windows11升級可能會加密硬盤 注意!這個按鈕不要碰!3. Win7系統(tǒng)怎么進(jìn)行磁盤分區(qū)?4. macOS Ventura 13.3 正式版發(fā)布: 增加了重復(fù)照片檢測功能等功能5. 統(tǒng)信uos系統(tǒng)怎么選擇打印機(jī)驅(qū)動? uos打印機(jī)驅(qū)動的兩種選擇方法6. issch.exe是什么進(jìn)程?issch.exe是不是病毒?7. Win11搜索欄項如何隱藏?Win11搜索欄項隱藏方法8. Win8系統(tǒng)提示無線適配器或訪問點有問題怎么解決?9. rsync結(jié)合 inotfiy 實現(xiàn)實時備份的問題10. 教你如何使用兩行代碼激活Windows系統(tǒng)
