is it possible, to change a file in /etc with sed -i that I am not the owner of? However, I do have write permissions to that file.

Or as an alternative, how do I change a line in a file in /etc without having su-priviliges from within a script.

Thanks a lot

2 Answers 2


It's very likely that sed -i will create a brand new file then replace the original with it. That means you would have to have write access to the directory /etc, not just the file.

None of the tools that create a new file will work without that power, and that is rightly so: /etc is a very important directory and you probably shouldn't be playing there without permission :-)

One solution is to put your file somewhere where you can modify it, then just create a soft link to it from /etc. something like:

ln -s /my_directory/my_file /etc/my_file

You'll need root powers to create the link of course. The soft link will prevent the linkage from disappearing should you do a new-file/rename operation, since the soft link is simply a file containing the target path (which, unlike the inode, won't change with sed -i).

And this limits their power to changing that file and that file only. No possibility for security holes by opening up the permissions on /etc.

  • You are absolutely right. However I do not want to give the script-executing user +w on /etc
    – Mo.
    Oct 29, 2009 at 14:14
  • Oh ha... I do like the idea of the symlink! Thanks a lot!
    – Mo.
    Oct 29, 2009 at 14:16
  • 1
    If you go the symlink route, make sure you lock down "my_directory" so somebody else can't replace the file. There is a reason why /etc/ is locked down as hard as it is, and you subvert that at your own risk. Oct 29, 2009 at 14:46
  • Note that you do not need any special privileges to create a symlink in, for example, /tmp that points to a file in /etc (so the "You'll need root powers to create the link" comment is bogus). The rest is OK. Oct 29, 2009 at 22:21
  • @JonathanL, it's not bogus. The method is to create the symlink in /etc pointing to somewhere else (that requires write access to /etc), not the other way around as you seem to have misunderstood.
    – user12022
    Oct 30, 2009 at 13:04

Instead of using "sed -i", you could try

sed ... > /tmp/foo && cat /tmp/foo > /etc/file

That way you're doing the actual changing of the file into a file in /tmp, and then writing that result to the file that you have write access but not replace access.

  • That's the way I go for now. This does not feel very "elegant" to me, but it works...
    – Mo.
    Oct 29, 2009 at 14:15
  • Use cp instead of cat, I suggest - it is a more direct expression of what you are doing. Oct 29, 2009 at 22:18
  • @JonathanLeffler No, it looks like it needs to be cat if you don't have write permission on the directory.
    – Tom Wadley
    Jan 8, 2016 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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