[root@centos7 bin]#cat checkip.sh
#!/bin/bash
touch /etc/hosts.deny
lastb | awk ‘/ssh/{IP[$3]++}END{for(n in IP){if(IP[n]>2){system("echo ‘sshd:‘" n ":deny >> /etc/hosts.deny")}}}‘
grep ‘^#‘ /etc/hosts.deny > /etc/hosts.deny.tmp
grep -v "^#" /etc/hosts.deny | sort -u >> /etc/hosts.deny.tmp
cat /etc/hosts.deny.tmp > /etc/hosts.deny
rm -f /etc/hosts.deny.tmp
[root@centos7 bin]#crontab -l
*/5 * * * * /bin/bash /root/bin/checkip.sh
# 使用visudo,添加下面一行
[root@centos7 ~]#visudo
magedu ALL=(root) ALL
# 验证效果
[root@centos7 ~]#su - magedu
Last login: Sat Jul 4 12:40:52 CST 2020 on pts/2
# 使用sudo才可以访问/root目录
[magedu@centos7 ~]$ls /root
ls: cannot open directory /root: Permission denied
[magedu@centos7 ~]$sudo ls /root
anaconda-ks.cfg bin create.sh expect2.sh expect3.sh expect.sh hellodb_innodb.sql initial-setup-ks.cfg
原文:https://www.cnblogs.com/kfscott/p/13251875.html