0

I have a file that is owned and written by root user.

I have another user called systemuser that we have given sudo privileges for every action.

I'm able to perform every action by systemuser just like how i do for root user.

But I'm trying to null a file owned and written by root and I get permission denied error.

 >api-server-out-0.log
-bash: api-server-out-0.log: Permission denied
$ sudo >api-server-out-0.log
-bash: api-server-out-0.log: Permission denied

Note: a process running with root user is currently writing to this log file.

Can you please suggest how can I using `systemuser ?

  • Your code looks messed up. Edit question to show what you tried. – ctrl-alt-delor Oct 2 '20 at 18:00
5
sudo > api-server-out-0.log

won't work. What it actually does, it starts sudo without any parameters and tries to redirect its output under your user account to the specified file which of course will not work since the file is owned by root.

What you really want to do is something like

sudo dd if=/dev/null of=api-server-out-0.log
# or
sudo truncate -s 0 api-server-out-0.log
# or
sudo sh -c 'echo -n "" > api-server-out-0.log'
# or
sudo sh -c ': > api-server-out-0.log'
  • What it dose is: … do re-directions, … execute commands (in this sudo something). So the root privilege arrives too late. – ctrl-alt-delor Oct 2 '20 at 17:59
  • echo -n "" outputs -n<SPC><NL> in UNIX-compliant implementations of echo. Standard commands that produce no output and are generally builtin include true, eval, test 1, [ 1 ], :, printf ''... In sh, you can also do > file alone. – Stéphane Chazelas Oct 2 '20 at 20:16
1

Artem S. Tashkinov has described the "why" and offered some of the many options. Here's another way to go about it:

sudo tee api-server-out-0.log </dev/null

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