1

I am executing the following command: chmod 000 x.txt

but as owner I still can rename it ! why ?

and how can I prevent all people from renaming that file ?

4
  • 2
    For that you need to set permissions on the directory containing the file.
    – muru
    Oct 23, 2014 at 12:48
  • I have the same result when I try that command over a folder (i.e I can delete the folder after: chmod 000 FolderName )
    – user341582
    Oct 23, 2014 at 12:51
  • 1
    And for that you need to set permissions on the directory containing that directory.
    – muru
    Oct 23, 2014 at 12:54
  • my folder is in HOME directory, so I should change the permission for Home ?! I want only to affect one folder/file, how ?!
    – user341582
    Oct 23, 2014 at 12:58

2 Answers 2

2

Directories are special files containing a list of named entries. Each entry references another file object, which in turn contains the locations of the file content on disk. When you create, move, or delete a “file” (and remember that directories are files too) you're actually creating, renaming, or deleting entries in a directory list – you're changing the directory the entry is part of. Since you need write permissions to an object to change it, you need write permissions to a directory to create, move, or delete entries within it.

Example

Let's assume you have a file x in directory a. To rename x a user needs write access to a. To revoke write access from anyone, who isn't the owner of a do:

chmod go-w a

This revokes (-) the “write” permission (w) for the classes “owner group“ (g) and “other“ (o).

0

As correctly answered by David Foerster, the rights to manipulate files and directories depends on the permission on the containing directory.

Said that, there is a (dirty?) trick to avoid that a directory could be deleted even if you have right on the containing one: put in it a (possibly hidden) file, and make that and the directory no-writable. Look at this example:

[romano:~] mkdir tmp/test; cd tmp/test
[romano:~/tmp/test] % mkdir subdir_e subdir_f
[romano:~/tmp/test] % touch subdir_f/.hiddenfile

I created two subdirs, one empty and the other with an hidden file.

[romano:~/tmp/test] % chmod 555 subdir_f/.hiddenfile subdir_f subdir_e
[romano:~/tmp/test] % sudo chown root subdir_f/.hiddenfile subdir_f subdir_e

I make this directory and the hidden file no-writable, by changing permission and giving ownership to root (otherwise I can simply change permission back). Let's see the status:

[romano:~/tmp/test] % ls -la  
total 16
drwxrwxr-x 4 romano romano 4096 oct 23 16:37 .
drwxr-xr-x 6 romano romano 4096 oct 23 16:31 ..
dr-xr-xr-x 2 root   romano 4096 oct 23 16:37 subdir_e
dr-xr-xr-x 2 root   romano 4096 oct 23 16:37 subdir_f

Notice that I own and have write rights to ., which is my current directory, so if I do:

[romano:~/tmp/test] % rmdir subdir_e

it succeeds, because I can modify . as I like. But if I try the same with the non-empty subdir:

[romano:~/tmp/test] % rmdir subdir_f
rmdir: failed to remove ‘subdir_f’: Directory not empty
[romano:~/tmp/test] 1 % rm -rf subdir_f
rm: cannot remove ‘subdir_f/.hiddenfile’: Permission denied
[romano:~/tmp/test] 1 % chown romano subdir_f
chown: changing ownership of ‘subdir_f’: Operation not permitted

...I need root privilege to remove it now.

You must log in to answer this question.