Page 1 of 1

Bash Scripting Help

PostPosted: Wed Jan 25, 2006 2:33 am
by MGE
Howdy

I have been trying to create a script to automate various processes, however, I can't figure out how to run certain parts of the script as root, and certain parts as my normal user.

I don't want to run the entire script as root, just the odd section.

I tried just using the su command, and then realised that I was now a totally different user and no longer executing my script.
Arse.

I realise su isn't the best idea, however for testing purposes, its fine.
Is there a way to do this?
Am I nuts for even thinking about using su in a script?

My second idea was to start another shell as root, however I'm not entirely sure
how to do that from a script.

Cheers in advance.

RE: Bash Scripting Help

PostPosted: Wed Jan 25, 2006 9:09 am
by MartyBartfast
Sounds like you want to use sudo, e.g

sudo kill 1234

will kill off a process using root privs.

You need to add an entry to /etc/sudoers which defines which users can execute sudo, and which commands they can execute. e.g. you could specify in sudoers that user fred can only execute cp (e.g. if you want to copy protected files) but can't run any other commands.


man sudo
should give you all the info you need.

RE: Bash Scripting Help

PostPosted: Wed Jan 25, 2006 9:39 am
by nelz
Yep, sudo is the way to go. You'll need to specify the full path to the commands that you want the user to be able to run, and tell sudo not to ask for a password for these commands. Otherwise it will stop the script to prompt for your password (not the root password). Here's a part of my /etc/sudoers file.

Code: Select all
nelz    ALL =  NOPASSWD: /bin/mount,/bin/umount


Note the comment at the top of /etc/sudoers, this file must be edited with the visudo command, not loaded directly into an editor. Run visudo as root and it will load the file into whatever program you have defined in $EDITOR. You can change this at the time you run visudo with, for example

Code: Select all
EDITOR=kate visudo

RE: Bash Scripting Help

PostPosted: Wed Jan 25, 2006 11:39 am
by MGE
Thanks everyone, I had been trying sudo but hadn't set up the sudoers file correctly.

I'll give that a go.

Thanks again :)