Linux用戶退出系統后限制IP登錄Shell的操作實例
Linux系統雖然比Windows系統安全很多,是很好的程序開發環境,但多少還是存在一些安全隱患的。在Linux用戶退出系統后,還是會有多個IP登錄的風險,那么Linux退出后要如何限制IP登錄Shell腳本呢?下面小編介紹一些實例。
生產環境中,有可能會出現被其他管理員或使用者修改hosts.allow允許更多的ip登陸機器;為了方便統一管理,我們可以在用戶退出的時候統一修改allow和deny文件成默認設置。同時也能自動獲取機器所配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登錄到服務器上就自動給運維報警,當然也可以改成短信報警,前提是你有短信網關。
#!/bin/bash
#該腳本作用是檢測是否有惡意IP登陸服務器并郵件報警
#可以結合139郵箱以達到短信及時通知到手機的功能
#適用系統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用戶退出系統后限制IP登錄Shell腳本的實例介紹了,限制IP登錄也方便統一管理,有需要的朋友可以嘗試一下。
相關文章:
1. 使用kali破解wifi密碼的方式2. 企業 Win10 設備出現開始菜單和任務欄快捷方式消失問題,微軟展開調查3. uos支持觸屏嗎? 統信uos使用觸屏操作的技巧4. 如何雙啟動Win11和Linux系統?Win11 和 Linux雙系統安裝教程5. grub2引導freebsd詳解6. 世界上最流行的操作系統不是Linux或者Windows,而是MINIX7. UOS怎么設置時間? 統信UOS顯示24小時制的技巧8. Win11Beta預覽版22621.1180和22623.1180發布 附KB5022363內容匯總9. uos怎么格式化硬盤? UOS磁盤分區格式化的技巧10. 統信UOS個人版(V201030)正式發布 新增功能匯總
