centos中退出时限制ip登陆设置_centos中退出时限制ip登陆shell脚本

更新时间:2020-05-05    来源:CentOS    手机版     字体:

【www.bbyears.com--CentOS】

在生产环境中,有可能会出现被其他管理员或使用者修改hosts.allow允许更多的ip登陆机器;为了方便统一管理,我们可以在用户退出的时候统一修改allow和deny文件成默认设置。同时也能自动获取机器所配ip的整个ip段为允许登陆

test -f /root/.bash_logout && chattr -i /root/.bash_logout
cat >/root/.bash_logout < 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

ssh登录限制ip的方法


vi /etc/hosts.allow
sshd:192.168.0.100:allow          //允许IP 192.168.0.100 登录
sshd:192.168.0.:allow            //允许IP 192.168.0.  网段登录
sshd:all:deny                     //禁止其他的所有IP登录
或者
sshd:223.227.223.*:allow              //允许IP 223.227.223.*   网段登录
sshd:192.168.0.*:allow                //允许IP 192.168.0.*  网段登录
sshd:all:deny                                //禁止其他的所有IP登录
fedora下 修改后保存后生效
freeBSD限制特定ip的ssh登陆:
1.
#ee /etc/hosts.allow
在ALL : ALL : allow的前面加上
sshd : 192.168.0.100 : allow
sshd : 223.227.223.* : allow
sshd : ALL : deny
2.
修改/etc/ssh/sshd_config
加入
Allowusers admin@172.16.2.188
意思为
只允许admin从172.16.2.188登陆

FreeBSD 改IP ,不用重启电脑:
在rc.conf里改完后/etc/netstart

iptables自动封IP脚本

#!/bin/bash
#Created by haiyun
num=10 #上限
for i in `awk "/Failed/{print $(NF-3)}" /var/log/secure|sort|uniq -c|sort -rn|awk "{if ($1>$num){print $2}}"`
do
iptables -I INPUT -p tcp -s $i --dport 22 -j DROP
done
加入crontab计划任务
crontab -e
* */5 * * * sh /path/file.sh #5小时执行一次

本文来源:http://www.bbyears.com/caozuoxitong/93357.html

热门标签

更多>>

本类排行