Contents
How to run a shell script as a different user?
: command to run – : option to su, make it a login session (source profile for the user) postgres : user to become I recommend always using full paths in scripts like this – you can’t always guarantee that you’ll be in the right directory when you su (maybe someone changed the homedir on you, who knows).
How to run Su script as another user?
When specified, su command will just execute the script without dropping into a new shell as the target user. In our example, we use the su command to execute the annie-script.sh with user annie. Then, su command will ask for annie ‘s password. Once authenticated, the script will be executed.
How to run a script as a superuser?
You should run your entire script as superuser. If you want to run some command as non-superuser, use “-u” option of sudo: When running as root, sudo doesn’t ask for a password. Well, you have some options. You could configure sudo to not prompt for a password.
Can you run the same script as root?
Once configured, we can now run the same command without the password prompt: However, if we try to run the same command as user root , su will ask for the root ‘s password. That’s because the password exemptions for dave only applies when he is executing scripts as annie, not as anyone else. 4. Using sudo
Can a script be executed as root after entering a password?
It doesn’t magically switch the UID of the current shell. If you type exit after entering your password, you’ll see that your script continues with the echo statement (but as the original user, not as root). You can put the part of the script that should be executed as root into a separate script, and execute that with su.
Can a Sudo script be run as root?
Assuming you’re actually running the script as root, however, you can use sudo. su is primarily for switching users, while sudo is for executing commands as other users. The -u flag lets you specify which user to execute the command as: sudo -u hudson command