如何禁止非root用户在Linux中创建crontab条目

本文说明了如何阻止非 root 用户编辑 crontab 条目。

有以下三种方法可以实现:

  1. 禁用非 root 用户 ssh 到系统,这反过来又禁用非 root 用户的 shell 登录本身。

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

# cat /etc/cron.deny 
oracle

另一个简单的解决方法是将 /etc/cron.deny 文件清空并仅将 root 用户名添加到文件 /etc/cron.allow 中。
这仅允许 root 用户修改/添加 cron 条目。

注意:确保文件 /etc/cron.allow 和 /etc/cron.deny 之间没有冲突。
有关 crontab 如何验证用户对 cron 的访问的更多信息,请参阅下面的帖子。

通过创建 crontab 条目验证被拒绝的用户。
它应该给你一个错误,如下所示。

# crontab -e
You (oracle) are not allowed to use this program (crontab)
See crontab(1) for more information
  1. 另一种激进的做法是去掉crontab命令的执行权限。
    这反过来会影响所有非 root 用户修改/添加 cron 条目的能力。

/usr/bin/crontab 文件的默认权限:

# ls -lrt /usr/bin/crontab 
-rwsr-xr-x 1 root root 51784 Jan 22  2016 /usr/bin/crontab

删除执行权限后:

# chmod 700 /usr/bin/crontab
# ls -lrt /usr/bin/crontab 
-rwx----- 1 root root 51784 Jan 22  2016 /usr/bin/crontab

注意:在更改文件权限之前,请确保我们有文件 /usr/bin/crontab 的备份。
另请注意,在软件包升级或者重新安装后,此更改将恢复为默认值。

请在任何更改之前注意 /usr/bin/crontab 文件的默认权限:

# stat /usr/bin/crontab
  File: `/usr/bin/crontab'
  Size: 51784     	Blocks: 104        IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 1318020     Links: 1
Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-09-13 09:39:27.192418684 +0530
Modify: 2016-07-22 12:50:39.000000000 +0530
Change: 2017-09-08 18:11:33.668586770 +0530

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

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