Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I was working on developing my command line skills last night and ran into an issue where when I used sudo, I got an error message that said 'permission denied'. However when I used 'sudo su' and became root the command worked.

Why didn't sudo work in the first instance?

It was:

$ sudo cat > /var/www/info. php
<?php
phpinfo( ) ;
? >
^D

From the Linux Bible 2010 edition in the section on setting up a LAMP server.

share|improve this question
    
If you don't give an example it's kind of hard to resolve the problem. –  Source Lab Sep 24 '10 at 19:26
    
Consider sudo -s over sudo su. –  Nerdling Sep 25 '10 at 23:34
add comment

4 Answers

up vote 17 down vote accepted

The <, > and >> are used for input / output redirection for commands - which is a feature provided by the shell (e.g., bash). So if you type a command like sudo cat > /var/www/info.php then the shell that receives this as input tries to open the file /var/www/info.php and provides that file as the standard output to the sudo command. The sudo command is not even aware whether its output is going to a console or redirected to a file, because this is taken care of by the shell that invokes it.

If the shell you typed your command into is your login shell or another shell running in a terminal with your user id, then it has same privileges as your user id - not those of root.

So in your case, whereas the cat command is executed as root, the copying of its output to /var/www/info.php is attempted by the shell running as a normal user, which, as expected, fails.

A workaround for such situations is to use the tee command :

sudo tee /var/www/info.php

That will have the intended effect of putting all the text entered at the console upto ^D into the file specified as parameter.

One perhaps undersirable side-effect is that tee will also echo the output to the stdout, so after you type each line and press enter tee will output a copy of it back. To avoid this you can use the following variant.

sudo tee /var/www/info.php > /dev/null

Details about tee can be had via info tee at a terminal.

share|improve this answer
2  
+1 for tee. That's the correct way to do this. –  Dennis Williamson Sep 24 '10 at 20:55
1  
Thank you. I'll have some fun teeing off. ;~) –  wdypdx22 Sep 24 '10 at 21:25
add comment

sudo does not work for commands that need permissions to write a file, such as:

sudo echo "vm.swappines = 100" >> /etc/sysctl.conf

It explains in the sudo man-page.

share|improve this answer
4  
Mentioning that you can "sudo -i" or "sudo -s" to get a root prompt would make this answer even better! –  Jorge Castro Sep 24 '10 at 19:23
    
There is no pipe in the example command; there is a redirection. Also, "sometimes does not work" seems to convey flaky behavior by sudo. As noted in koushik's answer, the behavior is quite predictable, consistent, and "correct" for the unix permission model. –  msw Sep 25 '10 at 2:07
add comment

The problem is that the "> foo.txt" part is interpreted and executed by your command interpreter (shell) long before the sudo command is being run. The sudo command has no idea that you want to redirect its output to a file.

Your command interpreter does not have root authority (but the sudo command will eventually later) so it cannot redirect the output to foo.txt.

share|improve this answer
add comment

Another way to solve this is to start a subshell with sudo and execute that command in that subshell:

$ sudo bash -c "cat > /var/www/info.php"
<?php
phpinfo( ) ;
? >
^D

here, the command that "has sudo" is bash, and anything executed in it has root permissions.

$2c, *-pike

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.