0
debian8@hwy:~$ sudo cat /etc/sudoers |grep debian8
debian8  ALL=(ALL:ALL) NOPASSWD:ALL

It means debian8 is a permitted user to execute a command as the superuser.
I want to write something into a log file.

debian8@hwy:~$ trafficlog="/var/log/traffic.log"
debian8@hwy:~$ sudo echo -n `date "+%Y-%m-%d %H:%M:%S"` >> $trafficlog
bash: /var/log/traffic.log: Permission denied

debian8 has root permission,why can't write date record into the trarric log?

Kusalananda
  • 333,661
showkey
  • 323
  • Shadur tells me why the command can't do,Shadur's answer is better than redirecting-stdout-to-a-file-you-dont-have-write-permission-on – showkey Feb 04 '17 at 09:14

1 Answers1

4

Because the sudo command ends where the command does.

Using implied parentheses, this is what you're doing (and let me add that you're doing it in a particularly convoluted way, but that aside)

(sudo echo -n) (date "+%Y-%m-%d %H:%M:%S") >> $trafficlog

As you can see, you execute the echo command as root, but the redirection happens as debian8.

What would work would be

echo -n date "+%Y-%m-%d %H:%M:%S" | sudo tee --append $trafficlog