Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 16 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
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
This is not the right answer. This is a right answer. Akira's got a better one, for the logic provided in that answer. – TOOGAM Jun 16 at 17:42

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

share|improve this answer
right, by using the generalized syntax of "command | sudo tee -a output.txt" (the -a appends instead of overwrites), the "command" doesn't need to be elevated. Only the process of writing to the output file ends up being privileged. So you provide less privileges for the "command", which often has more potential for danger/problems when the specified (customizable) "command" is something more elaborate than "echo". – TOOGAM Jun 16 at 17:45

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

echo ‘JAVA_HOME=/usr/lib/jvm/java-6-sun’ >> /etc/profile
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

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

sudo dd of=/etc/profile <<< END
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

Use ex-way:

sudo ex +'$put =\"FOO\"' -cwq /etc/profile

and replace FOO with your variable to append.

In some systems (such as OS X), the /etc/profile file has 444 permissions, so if you still getting permission denied, check and correct the permission first:

sudo chmod 644 /etc/profile

then try again.

share|improve this answer

I usually use shell HERE document with sudo tee -a. Something along the lines of:

sudo tee -a /etc/profile.d/ << 'EOF'
# configures JAVA
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .