Contents
- 1 Why can you use sudo Su within a shell script?
- 2 Why is the sudo command not found in Bash?
- 3 Can a Sudo script be invoked as root?
- 4 Why do I use Bash instead of Su?
- 5 Can you run Su in the middle of a bash script?
- 6 Can you use a CD in a Sudo script?
- 7 Can a script be run without Sudo privileges?
- 8 How long does it take Sudo to CACH a script?
Why can you use sudo Su within a shell script?
Because running “sudo su” opens a new shell and the command does not return until you exit from that shell. Perhaps split the script into 2 files: first one runs sudo and executes that 2nd script under sudo. MK. MK. sudo su will attempt to start a new shell as root.
Why is the sudo command not found in Bash?
Some UNIX shell does not support source. instead, they support . So try this It doesn’t work because source is a built in command, not a program. I wrote a bash script to force sudo on built in commands: Save the file as forceSudo and save it in your scripts location, possibly ~/.local/bin.
Why does Sudo not run after exec Sudo?
If the line after exec sudo gets executed, it means the shell failed to even start sudo and something is really wrong. If the user is authorized to run the script, sudo may or may not request a password; if a wrong password is entered or the user is not authorized to run the script as root, a standard sudo error message will be displayed.
Why does Sudo not run the script as root?
If the user is authorized to run the script, sudo may or may not request a password; if a wrong password is entered or the user is not authorized to run the script as root, a standard sudo error message will be displayed. If succesful, sudo will start the script a second time, now running with root privileges.
Can a Sudo script be invoked as root?
If not invoked as root, the script reinvokes itself with sudo -s, passing all arguments through as-is. Unless previously authenticated and still within the timeout period, sudo will prompt for an administrator password.
Why do I use Bash instead of Su?
Using bash -c instead of su -c to interpret the reconstructed command line makes the command more portable, because not all platforms support su -c (e.g., macOS doesn’t). In bash, $BASH_SOURCE is generally the more reliable way to refer to the running script.
Why are path variables different when running via sudo and Su?
Other reason why the environment could be different for sudo, is because you could have env_reset option enabled in your sudoers file. This causes commands to be executed with a new, minimal environment. So you can use env_keep option (not recommended for security reasons) to preserve your user’s environment variables:
How to run Sudo script without root permissions?
Don’t use plain sudo in scripts, run the whole script with root permissions instead and use sudo -u YOURUSERNAME to execute commands without root permissions if that’s really necessary. Save it, make it executable with chmod +x /path/to/script and start it with root permissions with:
Can you run Su in the middle of a bash script?
You can, but bash won’t run the subsequent commands as postgres. Instead, do: The -c flag runs a command as the user (see man su ). You can use a here document to embed multiple su commands in your script:
Can you use a CD in a Sudo script?
EDIT: As Douglas pointed out, you can not use cd in sudo since it is not an external command. You have to run the commands in a subshell to make the cd work. You may be asked to input that user’s password, but only once. It’s not possible to change user within a shell script.
How do I remove Sudo from BASH script?
If you want to enter a password for the user, remove the sudo. If you do not need the environment variables, remove -E from sudo. The /usr/bin/bash -l ensures, that the profile.d scripts are executed for an initialized environment.
How to find the root user of a Sudo script?
However, in case you want to drop root privileges to some of the commands and run them as the actual user who invoked the command with sudo, you can check for the $SUDO_USER variable to figure out the original user. #!/bin/bash # ref: https://askubuntu.com/a/30157/8698 if ! [ $ (id -u) = 0 ]; then echo “The script need to be run as root.”
Can a script be run without Sudo privileges?
That way, all commands within the script will be run with root privileges and you only need to give the password once when launching the script. If you need a particular command within the script to be run without sudo privileges, you can run it as a regular user with (thanks Lie Ryan ):
How long does it take Sudo to CACH a script?
The default caching time is 15 minutes. If you want to run a script longer than that and still keep the privilege without typing password again, you don’t need to worry about anything: simply run the script with sudo. In this case, whether each command inside the script contains sudo doesn’t matter.