Is this GNU only?
No. This rm
behavior dates back around 40 years and has been standardized.
What's the rationale for this behavior?
From the 1st edition man page for rm:
Removal of a file requires write
permission in its directory, but neither read nor write
permission on the file itself.
BUGS
rm probably should ask whether a read--only file is really to be removed.
The implication, I think, is that if a user wanted to protect a file against accidental writing by changing its mode to be read-only, then it was worth making an attempt to protect it from accidental deletion.
The TUHS archive is missing the 2nd edition man pages and rm source, but the 3rd edition man page for rm shows this had been addressed:
If there is no write permission to a
file designated to be removed,
rm will print the file name, its mode and then
read a line from the standard input.
If the line begins with 'y', the file is removed,
otherwise it is not.
The optional argument -f prevents the above interaction.
Unix and most of its commands eventually got standardized, and that's why rm behaves this way today.
When using rm as root this does not happen on my system. Is this normal behavior?
Not at first, but it is normal behavior now.
The earliest source code I can find is from V5:
if(getuid() == buf->uid)
b = 0200; else
b = 2;
if((buf->mode & b) == 0) {
printf("%s: %o mode ", arg, buf->mode);
i = b = getchar();
i = b;
while(b != '\n' && b != '\0')
b = getchar();
if(i != 'y')
return;
}
which shows the writability check looks at the write permission by owner flag if the user running rm owns the file, otherwise the write permission by others flag.
In V7, rm was changed to use the newly-added access system call:
if (access(arg, 02)<0) {
printf("rm: %s %o mode ", arg, buf.st_mode&0777);
if(!yes())
return;
}
Since access
considers root to have write access to any file (unless it's on a read-only filesystem), rm
will typically not ask for confirmation when it's run by root.
rm
. – Ryan Apr 09 '16 at 07:23