Can someone help me with the logic here ?
I thought I could use sudo for everything.
the logic is simple, you gave root permissions to the echo not to the redirect, the redirect (>) is session specific, the root privileges are not passed through it
to achieve what you want you have to pipeline the command
first echo something then use a sudo command to overwrite the file
[ali@linux chuck]$ cat test
hello dear johnny
[ali@linux chuck]$ sudo echo "" > test
bash: test: Permission denied
[ali@linux chuck]$ echo -n | sudo tee test
[ali@linux chuck]$ cat test
[ali@linux chuck]$
here's more proof that it's session specific
the > doesn't care what is before it and it's good because it doesn't
you don't want your log files to be owned by root
it's like this
(sudo echo hi) > test
> is owned by user that's why the output file is owned by user, you were just doing it wrong
you should use > to redirect what the user sees, here's a good example
[ali@linux chuck]$ cat test
[ali@linux chuck]$ echo "why can i still see the output?" | sudo tee test
why can i still see the output?
[ali@linux chuck]$ cat test
why can i still see the output?
[ali@linux chuck]$ echo "i'd rather not see it, thank you" | sudo tee test > /dev/null
[ali@linux chuck]$ cat test
i'd rather not see it, thank you
[ali@linux chuck]$