in reply to Re^5: Best way to write to a file owned by root?
in thread Best way to write to a file owned by root?
I'm just writing a script under my total control on my own local machine and does not have processes available to the public.
So it has no network connection? I don't think so.
I *think* my code accomplished that because it makes a copy of the /etc/hosts file, makes the ownership and privileges changes to copy of the file and, if those are successful, only then copies the modified file back to /etc/hosts.
Replace the last step with a rename. Rename is atomar, copy is not. And yes, rename will work for an existing file:
/tmp>echo bla > one /tmp>echo blubb > two /tmp>mv two one /tmp>cat one blubb /tmp>echo bla > one /tmp>echo blubb > two /tmp>perl -e 'rename "two","one" or die $!' /tmp>cat one blubb /tmp>
Also, the file is read only only by my account so no one else can look at it.
/etc/hosts should have mode 0644, not 0400 or 0600. Even if only you work with your computer, it still uses several different user accounts to do its job. And some of those user accounts need to resolve host names, i.e. read /etc/hosts. Even if they only need to resolve localhost to ::1 or 127.0.0.1.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Best way to write to a file owned by root?
by nysus (Parson) on Mar 15, 2017 at 14:09 UTC | |
by hippo (Archbishop) on Mar 15, 2017 at 14:52 UTC | |
by nysus (Parson) on Mar 15, 2017 at 15:23 UTC | |
by afoken (Chancellor) on Mar 15, 2017 at 22:48 UTC | |
by nysus (Parson) on Mar 16, 2017 at 01:29 UTC | |
| |
by Anonymous Monk on Mar 15, 2017 at 15:46 UTC | |
by afoken (Chancellor) on Mar 15, 2017 at 22:12 UTC |