0

there's a script that I have and it needs to be run by root user. I just wanted to know how to run that script using su in the script. - I'm running CentOS7.

The command I attempted (below) failed:

su root credentials=/home/root/root.cred

and then continue with the rest of the script - sadly it didn't work. I included the .cred file because root asks for the root password when changing to root. Is there a way to run su root and change to root in a script.

Any ideas?


I may be getting confused but I just want to explain further:

I have a script that I want to be able to run from a regular (non sudoer) user, but this command is a sudo command - therefore only executable by a sudoer. Root is a sudoer, so when the script is run by the regular user - it will run as root in the script. If I begin the script with su root it can't get past that because it requires a password. Is there a way to enter the root password to get past the su command and into running the rest of the script using the script itself. Maybe echo or something? - I have no idea, this is why I am making this question.

Any futher questions, just ask me below :)

AdminBee
  • 22,803
ekv_56
  • 57

2 Answers2

2

You have to add an entry in the /etc/sudoers file to be able to run just your script as root without using any password:

username ALL = (root) NOPASSWD: /your/script's/absolute/address

You have to replace the username with your own user name.

But it's dangerous in the sense that if a non-privileged (i.e non-sudoer) user tampers your script, then he/she can run whatever program he/she wants with the root permissions without any password, so a security measure is to make that file read-only for non-root users:

sudo chmod 755 your_script

By doing this you're giving the permission of reading and executing (but not writing) the script to non-sudoer users.

And also to prevent any future reversion:

sudo chown root your_script

By doing this, you're granting the ownership of the file only to root.

Fjor
  • 187
Parsa Mousavi
  • 1,090
  • 2
  • 17
  • 30
0

There is not a mechanism to "convert user to root for the rest of this script" safely. All means of obtaining root permissions from an user level have to start a separate instance of the shell or program with the new credentials to rise the program's rights. To solve this for user typing commands in the shell we should use the sudo system.

For your problem, the solution is to separate the script section you want elevated into another script and launch it with sudo at the end of the first 'normal user' part. As shown in the Parsa Mousavi's answer, you will use visudo to edit the sudoers file and configure this, and could give rights to the normal user to execute the root script without entering a password, if you like.

IMPORTANT: You must indicate the full path of the script and arguments (if any) in the sudoers definition, and also write exactly the same path and arguments in the calling script. Say, if you authorize /bin/mount /cdrom in the sudoers file, in the calling script must say sudo /bin/mount /cdrom for this to work. If your script does not have any arguments in the sudoers file, like /root/my_script.sh, then you'll call it with sudo /root/my_script.sh without arguments. Also, you have to make the script owned by root (and apply chmod go-w to it), so none can modify it and improperly execute unauthorized commands.

Fjor
  • 187