5

I am on Ubuntu 10.04.02. I thought that when using sudo that would execute commands as root. But here as root I cannot write to my own file. Is this correct?

david@ubuntu:/var/www/system/paydaydebt/log$ ls -l
total 16
-rw-r--r-- 1 root root   32 2011-08-27 01:00 cron_daily.log
-rw-r--r-- 1 root root 5082 2011-08-27 20:45 cron_email.log
-rw-r--r-- 1 root root  429 2011-08-27 20:00 cron_hourly.log
david@ubuntu:/var/www/system/paydaydebt/log$ sudo date >> cron_email.log
-bash: cron_email.log: Permission denied

david@ubuntu:/var/www/system/paydaydebt/log$ sudo chmod 664 cron_email.log
david@ubuntu:/var/www/system/paydaydebt/log$ ls -l
total 16
-rw-r--r-- 1 root root   32 2011-08-27 01:00 cron_daily.log
-rw-rw-r-- 1 root root 5082 2011-08-27 20:45 cron_email.log
-rw-r--r-- 1 root root  429 2011-08-27 20:00 cron_hourly.log
david@ubuntu:/var/www/system/paydaydebt/log$ sudo date >> cron_email.log
-bash: cron_email.log: Permission denied

david@ubuntu:/var/www/system/paydaydebt/log$ sudo chmod 666 cron_email.log
david@ubuntu:/var/www/system/paydaydebt/log$ ls -l
total 16
-rw-r--r-- 1 root root   32 2011-08-27 01:00 cron_daily.log
-rw-rw-rw- 1 root root 5082 2011-08-27 20:45 cron_email.log
-rw-r--r-- 1 root root  429 2011-08-27 20:00 cron_hourly.log
david@ubuntu:/var/www/system/paydaydebt/log$ sudo date >> cron_email.log

david@ubuntu:/var/www/system/paydaydebt/log$ sudo chmod 644 cron_email.log
david@ubuntu:/var/www/system/paydaydebt/log$ ls -l
total 16
-rw-r--r-- 1 root root   32 2011-08-27 01:00 cron_daily.log
-rw-r--r-- 1 root root 5111 2011-08-27 20:47 cron_email.log
-rw-r--r-- 1 root root  429 2011-08-27 20:00 cron_hourly.log
david@ubuntu:/var/www/system/paydaydebt/log$ sudo date >> cron_email.log
-bash: cron_email.log: Permission denied
david@ubuntu:/var/www/system/paydaydebt/log$




enter image description here

davidjhp
  • 700

2 Answers2

13

sudo only applies to the command run; your append >> occurs as the current user.

Shane Madden
  • 115,228
1

Best solution: run "sudo program|sudo tee -a output_file >/dev/null"

tee writes to file and stdout, >/dev/null redirects the output to /dev/null (just discards it) and the -a option tells tee to append to the file (as >> does) instead of overwriting the file (as > does).

run a simple command with sudo first, so the password gets cached, because otherwise you get two password-prompts on the same line and need to answer both before being able to continue.

allo
  • 1,713