LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-24-2010, 05:29 PM   #1
rolandpish
Member
 
Registered: May 2007
Posts: 30

Rep: Reputation: 0
Question about sudo


Hi, in a linux ubuntu box, I have a bash script to automatize the creation of a svn repository (this is not a svn question).

In the script, I have these lines of code (among others):

Code:
# Create the repository
sudo -u www-data svnadmin create --fs-type fsfs $PATH$1 2>&1

# Create a pre-commit hook manually
sudo -u www-data echo '#!/bin/sh
REPOS="$1"
TXN="$2"
...some more content' > $PATH$1/hooks/pre-commit
Ok, the second sentence (sudo -u www-data echo ...) cannot complete because it shows a "permission denied" even though the $PATH$1/hooks is owned by www-data:www-data.

Interesting thing is that if I remove the "sudo -u www-data" from the script, and I execute it "outside" as: sudo -u www-data ./createrepo nameofrepo, no more "permission denied" errors are showed.

How can I achieve this "sudo -u www-data" inside the script?

Thanks in advance.

Cheers,
Roland
 
Old 08-24-2010, 06:43 PM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
you might check out this reference
http://linux.byexamples.com/archives...ot-permission/

The idea is
  • Write a script that does what you want it to do
  • Test that your script works while using root permissions
    Code:
    user@host:/path/ $ sudo -i
    user@host:/path/ # ... you are now "root" ...
    
    user@host:/path/ # ./myScript.sh opt1 ... optLast
  • Now you can run your script in everyday mode:
    Code:
    user@host:/path/ $ sudo myScript.sh opt1 ... optLast

You don't want to use sudo -i routinely. While it logs things and su - does not, it is too easy to screw things up at a root prompt.

Goo luck,
~~~ 0;-Dan
 
Old 08-24-2010, 06:53 PM   #3
ZaSter
LQ Newbie
 
Registered: Apr 2006
Posts: 9

Rep: Reputation: 0
Roland,

The difference is that the sudo inside the script becomes www-data to run the "echo" command and then the results of the sudo command is redirected to the $PATH$1/hooks/pre-commit file. In other words, you run the sudo command and redirect its output as your normal user account which probably does not have write permissions to the output file.

When you execute the script from "outside", you first become www-data that executes the createrepo. In this case, you are doing everything as "www-data" so you have its permissions all of the time. This is different from the first way in which you only have www-data permissions during the execution of the "echo" command.

I hope this help?

--
ZaSter
 
Old 08-24-2010, 07:49 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
are you sure $PATH is what you think it is? normally $PATH contains the directories to search in for executables (like /bin:/usr/bin:... )
 
Old 08-25-2010, 09:00 AM   #5
rolandpish
Member
 
Registered: May 2007
Posts: 30

Original Poster
Rep: Reputation: 0
Thanks for your replies!

Ok, your information helped me a lot, so I'm trying to find a way to "execute" the redirection as www-data inside the script. I've been googling around but with unsuccessful results.

Any suggestion?

Thanks a lot!
 
Old 08-25-2010, 05:41 PM   #6
ZaSter
LQ Newbie
 
Registered: Apr 2006
Posts: 9

Rep: Reputation: 0
Roland,

Based upon the code you supplied, simply move the last single quote passed the output file name. In other words, change this:

Code:
# Create the repository
sudo -u www-data svnadmin create --fs-type fsfs $PATH$1 2>&1

# Create a pre-commit hook manually
sudo -u www-data echo '#!/bin/sh
REPOS="$1"
TXN="$2"
...some more content' > $PATH$1/hooks/pre-commit
to this:

Code:
# Create the repository
sudo -u www-data svnadmin create --fs-type fsfs $PATH$1 2>&1

# Create a pre-commit hook manually
sudo -u www-data echo '#!/bin/sh
REPOS="$1"
TXN="$2"
...some more content > $PATH$1/hooks/pre-commit'
 
Old 08-26-2010, 08:51 AM   #7
rolandpish
Member
 
Registered: May 2007
Posts: 30

Original Poster
Rep: Reputation: 0
Thanks a lot ZaSter!
I'll try it that way when I get home.

Regards
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
LXer: sudo, or not sudo: that is the question LXer Syndicated Linux News 0 02-07-2008 05:40 PM
Restricting Editing in Sudo (Advanced Sudo Question) LinuxGeek Linux - Software 4 11-04-2006 03:20 PM
sudo question satimis Linux - Software 3 12-07-2004 08:47 AM
Sudo question. wbdune Linux - Newbie 2 10-14-2003 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:59 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