如何防止非root用户创建crontab条目

通常,在生产环境中需要禁用非 root 用户创建任何 crontab 条目。
有三种方法可以实现这一点:

  1. 禁用非root用户ssh到系统,那么非root用户根本无法使用shell。

  2. 将用户添加到文件 /etc/cron.deny 中,每行每个用户(仅影响列表用户的典型方法):

# cat /etc/cron.deny 
johny

注意:确保文件 /etc/cron.allow 和 /etc/cron.deny 之间没有冲突。

通过创建 crontab 条目验证被拒绝的用户:

$ crontab -e
You (johny) are not allowed to use this program (crontab)
See crontab(1) for more information
  1. 弃用 crontab 命令的执行权限(影响所有非 root 用户的激进方法):
    默认权限:
# ls -lrt /usr/bin/crontab 
-rwsr-xr-x 1 root root 57552 Apr 21  2015 /usr/bin/crontab

更改权限(去掉 setuid 位):

# chmod 700 /usr/bin/crontab
# ls -lrt /usr/bin/crontab
-rwx------ 1 root root 57552 Apr 21  2015 /usr/bin/crontab

注意:在更改文件权限之前,请确保我们有备份文件。

软件包升级后,此更改将恢复为默认值。

默认权限:

# stat /usr/bin/crontab
  File: ‘/usr/bin/crontab’
  Size: 57552     	Blocks: 120        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 10751442    Links: 1
Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-12 17:03:10.112443944 +0530
Modify: 2015-04-21 19:38:05.000000000 +0530
Change: 2016-03-14 14:03:30.347276747 +0530
 Birth:

通过创建 crontab 条目验证被拒绝的用户:

$ crontab -e
bash: /usr/bin/crontab: Permission denied
日期:2020-09-17 00:13:45 来源:oir作者:oir