Elevated privilege output redirection


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Elevated privilege output redirection
# 1  
Old 09-17-2008
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?
# 2  
Old 09-17-2008
Create the file as a regular user, then move it on top of the old conf using sudo.
# 3  
Old 09-17-2008
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?
# 4  
Old 09-17-2008
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!
# 5  
Old 09-17-2008
Quote:
Originally Posted by Bilge
[...]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'

# 6  
Old 09-18-2008
Quote:
Originally Posted by radoulov
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 ;)
# 7  
Old 09-18-2008
Quote:
Originally Posted by Bilge
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 Smilie

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop output redirection

Hi All, i have below for loop of which i am trying to redirect output in a file: for i in `/usr/sbin/ifconfig -a | awk '/flags/ {print $1}' | grep -v lo | sed 's/://g'` do ifconfig $i dhcp status done >> /tmp/logfile but instead the output is appearing as stdout on screen rather than... (12 Replies)
Discussion started by: omkar.jadhav
12 Replies

2. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

3. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

4. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

5. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

6. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

7. Shell Programming and Scripting

redirection and output

I'm redirecting the output of a command to a logfile, however, if the user is on a terminal I would also like the output to be displayed on the screen. tar tvf some_tarfile >Logfile if the user is on a term then have the output to the Logfile and also be displayed on the screen at the same... (2 Replies)
Discussion started by: nck
2 Replies

8. UNIX for Dummies Questions & Answers

Output file redirection

Suppose I have a file named a When I write cat a>a The following error message is displayed cat: a: input file is output file and my file a is truncated to zero size. Also the exit status of the last command is 1 Can someone tell me what actually happens when I do so? (1 Reply)
Discussion started by: aagajaba
1 Replies

9. Shell Programming and Scripting

Redirection of output (for logging)

Hi, Currently I'm working on a lenghty script so I figured it would be useful to create a logfile so that output that is displayed on the users screen is also stored in the log file for later reference...... kinda like the whole point of a log file! Anyway, I was just wondering if there was an... (3 Replies)
Discussion started by: _Spare_Ribs_
3 Replies

10. Shell Programming and Scripting

Standard output and redirection

Hello, Is is possible to redirect stdout to a file as well as to the console/screen or display in ksh. any thoughts suggestions/input is appreciated. Thanks. (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question