Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicate:
sudo permission denied

I got need to edit /etc/network/interfaces through bash script, I am doing something like below...

sudo echo something > /etc/network/interfaces

When this lines is executed I get error mentioned below: bash: /etc/network/interfaces: Permission denied

But if I edit same file with some editor it work flawlessly...i.e.

sudo nano /etc/network/interfaces

above let me edit this file.

share|improve this question

marked as duplicate by Hennes, Ƭᴇcʜιᴇ007, slhck Dec 29 '12 at 20:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

This question is already answered somewhere on this site, though I failed to find it.

But basically it comes down to this:

sudo nano /etc/network/interfaces starts nano /etc/network/interfaces as uid 0. Since nano runs at uid 0 and has permission to write to /etc/network/interfaces.

sudo echo something > /etc/network/interfaces starts echo something as uid 0. That part succeeds. The output is redirected with the > to /etc/network/interfaces`. However this redirect is done with your own account. And that one has no permission to write to that location.


Try this instead:
sudo bash -c "echo something > /etc/network/interfaces"

That should run bash as uid 0, and let bash execute a the whole command as uid 0.

Or simply su - to root. Running all the time as root is not recommended, however there are times when you simply want to have sufficient rights without typing a sudo-command in front of every single line. Just do not use root as main user. Using it to install and configure a system is fine.

share|improve this answer
    
Thanks Hennes! It worked :-) –  Saqlain Dec 31 '12 at 6:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.