-1

I thought that running a command with sudo was identical to logging in as the root user and executing the command. However I have found a few commands that do not work with sudo but do work for su.

For instance,

sudo > mail.log
bash: mail.log: Permission denied

but actually typing su and executing the same command works.

Why is this?

Startec
  • 107
  • 3

1 Answers1

2

Look at what you're doing with this command.

sudo > mail.log

> in command-line before redirect stdout try to truncate mail.log file. This operation is performed by a regular user (before your sudo without arguments starts). Probably a regular user does not have write access to mail.log file.

Everything works as it should.

If you need to sudo to execute the command and write it to a file type, use the following command:

sudo "some_command with operands > mail.log"
Slipeer
  • 3,255
  • 2
  • 18
  • 32
  • IIRC the redirection is attempted before sudo. This means that the original user attempts the redirection and fails (no permission). Your answer has the order of operations incorrect which makes the rest of it based on that order less than useful. Don't forget also that `>file` can be used to truncate file. – user9517 Jan 18 '17 at 07:20
  • [@is the English way](http://serverfault.com/users/9517/is-the-english-way) Thanks i will improve my answer. – Slipeer Jan 18 '17 at 07:23