First, directory Permissions
Before learning the file permissions rwx, for the file can read the contents of the file (r), modify the file contents (w), the file as a script or application execution (x). What about the catalog?
A directory in Linux is also a file that differs from regular files:
- R Permission: Indicates that you can browse the contents of the directory, that is, LS allows you to view the contents of the directory.
- W Permissions: Indicates that you can add files and delete files in the directory to write permissions.
- X permissions: Because the directory is not related to execution, but in order to unify we also call execute permission. tables can be searched for files in this directory and can be entered in the directory.
Without the X permission, the Old MO test found: Into the directory, LS directory can see the file name in the directory, but the properties of the file (such as time, permissions, etc.) can not be read, that is, unable to find, the equivalent of nothing to do, so the directory must have X permission to use.
(i) new directory default permissions
When we create a new directory, we find that the permissions of the directory are rwxrwxr-x, which means:
- Other people have permission to read content in the directory
- Other people have permission to search and access the directory.
- Only file owners and group owners have permission to modify and delete content in the directory.
(ii) Permissions for the home directory
I use the CentOS test, create a user after the permission is rwx------, this means that the home directory is protected, others can not be viewed and modified, even if the subdirectory permissions in this home directory is rwxrwxrwx, outside the user is not accessible, the equivalent of the door closed.
In Linux, folders can be created only in the home directory and the/tmp directory, except in other locations, unless the administrator assigns a directory to the user elsewhere.
Ii. octal notation for file permissions
RWX can be expressed in octal numbers,
Each bit rwx corresponds to a number, and these numbers represent the rwx, and if it is empty-it is represented by 0.
Analysis: Rwxr-xr-x
rwx = 4 + 2 + 1 = 7r-x = 4 + 0 + 1 = 5r-x = 4 + 0 + 1 = 5
Final: Rwxr-xr-x can be represented as 755
If we want to set the file's permission to Rwxr-xr-x, we can write it directly:
chmod u=rwx,g=rx,o=rx filechmod 755 file
We found the second way is convenient, and for chmod o-x file This way, literally the user does not know the entire file permissions, and through the number can parse the file permissions.
Analysis: 644 of file permissions
6 = 4 + 2 + 0 = rw-4 = 4 + 0 + 0 = r--4 = 4 + 0 + 0 = r--
So the 644 privilege is rw-r--r--.
Third, control the default permissions
When we create a new file or directory there will be a default permission, the default permissions for the file is 664 (rw-rw-r--), the default permissions of the directory is 775 (rwxrwxr-x).
Default permissions: Read and Write permissions for the file, others only. For the directory to have read and write permissions, others can only read and search files, not in the folder to create and delete files.
The default permissions for files and folders are determined by Umask, and the value of Umask determines the default permissions when creating files and folders. The default maximum permissions for the Linux kernel creation file is 666 (rw-rw-rw-), the default maximum permission to create the directory is 777 (RWXRWXRWX), and then the default maximum permissions minus umask to get the final default permissions.
- The final file permission is: 666 minus the value of umask
- The final directory permission is: 777 minus the value of umask
Note that the subtraction here has its own arithmetic rules. The direct subtraction for the directory is different for the file:
- The value of Umask is 022 (all is even), then 666-022 = 644
- The value of the umask is 123 (some bits are odd), then 666-123=644, it should be: 543, but the odd digit minus 1, so the result is: 644
Iv. Modification of Umask
#显示当前的umask值umask #设置umask的值umask 022
Example: I want to create a file, get the default permissions are: RW-RW----
Because the file default maximum permissions is 666, and 6 is rw-permissions, so umask the first two bits is 00, the last one to remove all the permissions, directly minus 6 is OK, so the value of umask should be: 006
Setting the value of Umask is also affected by the creation of the directory, because the maximum default permission of the directory is 777, so the other person's permissions become 1, that is--x. Of course I do not want to do this, my directory does not want others to see, so should set the umask to 7, so that the files and directories other people are not available.
The value of the Umask setting disappears when you log out, so we should add it to ~/.BASHRC. This file is called when the user logs in, and the value of Umask is written to this file, and the user is set to Umask as soon as they log in.
echo "umask 007" >> ~/.bashrc
Finally, the system default Umask value is 022
Linux Basics-12