15

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.

2
  • If you don't give an example it's kind of hard to resolve the problem. Sep 24, 2010 at 19:26
  • 1
    Consider sudo -s over sudo su.
    – Jeremy L
    Sep 25, 2010 at 23:34

4 Answers 4

18

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.

0
2

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.

2
  • 4
    Mentioning that you can "sudo -i" or "sudo -s" to get a root prompt would make this answer even better! Sep 24, 2010 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, 2010 at 2:07
2

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.

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .