sudoedit root owned file in a non-root directory

Why can’t I edit files owned by root but being e.g. somewhere deep in my personal directory, it says:

sudoedit: existingFile: editing files in a writable directory is not permitted

While I have the following function defined:

function sunano {
    export SUDO_EDITOR='/usr/local/bin/nano'
    sudoedit "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97b3d7">[email protected]</a>"
}

And I edit like this:

sunano existingFile

Where the file is indeed owned by root:

ls -l existingFile

Proves that:

-rwxr-xr-x 1 root root 40 Jun 15  2015 existingFile

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The manpage says

Files located in a directory that is writable by the invoking user may not be edited unless that user is root (version 1.8.16 and higher).

If you can write to the directory containing the file, then you can edit it in practice without needing sudoedit (although you may not be able to read its current contents): you can move it out of the way and create a new file with the same name. In your particular case, you can read the file, and you should find that at least some editors will allow you to edit it (at least those which save files by writing a temporary file and renaming it into place).

The reasoning behind this feature is given in sudo bug 707: basically, allowing users to edit files in directories they can write to with sudoedit can allow them to circumvent the restrictions set up in sudoedit’s configuration (and effectively edit any file on the system).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x