LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-07-2010, 01:10 AM   #1
digity
Member
 
Registered: Apr 2005
Posts: 105

Rep: Reputation: 15
Running shell scripts, commands need root/su, how?


I've written a couple of shell scripts and some of the commands in them need root privileges. I've read somewhere to never put the root or super user's password in a script because it's not encrypted. How do I run these commands as root/su in a script?
 
Old 01-07-2010, 01:12 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
sudo scriptname
 
Old 01-07-2010, 01:20 AM   #3
digity
Member
 
Registered: Apr 2005
Posts: 105

Original Poster
Rep: Reputation: 15
oops, sorry. They won't be ran manually, they'll be kicked off by cron. So it has to be automated, no user input required.
 
Old 01-07-2010, 02:13 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You can configure sudo to not require a password for specific commands.

Evo2.

PS. However, if you do this for the script, make very sure that the script is only writable by root. A slightly better approach is just to use sudo for the specific commands where root is needed, not for the whole script.

Last edited by evo2; 01-07-2010 at 02:15 AM.
 
Old 01-07-2010, 02:46 AM   #5
digity
Member
 
Registered: Apr 2005
Posts: 105

Original Poster
Rep: Reputation: 15
hmmm. how about "sudo crontab -e"? won't that make my scripts run with root privileges?
 
Old 01-07-2010, 06:19 AM   #6
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
@ digity - you are correct.

Just add it to root's crontab instead of yours.
Running from inside su or sudo can confuse crontab (see man crontab) so:

sudo -i
crontab -e
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running Commands from Ruby scripts... custangro Programming 2 10-05-2008 09:28 PM
able to run root-only commands via php scripts? Swakoo Linux - General 2 01-09-2006 07:01 AM
Running Scripts/Commands on Startup drknow Linux - Newbie 4 01-09-2004 06:14 AM
Linux commands in shell scripts james_cwy Linux - Newbie 4 11-10-2003 02:14 AM
Running ftp commands in scripts john lee Linux - Newbie 4 05-07-2003 01:19 AM


All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration