The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirection of output to a log file JohnCrump UNIX for Dummies Questions & Answers 8 08-22-2008 01:25 AM
who truncates the output? redirection? tty? Bug? fredy UNIX for Advanced & Expert Users 7 12-09-2006 11:21 PM
Redirection of output (for logging) _Spare_Ribs_ Shell Programming and Scripting 3 12-04-2006 11:17 AM
redirection of ladebug output yakari UNIX for Advanced & Expert Users 2 10-05-2006 11:23 PM
Standard output and redirection jerardfjay Shell Programming and Scripting 2 06-27-2005 08:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-17-2008
Registered User
 

Join Date: Sep 2008
Posts: 7
Elevated privilege output redirection

If one wants to create a file with some content, one might use:
Code:
echo 'my content' > new.conf
In the event that the target file requires elevated privileges, such as root access to write the file, one might think to just slap sudo on the front of it, as ever:
Code:
sudo echo 'my content' > new.conf
However, this just runs `echo` as root, and the target file still cannot be written. How might one overcome this?
Reply With Quote
Forum Sponsor
  #2  
Old 09-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Create the file as a regular user, then move it on top of the old conf using sudo.
Reply With Quote
  #3  
Old 09-17-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
OR sudo a chmod command that will allow writing -- plus, all of this sounds very much like a security breach in the making. Why are you writing to some type of conf file as a plain user? Do you want all unprivileged users to be able to change files that used to be protected?
Reply With Quote
  #4  
Old 09-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
I wouldn't take things so seriously. Ubuntu for example basically suggests you do all administration through sudo, and creating the config as the normal you is just good hygiene. Just make sure the file you commit is only writable by root!
Reply With Quote
  #5  
Old 09-17-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,931
Quote:
Originally Posted by Bilge View Post
[...]However, this just runs `echo` as root, and the target file still cannot be written. How might one overcome this?
The shell redirection has higher precedence and is performed before the sudo command.
Try this:

Code:
% sudo touch /tmp/f
% sudo > /tmp/f
zsh: permission denied: /tmp/f
% sudo sh -c '> /tmp/f'
%
So your code becomes:

Code:
sudo sh -c 'echo "my content" > new.conf'
Reply With Quote
  #6  
Old 09-18-2008
Registered User
 

Join Date: Sep 2008
Posts: 7
Quote:
Originally Posted by radoulov View Post
Code:
sudo sh -c 'echo "my content" > new.conf'
Ah, nice one. I see you are invoking a new shell to perform the command. It's good that doing so only involves a few extra key presses. I'll have to add "sh -c" to my local memory banks ;)
Reply With Quote
  #7  
Old 09-18-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 605
Quote:
Originally Posted by Bilge View Post
Ah, nice one. I see you are invoking a new shell to perform the command. It's good that doing so only involves a few extra key presses. I'll have to add "sh -c" to my local memory banks

Yes, you might also learn about the 'tee' command. This outputs to a every file you name on the command-line AND to stdout. So:

Code:
do_some_work | sudo tee outfile.dat
Results in work being sent to outfile.dat, which is create with root permissions.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0