LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-25-2008, 02:14 PM   #1
mcd
Member
 
Registered: Aug 2003
Location: Boulder, CO
Distribution: Slackware, RHEL, CentOS
Posts: 825

Rep: Reputation: 33
sudo and >>: permission denied


I ran into this interesting problem today. I have the following directory:

drwxr-xr-x 2 mcd mcd 168 2008-06-25 12:05 .
drwxr-xr-x 23 mcd mcd 1360 2008-06-25 12:04 ..
-rw-r--r-- 1 root root 15883 2008-06-25 12:05 dmesg.txt
-rw-r--r-- 1 root root 655133 2008-06-25 12:05 messages.txt
-rw-r--r-- 1 root root 0 2008-06-25 12:05 out
-rw-r--r-- 1 mcd mcd 2114 2008-06-25 12:05 update.txt


These are test files I just created. Now, I want to cat the contents of the text files and append them to out:

$ sudo cat *.txt >> out

Only I get permission denied:

-bash: out: Permission denied

It's the same if I try these:

$ sudo cat *.txt > out
$ sudo cat *.txt | sudo >> out


But if I su to root, and then just run cat *.txt >> out it works just fine. So it seems like the change of identity does not "propagate" past the >>. Sudo's man page doesn't help me understand this any better. Does anyone have any insight into what's actually going on here?

Thanks,

McD
 
Old 06-25-2008, 02:49 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Actually the man page DOES say you have to run it in a subshell to allow redirection to work (or gives an example that tells you that):

Quote:
To make a usage listing of the directories in the /home partition.
Note that this runs the commands in a sub-shell to make the cd and file redirection work.

$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"

So to do what you want you just need to modify your command line:

sudo sh -c "cat *.txt >> out"
 
1 members found this post helpful.
Old 06-25-2008, 04:30 PM   #3
fptt
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 4
Command:
sudo cat *.txt >> out

The shell you're using interprets this as.

Run command:

sudo cat *.txt # Elevates privs

Then take the output and put it into file out.

The first part is run as root 'cat *.txt' then your shell, running as you and outside of the sudo escalated privs, tries to open a file 'out' and put the output in it.

Thus the permission denied.

Hopefully this made a tiny bit more sense to you.

The subshell example above in this thread should fix it as well.
 
1 members found this post helpful.
  


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
can't execute c++ binaries, "permission denied"... even though permission is 777 SerfurJ Programming 14 02-20-2009 05:50 AM
'permission denied" inspite of right permission flags on network drive anirudhvij Linux - Enterprise 8 05-22-2007 06:57 AM
"sudo mount -a" works perfect but permission denied when mounting on startup, why? aaron4katie Linux - Software 10 01-11-2007 11:16 AM
sudo - permission denied! yogaboy Linux - Newbie 5 12-29-2006 12:58 AM
sudo permission denied Swift&Smart Slackware 5 07-13-2006 02:05 AM


All times are GMT -5. The time now is 06:40 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration