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
Related videos on Youtube
Author by
8k_of_power
Updated on September 17, 2022Comments
-
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 over 13 yearsOr get persistent su-rights with just
su
. -
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 over 13 yearssorry, I forgot to mention that this is in a script so I can't do sudo to run it.
-
8k_of_power over 13 yearsIt still gives me "Permission denied".
-
MOnsDaR over 13 yearsIf its in a script you could start the script with sudo as far as I know:
# sudo ./myScript
-
8k_of_power over 13 yearsThanks for the description of the problem. But how do I solve it?
-
Benoit over 13 yearsI have edited the answer. use sudo tee rather.
-
8k_of_power over 13 yearsBut since it's a script I cannot use sudo -s inside the script cause the lines after it won't run.