FG

sudo unable to write to /etc/profile

Freshabout 21 hours ago
Mar 15, 20267450 views
Confidence Score0%
0%

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

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Correctly Update /etc/profile with Sudo

Medium Risk

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. 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.

    bash
    echo 'JAVA_HOME=/usr/lib/jvm/java-6-sun' | sudo tee -a /etc/profile
  2. 2

    Verify the Change

    Check that the JAVA_HOME variable has been added to /etc/profile by viewing the file.

    bash
    cat /etc/profile | grep JAVA_HOME
  3. 3

    Reload the Profile

    To apply the changes made to /etc/profile, either log out and log back in or source the file.

    bash
    source /etc/profile
  4. 4

    Confirm JAVA_HOME is Set

    Check if the JAVA_HOME environment variable is set correctly in the current session.

    bash
    echo $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

Environment