Take the 2-minute tour ×
Server Fault is a question and answer site for 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

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

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.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.5.1.2550\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Server Fault works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscri"], "copyright__copyright__copyright": ["|improve this answer\n\nInstead of using \"sed -i\", you could try\n\nsed ... > /tmp/foo && cat /tmp/foo > /etc/file\n\nThat 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.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}