LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-08-2007, 01:54 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Rep: Reputation: 30
how to become root within a script


Hello, i want to run a script as a regular user, and in that script, automatically become root so that the script can execute a command.

I'm using DSL Linux.

I tried putting sudo and sudo su before the script, but that didn't work.
This is an automated script, so i don't have the luxury of just typing in sudo root_only_script.sh from a bash window.

This is the idea:

Code:
#!/bin/bash
((become root))
root_only_script.sh
exit
The reason for this is because i have a samba share setup on the server but when files are moved to the share from windows computers, they require root permission in order to be changed. So i want to have a script that can change those permissions via a chmod 0777 -R /home/dsl/

The samba share is a no password, guests ok share. If there is a better solution than becoming root, that is welcome too.

-Thanks for the help
 
Old 01-08-2007, 02:09 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You need to use sudo with the nopasswd option, since you don't want sudo to prompt for a password. In addition, you need sudo configured to allow use without a tty (a script).

Run visudo, and make sure the following statement has the exclamation point:

Defaults !requiretty

That will now allow sudo from a script.

Add an entry that will allow the script to be executed by the user. For example:

someuser somehost = NOPASSWD: /usr/local/bin/somescript
 
Old 01-08-2007, 02:12 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
I think the way to do it would be to edit the sudoer file to add the username you will use, and the command to run from sudo.

Then, in the script, 'sudo <command>'.

In the sudoer file, <username> /path/to/root_only_script.sh

In the script, sudo /path/to/root_only_script.sh

I don't do such things, so I'm just guessing.

Last edited by bigrigdriver; 01-08-2007 at 02:14 PM.
 
Old 01-08-2007, 02:33 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You don't want to directly edit the sudoers file. The reason is that sudo is very picky - The slightest syntax error will prevent all sudos from working. The visudo command runs a syntax check before changing the file, to prevent such an error.
 
Old 01-08-2007, 03:11 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you have access to this samba server, you could call your script as a root cron job. Then the script will be running as root anyway. You will need to make sure that your script doesn't print our expect input, so you may need to add redirection to /dev/null for each command.
 
  


Reply



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
Root Password Script? wellington Linux - Software 5 05-30-2006 12:21 AM
Root Script Boffy Linux - General 4 09-26-2004 02:48 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
Changing to root from within a script bdika Linux - Software 2 01-01-2004 11:23 PM
Running a script as root davee Linux - Newbie 3 02-07-2003 05:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:02 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
Open Source Consulting | Domain Registration