Wednesday, August 7, 2013

Re: vim vs nano on editing system files

* Dahong Tang <tang.dahong@gmail.com> [130808 04:59]:
> > $ FILE=/tmp/testfile
> > $ sudo touch $FILE
> > $ sudo chmod 400 $FILE
> Thanks Sven. Your explanation makes sense if I used the command vim
> $FILE, but I used sudo vim $FILE. Why does vim even bother to set the
> file to readonly when I used sudo? That is the part that I don't
> understand.

Because you told it so: If a file has no (w)rite permission (as in 400,
which is r--), vim would open it as read-only, so a
:w
fails. But now that you are root (sudo), no-one prevents you from
"changing" the flags, writing, and "changing flags back". I quoted this,
since I assume that the Linux kernel just allows root to write files
that she/he has no obvious write permissions for.

Short: if vim sees a file without w-permission, it opens it as readonly;
but root may overwrite it nevertheless.

Greetings,
mo

No comments: