Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bash Scripting Help

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
MGE



Joined: Thu Sep 08, 2005 3:53 am
Posts: 94
Location: Manchester

PostPosted: Wed Jan 25, 2006 2:33 am    Post subject: Bash Scripting Help Reply with quote

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
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Wed Jan 25, 2006 9:09 am    Post subject: RE: Bash Scripting Help Reply with quote

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.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8450
Location: Warrington, UK

PostPosted: Wed Jan 25, 2006 9:39 am    Post subject: RE: Bash Scripting Help Reply with quote

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:
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:
EDITOR=kate visudo

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
MGE



Joined: Thu Sep 08, 2005 3:53 am
Posts: 94
Location: Manchester

PostPosted: Wed Jan 25, 2006 11:39 am    Post subject: RE: Bash Scripting Help Reply with quote

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

I'll give that a go.

Thanks again Smile
_________________
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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast