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(111cn)

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登录也方便统一管理,有需要的朋友可以尝试一下。


上一篇:Linux系统如何设置安全管理

下一篇:如何安装Ubuntu系统?Ubuntu系统的安装教程


Linuxshell脚本 linuxip登录shell
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器