Heavybullets8/heavy_script

install error This script must be run as root.

jelle12345678 opened this issue · 9 comments

i want to install heavy script but i get the error ( This script must be run as root. ) does anyone know how i can fix that

Run the script as root (sudo <script>)

I just did a fresh install of TrueNAS Scale. As of this writing, I'm on version 22.12.3.3. I'm pretty new to truenas but have a little bit of linux knowledge. For me putting sudo on the front did not work from the shell in the GUI. I had to "sudo su -" give the admin password then from there it dropped me into root prompt. I shift+insert the one-liner and it installed.

I also noticed to run the heavyscript command I also need to be in the root prompt. I guess that is how it is supposed/has to run. #linuxnoob :-D

This probably needs to be looked at in more depth considering iX is talking about removing root logins completely in a "soon to come" release (they already are discouraging even allowing root)... the requirements of a home directory might make things interesting too

I need your guys' feedback on the implementation prior to me pushing the changes to main.

If you already have heavyscript installed, you can just change branch, otherwise go with the installation, both methods will REQUIRE you to switch to the permissions branch.

Installation

The one liner below uses the new branch, which contains the new implementation, this will allow you to install into a non-root user

curl -s https://raw.githubusercontent.com/Heavybullets8/heavy_script/permissions/functions/deploy.sh | bash && source "$HOME/.bashrc" 2>/dev/null && source "$HOME/.zshrc" 2>/dev/null

Changing Branches

heavyscript git --branch

Select the Permissions branch

Changes Made

Changes to /bin/heavyscript

  1. Check if the script is being run with root privileges (sudo). If so, look for the heavy_script directory in the home directory of the user who invoked sudo.

  2. If the script is not being run with sudo, or if the heavy_script directory is not found in the first step, then look for the heavy_script directory in the current user's home directory.

  3. If the heavy_script directory is not found in either of the above locations, use a custom find_script_dir function to search all user-created directories for the heavy_script directory.

The seemingly convoluted search is due to the wrapper script not knowing where heavy_script was originally installed. However, it is most likely to be in one of the first two locations mentioned.

Changes to functions/deploy.sh

  1. Allow users to install with non-root users
  2. Create symbolic links to both /usr/local/bin as well as the users bin $HOME/bin

Both symbolic links are unfortunately required to allow both the sudo users, and the user who installed the script to be able to view it in their $PATH environment. As you guys know, the sudoers $PATH is reset on each upgrade of Truenas SCALE

Changes to heavy_script.sh

  • A new function, check_root, is added to determine if root access is required for specific commands (e.g., update, dns). Most heavy_script functions require root access.

    • If the user just wants the help page, it is displayed without needing root.
    • Otherwise, the script exits with an error message stating that root privileges are required.
  • A second function, ensure_symlink, makes sure a symbolic link is present in /usr/local/bin/heavyscript.

    • If missing, a warning is displayed suggesting the script be run as sudo.
    • Cron jobs, assumed to be a common use-case, require root and the symbolic link is wiped with each TrueNAS SCALE update, thankfully the cron job should re-establish the symbolic link, so hopefully no user interaction is actually required for those of you, who use cron jobs frequently.

New Permissions File: utils/permissions.sh

  • Contains both the check_root and ensure_symlink functions.

Thanks for your work :)

sudo heavyscript
main: Error: /root/heavy_script does not exist.
permissions: success!

Commands that require root access are broken when running as non-root. For example, List DNS Names:

WARN[0000] Failed to stat /mnt/A/ix-applications/k3s/agent/etc/crictl.yaml: permission denied 
FATA[0000] load config file: stat /mnt/A/ix-applications/k3s/data/814fb91b7bfff921102404a4b77e94cfa15ada3c1c4735f6560f515889994d38/bin/crictl.yaml: no such file or directory 
Error: failed to retrieve pod names
There are no applications ready

My preference is for the script to ask for permission when necessary. Like when I choose "List DNS Names" it should prompt for sudo password. At the very least, exit with a helpful notice like "This feature requires the script to be run as root (sudo)".

@mjforan that's exactly what it's supposed to do now. Can you verify that you've swapped to the correct branch? Also, you may need to run it manually with sudo a single time with the actual script path to establish the link.

sudo bash ~/heavy_script/heavy_script.sh

Afterwards, you should be able to run just

sudo heavyscript 

Looks like I forgot to add the check_root function to the menu options, this is why I want testers haha.. Those have now been added, and should exit when the user selects an option that always requires root. Such as any application options, backup options, etc.

Update while on the permissions branch with:

heavyscript self-update

Nice, working now.

Great, If you have any suggestions, or run into any issue,s or just have any feedback to supply whatsoever. Please let me know.
I want to make sure that this is done right, before I push it to main.