sudo permission denied

8,183

Solution 1

The redirection to a file is handled by bash. It does therefore not inherit permissions granted by sudo.

Use sudo tee for writing to a file as root.

Try this:

cat | sed -e 's,%,$,g' | sudo tee /etc/init.d/dropbox << EOF
  echo "Hello World"
EOF

Notice that $, inside double quotes might be interpreted.

Solution 2

You could grant yourself persistent su-rights with
# sudo -s

then your command (do not need to sudo anymore) and exit with
# exit

EDIT:
I assume you're asking Ubuntu-related because your question is tagged with that. In other distribution like Suse you'll have the ability to use
# su
instead of # sudo -s

Share:
8,183

Related videos on Youtube

8k_of_power
Author by

8k_of_power

Updated on September 17, 2022

Comments

  • 8k_of_power
    8k_of_power over 1 year

    I ran this code:

    sudo cat <<EOF | sudo sed -e "s,%,$,g" >/etc/init.d/dropbox
      echo "Hello World"
    EOF
    

    But even though, I get "permission denied", cause you have to be root to make changes in the /etc/init.d directory. And somehow, my command above doesn't work.

    Is there a way to solve this?

  • Chetan
    Chetan over 13 years
    Or get persistent su-rights with just su.
  • MOnsDaR
    MOnsDaR over 13 years
    @Chetan: This does not work in Ubuntu. (The question is tagged with Ubuntu, so I expect that it was in this consense)
  • 8k_of_power
    8k_of_power over 13 years
    sorry, I forgot to mention that this is in a script so I can't do sudo to run it.
  • 8k_of_power
    8k_of_power over 13 years
    It still gives me "Permission denied".
  • MOnsDaR
    MOnsDaR over 13 years
    If its in a script you could start the script with sudo as far as I know: # sudo ./myScript
  • 8k_of_power
    8k_of_power over 13 years
    Thanks for the description of the problem. But how do I solve it?
  • Benoit
    Benoit over 13 years
    I have edited the answer. use sudo tee rather.
  • 8k_of_power
    8k_of_power over 13 years
    But since it's a script I cannot use sudo -s inside the script cause the lines after it won't run.