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

When trying to push a couple lines to the end of a file, I get a permission issue. I understand why I'm getting the error, but I can't think of a way to resolve it. Any help would be appreciated.

sudo cat > /etc/php5/apache2/php.ini << EOF
    # extensions
share|improve this question
ls -al /etc/php5/apache2/php.ini, what's the permission settings of the file? – Jin Sep 26 '11 at 18:02
up vote 7 down vote accepted

Heredoc usage, or "appending to EOF", is not the problem.

All redirections (including >) are applied before executing the actual command. In other words, your shell first tries to open /etc/php5/apache2/php.ini for writing using your account, then runs a completely useless sudo cat.

One way to get around this:

sudo bash -c "cat >> /etc/php5/apache2/php.ini" <<EOF

(You can run an interactive shell via sudo -s, or use dd or tee for writing to the file.)

On a related note, using > will overwrite the old php.ini. Use >> to append.

share|improve this answer
Thanks grawity. I'm having issues running the above for hidden files. I get this kind of response: – onassar Sep 26 '11 at 18:58
@onassar: If you open the heredoc with "EOF", you must close it with exactly the same "EOF". Not with ".EOF" as you are currently doing. – grawity Sep 26 '11 at 19:25
this is the code I'm pasting in: it's actually just being rendered improperly by my terminal. – onassar Sep 26 '11 at 19:27
it seems the tabs in my code (\t character) was throwing things off. replacing those with 4 spaces did the trick. thx! – onassar Sep 26 '11 at 20:14

sudo su and then you have a proper shell as root. Run the command in there, without sudo prefix. Afterwards, exit to return from the root shell.

share|improve this answer

You must log in to answer this question.

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