-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?

1

1 Answer 1

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"
2
  • 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, 2017 at 7:20
  • @is the English way Thanks i will improve my answer.
    – Slipeer
    Jan 18, 2017 at 7:23

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