Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
You are absolutely right. However I do not want to give the script-executing user +w on /etc –  Mo. Oct 29 '09 at 14:14
    
Oh ha... I do like the idea of the symlink! Thanks a lot! –  Mo. Oct 29 '09 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. –  Paul Tomblin Oct 29 '09 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. –  Jonathan Leffler Oct 29 '09 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. –  paxdiablo Oct 30 '09 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.

share|improve this answer
    
That's the way I go for now. This does not feel very "elegant" to me, but it works... –  Mo. Oct 29 '09 at 14:15
    
Use cp instead of cat, I suggest - it is a more direct expression of what you are doing. –  Jonathan Leffler Oct 29 '09 at 22:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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