sudo unable to write to /etc/profile
Problem
I am following the instructions on this site for Grails installation, but am not able to write to /etc/profile like it instructs. Instead, I get this error: Why am I not able to write to the file even using sudo?
Error Output
sudo echo ‘JAVA_HOME=/usr/lib/jvm/java-6-sun’ >> /etc/profile
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Correctly Update /etc/profile with Sudo
The command 'sudo echo ... >> /etc/profile' fails because the redirection '>>' is executed in the context of the current shell, not with elevated privileges. Therefore, the user does not have permission to write to /etc/profile, even when using sudo.
Awaiting Verification
Be the first to verify this fix
- 1
Use Sudo with a Shell Command
Instead of trying to redirect output with sudo, use a shell command to execute the echo with elevated privileges.
bashecho 'JAVA_HOME=/usr/lib/jvm/java-6-sun' | sudo tee -a /etc/profile - 2
Verify the Change
Check that the JAVA_HOME variable has been added to /etc/profile by viewing the file.
bashcat /etc/profile | grep JAVA_HOME - 3
Reload the Profile
To apply the changes made to /etc/profile, either log out and log back in or source the file.
bashsource /etc/profile - 4
Confirm JAVA_HOME is Set
Check if the JAVA_HOME environment variable is set correctly in the current session.
bashecho $JAVA_HOME
Validation
Ensure that the output of 'echo $JAVA_HOME' displays '/usr/lib/jvm/java-6-sun'. If it does, the fix has worked.
Sign in to verify this fix