2

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.

Saqlain
  • 171

1 Answers1

5

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.

Hennes
  • 65,804
  • 7
  • 115
  • 169