通过sudo你成为root

通过sudo你成为root

我学会了使用装有 DOS 的计算机,因此我尝试使用近似批处理/.bat 文件来运行系统的所有更新并创建结果的日志文件。

#!/bin/bash
#update system in one step
cd ~
date > aupdate.log
echo Launching updates...
echo Beginning apt update
sudo apt update >> aupdate.log
sudo apt upgrade -y >> aupdate.log
echo Completing apt autoremove process
sudo apt autoremove -y >> aupdate.log
echo Beginning snap update
sudo snap refresh >> aupdate.log
echo Beginning flatpak update
flatpak update -y >> aupdate.log
echo Updates complete. Review aupdate.log for details.

我走的路对吗?到目前为止,我有两个问题需要解决。

  1. 未创建日志文件
  2. 如何在运行 apt > filename 时抑制有关重定向输出的警告(编辑:见下文)

编辑:这是我运行脚本时的结果:

Launching updates...
Beginning apt updates

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.


WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Completing apt autoremove process

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Beginning snap update
All snaps up to date.
Beginning flatpak update
Updates complete. Review aupdate.log for details.

编辑:根据评论中的一些早期反馈,我修改了脚本

#!/bin/bash
#update system in one step. Vers 1.1
date > ~/aupdate.log
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> ~/aupdate.log
apt-get upgrade -y >> ~/aupdate.log
echo Completing apt autoremove process
apt-get autoremove -y >> ~/aupdate.log
echo Beginning snap update
snap refresh >> ~/aupdate.log 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> ~/aupdate.log
echo Updates complete. Review aupdate.log for details.

切换到apt-get解决了警告,并使用2>/dev/nullsnap抑制了我刚刚注意到的快照输出。我还sudo从行中删除了,因为我以 su 身份运行脚本。但仍然没有日志文件

编辑:这是我现在以及ls之后运行脚本的结果。

eddie@mrpbell5:~$ sudo ./aupdate.sh
Beginning update...
Beginning apt-get updates
Completing apt autoremove process
Beginning snap update
Beginning flatpak update
Updates complete. Review aupdate.log for details.
eddie@mrpbell5:~$ ls ~ | grep log
eddie@mrpbell5:~$ ls -a
.           .bash_history  .cache      .config    Downloads  .local    Music     .profile  src                        .surfshark  Videos
..          .bash_logout   .cert       Desktop    .gnome     move      Pictures  Public    .ssh                       Templates   .wget-hsts
aupdate.sh  .bashrc        CiscoSpark  Documents  .gnupg     .mozilla  .pki      snap      .sudo_as_admin_successful  .var        .zoom

答案1

通过sudo你成为root

我使用 运行了您修改后的脚本sudo ./aupdate.sh

正如我所料,日志文件创建在 文件夹中/root/。这是因为,当您使用 执行命令(或脚本)时,sudo该命令由特殊用户 执行root。该用户root的“主目录”位于 处的一个特殊文件夹中/root/

日志应该存放到哪里?

您可以将日志放在主文件夹中/home/ehj/。但是,由于这是系统更新活动的日志,因此您可能希望将其保留在/var/log/

示例代码

我在您的脚本中添加了“环境变量”来保存有关日志文件的信息。

#!/bin/bash
# Location of log
LOG="/var/log/aupdate.log"
#update system in one step. Vers 1.1
date > "$LOG"
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> "$LOG"
apt-get upgrade -y >> "$LOG"
echo Completing apt autoremove process
apt-get autoremove -y >> "$LOG"
echo Beginning snap update
snap refresh >> "$LOG" 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> "$LOG"
echo Updates complete. Review "$LOG" for details.

LOG=...如果愿意,可以在以您的主文件夹开头的行中编辑日志文件的位置。

其他选项

无人值守升级

unattended-upgrades应用通常会保持安全更新。但是,它也可以用于保持其他应用更新。请参阅如何为任何存储库启用静默自动更新?如何进行设置。

unattended-upgrades还会记录它所做的事情,很像上面的脚本创建的日志。

别名

另一种方法是使用alias命令为命令列表创建别名。您必须创建一个名为的文件,/home/$USER/.bash_aliases其内容如下:

# My list of aliases
# Note: Since this is called by .bashrc 
# it seems this file does not need to be executable

alias update="sudo apt update && sudo apt -y upgrade && sudo apt -y --purge autoremove"

这个别名update并不保存更新日志但是做了类似的事情。

希望这可以帮助

相关内容