您当前所在位置:首页脚本Linux退出时限制ip登陆脚本

Linux退出时限制ip登陆脚本

更新:2022-05-21 09:32:12编辑:Story归类:脚本人气:206

在生产环境中,有可能会出现被其他管理员或使用者修改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
iEOF
/usr/bin/chattr +i /etc/hosts.allow /etc/hosts.deny

clear
EOF

sh /root/.bash_logout 2>&1 >/dev/null

成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

Linux批量添加用户和组脚本 一键自安装ISO封装打包脚本