Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
I think this is a question for superuser.com. –  Ricardo Polo Aug 28 '11 at 1:06

2 Answers 2

up vote 12 down vote accepted

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

share|improve this answer
3  
so, as a workaround you could do something like: sudo sh -c 'date >> cron_email.log' –  Chad Feller Aug 28 '11 at 1:07
1  
That is probably the best option. At that point, though, it would be simpler just to sudo su - :) –  Rilindo Aug 28 '11 at 1:11
1  
"sudo su" is useless use of su. just do "sudo -s" –  allo Sep 2 '11 at 22:34

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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