为什么以 root 身份运行 Cron 作业会停止 sshd 服务并禁止其他用户登录?

为什么以 root 身份运行 Cron 作业会停止 sshd 服务并禁止其他用户登录?

我有一个脚本,用于从另一台服务器获取 Clamav 更新,我以 root 身份作为 Cron 作业运行。另外,我禁用了 root 登录/sbin/nologin。但这对系统有严重影响,因为它会停止 sshd 服务,删除我的用户的 /bin/bash 登录名。这些是我达到这种状态所采取的步骤:-

  1. 创建任何可执行脚本 int /root/。就我而言,这是一个更新 Clamav 的脚本。
  2. 通过将 root 用户的 shell 从 /bin/bash 更改为 /sbin/nologin 来确保禁用 root 登录。
  3. 在 /etc/crontab 中手动添加一个条目,以 root 用户身份每分钟运行此脚本。
  4. 现在重新启动 ssh 服务并在 5 分钟后尝试 sshing。

随着我的/etc/crontab进入。

* *  * * * root    /root/clamu.sh >/dev/null 2>&1

以下是clamu.sh脚本

#this script need to run by root user

#create a folder temp
mkdir /temp

#go to the temp dir
cd /temp

#clean this folder
rm -f *

#Copying the files from server to temp dir

scp -P 2277 [email protected]:/var/lib/clamav/daily.* .
scp -P 2277 [email protected]:/var/lib/clamav/bytecode.* .

#Updating the files to actual location

rm -f /var/lib/clamav/daily.*
cp -u daily.* /var/lib/clamav/
cp -u bytecode.* /var/lib/clamav/

#do scanning & save report

cd /
clamscan --recursive=yes >> /var/log/clamav/clamd.log

# Clean the /temp folder
cd /temp
rm -f *

我在用CentOS 7 x64。虽然我不认为这与分布有关,但这是一个常见问题。

我知道问题是由于以某种方式在 cron 中以 root 身份运行此脚本引起的。但为什么它会导致 ssh 失败和内核恐慌是我想知道的,只是为了了解发生了什么。我是一名业余 Linux 用户,当然希望得到一些解释。谢谢。

编辑 你好,因为我没有表现出任何研究工作,因为这不是最初的问题。这发生在我的一台远程虚拟机中,并且我没有日志来调试它。在弄清楚如何访问日志后,我确实尝试解决问题,并且能够通过在 crontab 条目中使用不同的用户来解决它。但问题仍然存在,一个 cron 作业如何造成如此大的破坏。这就是我需要的答案,甚至是一个方向,以便我能够找到答案。

答案1

真的存在吗/temp(你不是说的吗/tmp)?

如果没有,那么您的脚本将尝试cd执行/temp,失败,然后所有命令都在起始目录中运行。

最后两个命令特别危险,因为您cd先 to /,然后cdto /temp(可能不存在),然后是rm所有内容(很可能是根文件系统)。

您应该为rm命令指定完整路径。

我不知道为什么会出现您所看到的结果,但该脚本非常危险,因此我首先修复它,看看是否可以解决问题。

答案2

在我看来,你永远不应该完全禁用 root 用户。如果您不希望能够使用 root 身份登录,则应该在 中ssh设置指令。PermitRootLogin no/etc/ssh/sshd_config

对于其他应用程序,可以进行大部分等效设置。

相关内容