Linux Permission on Dir and File
2015-10-08 07:19 三犬风 阅读(228) 评论(0) 收藏 举报Directories
Directories (and nearly everything else) in Unix are just files. They contain little information, just the name of a file and its inode number. System calls that read or modify directories work similarly as for ordinary files. However the permission bits on directories control access to additional system calls (such as chdir) then just the few used for regular files (such as read, write, and exec).
To read the names of files in a directory using read(), or using opendir() or readdir() system calls, requires read permission. Note the ls command needs this permission to access the names of files in a directory. Directories also contain inode numbers for each file, but read permission does not grant access to these.
To modify the contents of a directory requires write permission. If you have write permission on some directory, you can add files to it, rename and delete files from it.
Deleting, linking, and renaming files require execute permission too, as discussed below). This is because such operations also require access to a file's inode in addition to the file's name. While read permission will allow access to the name of a file in a directory, execute permission is needed to access the inodes of files in that directory.
Note you don't have to be the owner of a file or have write permission on it to rename or delete it! You only need write permission on the directory that contains the file.
Execute Permission for Directories
The chdir() system call is one of many that requires execute permission on a directory. Of course a directory isn't really a program that you can run even if it has execute permission. The execute bit is reused rather than waste space with additional permission bits.
Besides controlling a user's ability to cd into some directory, the execute permission is required on a directory to use the stat() system call on files within that directory. The stat() system called is used to access the information in a file's inode, and must be done before you can open or delete (via the unlink() system call) that file. (See Note.)
Because of its role in file access the execute bit on a directory is sometimes called search permission. For example, to read a file foo/bar, you must have read permission for the file itself, but before the file can be accessed you must first search the directory foo for the inode of file bar. This requires search (
) permission on the directory xfoo. (Note you don't need read permission on the directory in this case! You only need read permission on a directory to list its contents.)
ref:
http://blog.itpub.net/131953/viewspace-1050491/
https://wpollock.com/AUnix1/FilePermissions.htm
浙公网安备 33010602011771号