定期在桌面系统上运行rkhunter
当rkhunter在我的系统上发现奇怪的东西时,我想在桌面上收到警告。
我将/etc/rkhunter.conf
一些文件和目录列入白名单,因此我不再收到任何警告。
现在我想把这个命令放在某个地方:
sudo rkhunter --checkall --report-warnings-only | while read OUTPUT; do notify-send "$OUTPUT"; done
我知道如何使用cron
但是这不起作用,因为我的计算机是在不规则的时间运行的,所以我必须把它放在哪里这样它每天执行一次而不是在系统启动期间执行? 启动后最佳时间为30分钟。
使用anachron
和notify-send
解决方案
该问题的答案是anachron 以root身份自动执行命令,root需要访问主用户的dbus会话。
1.授予您桌面会话的root访问权限(以用户身份)
要让root用户访问默认用户的桌面,首先需要设置DBUS_SESSION_BUS_ADDRESS
变量。 默认情况下,cron无权访问更改每个系统启动的变量。 要解决此问题,请将以下脚本放在主目录中并将其命名为~/dbus-session-export
#!/bin/sh touch ~/.dbus/Xdbus chmod 600 ~/.dbus/Xdbus env | grep DBUS_SESSION_BUS_ADDRESS > ~/.dbus/Xdbus echo 'export DBUS_SESSION_BUS_ADDRESS' >> ~/.dbus/Xdbus exit 0
赋予它可执行权限:
chmod +x ~/dbus-session-export
并在你的启动程序中调用它。 这将创建/更新文件~/.dbus/Xdbus
其中包含在每个系统引导时使用的anachron所需的Dbus环境变量。
2. Cron脚本(以root身份)
将脚本放在/etc/cron.daily/
文件夹中并使其可执行:
sudo touch /etc/cron.daily/rkhunter-check sudo chmod +x /etc/cron.daily/rkhunter-check
编辑文件gksu gedit /etc/cron.daily/rkhunter-check
#!/usr/bin/env bash sleep 1800 # wait 30 minutes in case the script is called directly at boot MAINUSER="$(awk -F: '$3==1000{print $1}' /etc/passwd)" if [ -r "/home/$MAINUSER/.dbus/Xdbus" ]; then . "/home/$MAINUSER/.dbus/Xdbus" fi su $MAINUSER -c 'notify-send "starting rkhunter scan... "' rkhunter --checkall --report-warnings-only | while read OUTPUT; do if [ "$OUTPUT" != "" ]; then OUTPUT="${OUTPUT//[\`\"\']/}" su $MAINUSER -c $"notify-send \"rkhunter: $OUTPUT\"" fi done
这将每天运行一次脚本,如果rkhunter运行生成任何输出(仅警告),此脚本将显示为屏幕右上角的每个警告的通知
资源:
- https://unix.stackexchange.com/questions/111188/using-notify-send-with-cron
- 如何在root用户启动的屏幕上创建通知
在启动时运行,以zenity
显示
创建一个文件/usr/local/sbin/rkhunter-check
并使其可执行:
sudo touch /usr/local/sbin/rkhunter-check sudo chmod +x /usr/local/sbin/rkhunter-check
编辑文件gksu gedit /usr/local/sbin/rkhunter-check
#!/usr/bin/env bash export DISPLAY=:0 MAINUSER="$(awk -F: '$3==1000{print $1}' /etc/passwd)" LOG=/tmp/.rkhunter-warnings rm -f $LOG touch $LOG rkhunter --checkall --report-warnings-only | while read OUTPUT; do if [ "$OUTPUT" != "" ]; then OUTPUT="${OUTPUT//[\`\"\']/}" echo "$OUTPUT">>$LOG fi done if [ "$(cat $LOG)" = "" ]; then #like this there is always a notification, even if there is no warning, it will show an empty notification. echo "#no warnings">$LOG fi if [ "$(cat $LOG)" != "" ]; then su $MAINUSER -c 'zenity --text-info --width 800 --title "Rkhunter warnings" < '"$LOG" fi
如果rkhunter run生成任何输出(仅警告),则此脚本将显示为带有rkhunter输出的可滚动窗口。
-
创建一个systemd启动脚本
创建脚本
/etc/systemd/system/rkhunter.service
:[Unit] Description=starts rkhunter and displays any findings with zenity [Service] ExecStartPre=/bin/sleep 1800 ExecStart=/usr/local/sbin/rkhunter-check [Install] WantedBy=default.target
更新systemd:
sudo systemctl daemon-reload sudo systemctl enable rkhunter sudo systemctl start rkhunter
-
从
/etc/rc.local
开始在没有
systemd
系统上,在运行时在/etc/rc.local
调用脚本并在整个命令之前调用sleep:gksu gedit /etc/rc.local
在
/etc/rc.local
中包含exit 0
的最后一行之前添加此命令:sleep 1800 && /usr/local/sbin/rkhunter-check &
在以root身份执行rkhunter检查之前,两个解决方案都将等待30分钟。
您还可以将此解决方案与notify-send解决方案结合使用,因为如果没有警告,zenity对话框并不完美。 在这种情况下,通知就足够了
#!/usr/bin/env bash export DISPLAY=:0 MAINUSER="$(awk -F: '$3==1000{print $1}' /etc/passwd)" LOG=/tmp/.rkhunter-warnings echo ""> $LOG rkhunter --checkall --report-warnings-only | while read OUTPUT; do if [ "$OUTPUT" != "" ]; then OUTPUT="${OUTPUT//[\`\"\']/}" echo "$OUTPUT">>$LOG fi done if [ "$(cat $LOG)" = "" ]; then MAINUSER="$(awk -F: '$3==1000{print $1}' /etc/passwd)" if [ -r "/home/$MAINUSER/.dbus/Xdbus" ]; then . "/home/$MAINUSER/.dbus/Xdbus" fi su $MAINUSER -c $"notify-send \"rkhunter: no warnings\"" fi if [ "$(cat $LOG)" != "" ]; then su $MAINUSER -c 'zenity --text-info --width 800 --title "Rkhunter warnings" < '"$LOG" fi
source: 如何以root身份在引导期间运行脚本
你可以使用cron
。 编辑:
crontab -e
有关如何使用cron
更多信息,请点击此链接:
crotab教程