TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Use sudo to redirect output to a location in which user doesn't have permission to write

Sourav Gulati
Site Owner
Posts: 83

Suppose you want to redirect output of a sudo command to  a location where you dont have write permission.

Now if you have do it in usual way , you will get permission denied error:

$ sudo echo "hello" > /etc/hello

sh: /etc/hello: Permission denied

It makes sense . Here you ran a command with sudo however file is being created with rights of current user.

This issue will be resolved as follows:

$ sudo sh -c 'echo "hello" > /etc/hello'

Here you are invoking a shell with sudo rights.

sh -c  :-  -c option provides command string to sh . It states "Read commands from the command_string operand instead of from the standard input"




--


November 28, 2012 at 4:22 AM Flag Quote & Reply

You must login to post.