I want to run command as root inside a bash script.
This command needs to be able to read from STDIN.
I have a root password as a local variable inside bash script and don't want to enter it manually.
Whan I've tried:
I know approach with running initial script as root (and actually using it, but looking for better ways to live) but it has it's limitations when complex logic, different users and environment variables involved.
I know about sudo-NOPASSWD approach, but it's not something I want to use for security reasons.
I know
su -c
approach, but it makes me enter root password every time:
su - -c "adduser new_user"
- I know heredoc approach but it blocks STDIN for me:
read -sp "Please, enter root password: " ROOT_PASSWD
echo
su - <<EOI
$ROOT_PASSWD
echo "<This password input is handled automatically based on previously asked password>"
Do NOT asks user password and data as supposed to:
adduser new_user
EOI
t
) has nothing whatsoever to do with setuid privileges. Furthermore scripts ignore setuid bits and have done for years – Chris Davies Sep 14 '22 at 06:57printf '#!/bin/sh\nid\n' > suidtest.sh && chmod u+s,a+xr suidtest.sh
and then running that. – ilkkachu Sep 14 '22 at 20:35-p
option, it'll switch back – ilkkachu Sep 14 '22 at 20:36sudo
to allow running a script under the changed UID. And make a wrapper script withsudo somescript.sh "$@"
if you want to hide the fact that you're usingsudo
from the user. (Or create a five-line C wrapper program to runset*uid()
andexecve()
) – ilkkachu Sep 14 '22 at 20:38