Bash Scripting Help

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Bash Scripting Help

Postby MGE » Wed Jan 25, 2006 2:33 am

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.
Pentium 4 3.2Ghz Prescott 1MB Cache
Abit IC7 (875P Chipset)
3GB Corsair Dual Channel DDR
Nvidia GeForce 6600GT
500GB (SATA2-ReiserFS) Main drive
500GB (PATA-ReiserFS) Media drive
120GB (PATA-NTFS) Legacy data
OS: SimplyMEPIS 8
User avatar
MGE
 
Posts: 94
Joined: Thu Sep 08, 2005 2:53 am
Location: Manchester

RE: Bash Scripting Help

Postby MartyBartfast » Wed Jan 25, 2006 9:09 am

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.
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 819
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

RE: Bash Scripting Help

Postby nelz » Wed Jan 25, 2006 9:39 am

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
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8555
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: Bash Scripting Help

Postby MGE » Wed Jan 25, 2006 11:39 am

Thanks everyone, I had been trying sudo but hadn't set up the sudoers file correctly.

I'll give that a go.

Thanks again :)
Pentium 4 3.2Ghz Prescott 1MB Cache
Abit IC7 (875P Chipset)
3GB Corsair Dual Channel DDR
Nvidia GeForce 6600GT
500GB (SATA2-ReiserFS) Main drive
500GB (PATA-ReiserFS) Media drive
120GB (PATA-NTFS) Legacy data
OS: SimplyMEPIS 8
User avatar
MGE
 
Posts: 94
Joined: Thu Sep 08, 2005 2:53 am
Location: Manchester


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest