无法以非 root 用户身份编辑 crontab

无法以非 root 用户身份编辑 crontab

当我以非 root 用户身份(在本例中为 postgres)在 Debian 服务器上使用该命令时crontab -e,我无法编辑它,因为"/tmp/crontab.SJlY0Y/crontab" [Permission Denied]

crontab -l另一方面工作正常。我该如何解决这个问题?

以下是当前权限:

$ ls -l /tmp/crontab.SJlY0Y/crontab
-rw------- 1 root postgres 1.2K Aug  3 11:44 /tmp/crontab.SJlY0Y/crontab
$ ls -l /var/spool/cron
total 12K
drwxrwx--T 2 daemon daemon  4.0K Sep 12  2012 atjobs
drwxrwx--T 2 daemon daemon  4.0K Jun  9  2012 atspool
drwx-wx--T 2 root   crontab 4.0K Aug  3 11:15 crontabs
$ ls -l /var/spool/cron/crontabs
total 12K
-rw------- 1 git      crontab 1.3K Mar  2 16:48 git
-rw------- 1 postgres crontab 1.4K Aug  3 11:15 postgres
-rw------- 1 root     root    2.3K Jul 20 20:32 root
$ ls -l /usr/bin/crontab
-rwsr-xr-x 1 root root 36K Jul  3  2012 /usr/bin/crontab
$ ls -ld /tmp/
drwxrwxrwt 6 root root 4.0K Aug  3 11:43 /tmp/

答案1

$ ls -l /usr/bin/crontab
-rwsr-xr-x 1 root root 36K Jul  3  2012 /usr/bin/crontab

所有权和许可实际上应该是

-rwxr-sr-x 1 root crontab 35880 Jul  3  2012 /usr/bin/crontab

从 Debian sarge 开始,crontab是 setgid crontab,而不是 setuid root,如要求的错误#18333

这是问题的原因:程序crontab期望运行 setgid,而不是 setuid,因此它会创建临时文件作为其运行时的用户和组,这是root调用者的主要组,而不是调用者的用户和crontab组。

重新安装cron包:

apt-get --reinstall install cron

(作为根)。检查是否/var/spool/cron/crontabs具有正确的权限和所有权:

drwx-wx--T 2 root crontab 4096 Oct  8  2013 /var/spool/cron/crontabs

以后不要乱搞系统文件的权限。

答案2

无法使用,apt-get --reinstall install cron因为它返回以下错误:

gateway:/home/firewall# apt-get --reinstall install cron
Reading package lists... Done
Building dependency tree... Done
Suggested packages:
  anacron logrotate lockfile-progs checksecurity
Recommended packages:
  exim4 postfix mail-transport-agent
The following NEW packages will be installed:
  cron
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/78.4kB of archives.
After unpacking 303kB of additional disk space will be used.
(Reading database ... 48768 files and directories currently installed.)
Unpacking cron (from .../cron_3.0pl1-100_i386.deb) ...
dpkg: error processing /var/cache/apt/archives/cron_3.0pl1-100_i386.deb (--unpack):
 ***unable to make backup link of `./usr/bin/crontab' before installing new version: Operation not permitted***
Errors were encountered while processing:
 /var/cache/apt/archives/cron_3.0pl1-100_i386.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
gateway:/home/firewall#

如果我尝试手动删除该文件,它也不允许。由于该文件没有权限,如何删除该文件?

gateway:~# ls -la /usr/bin/crontab
---------- 1 root crontab 26380 Dec 20  2006 /usr/bin/crontab

相关内容