Can't edit '/etc/network/interfaces' file through bash script in Ubuntu

8,107

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:
8,107

Related videos on Youtube

Saqlain
Author by

Saqlain

I am working as Technical Lead in Mentor Graphics Corporation and also got contribution in opensource community. If you want to know more about me, have a look at my linkedin profile pk.linkedin.com/pub/saqlain-abbas/7/289/b98/

Updated on September 18, 2022

Comments

  • Saqlain
    Saqlain over 1 year

    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.