Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am following the instructions on this site for Grails installation, but am not able to write to /etc/profile like it instructs.

sudo echo ‘JAVA_HOME=/usr/lib/jvm/java-6-sun’ >> /etc/profile

Instead, I get this error:

bash: /etc/profile: Permission denied

Why am I not able to write to the file even using sudo?

share|improve this question
    
Are you able to use sudo for any other commands? Which Linux Distribution are you using? Which owner, group and rights are on /etc/profile? –  brandstaetter Oct 13 '09 at 14:08
    
I am using Ubuntu 9.04 and am able to use sudo for every command I've needed to so far. Spawning a sub-shell worked. –  Matt Norris Oct 13 '09 at 14:57

5 Answers 5

up vote 14 down vote accepted

Have sudo spawn a sub-shell:

sudo sh -c "echo 'JAVA_HOME=/usr/lib/jvm/java-6-sun' >> /etc/profile"

In this example, sudo runs "sh" with the rest as arguments.

(this is shown as an example in the sudo man page)

share|improve this answer
    
+1 this is the right answer –  Pascal Thivent Oct 13 '09 at 14:30
6  
The reason is that it's your shell (running as you) that does the redirection, not sudo. Since you don't have permission to write to the file, you get the Permission denied error. What this answer does is to start a new shell that is running as root, and therefore is able to write to the file. –  Randy Orrison Oct 13 '09 at 14:48

In my opinion, the best in this case is dd:

sudo dd of=/etc/profile <<< END
JAVA_HOME=/usr/lib/jvm/java-6-sun
END
share|improve this answer

There may be a problem with the sudo here and the redirecting. Use a texteditor of your choice instead to add the line.

sudo nano /etc/profile

Or, you could try su instead

su
echo ‘JAVA_HOME=/usr/lib/jvm/java-6-sun’ >> /etc/profile
exit
share|improve this answer
    
Unfortunately, this produced the same result. –  Matt Norris Oct 13 '09 at 14:57
    
su -c 'echo "JAVA_HOME=/usr/lib/jvm/java-6-sun" >> /etc/profile' should work. since there's nothing the shell can mistakenly expand in the arg to echo, single-quote the overall command. –  quack quixote Oct 13 '09 at 15:55

use tee:

% do_something | sudo tee 'filename'

in your case thats:

% echo 'JAVA_HOME=/usr/lib/jvm/java-6-sun' | sudo tee /etc/profile

the reason to favour tee over subshells is to encapsulate the stuff which creates the content from the stuff which writes the content to a file. imagine more complex stuff than just 'echo' than the other solutions become bloatish, aside from the fact that all of the commands running in the subshell are executed with sudo-permissions.

further discussion at http://serverfault.com/questions/68541/bash-how-can-i-add-a-line-to-the-end-of-a-file-which-i-only-have-access-to-via-s/68557#68557.

share|improve this answer

This won't work you are trying to redirect (using >>) the output of sudo. What you really want to do is redirect the output of echo. I suggest you simply use your favorite editor and add that line manually to /etc/profile. This has the additional benefit that you can check whether /etc/profile already sets JAVA_HOME.

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.