Linux chmod command details
The chmod command is very important for changing the access permissions of files or directories. You can use it to control access to files or directories.
This command can be used in two ways. One is the text setting method that contains letters and operator expressions, and the other is the number setting method that contains numbers.
1. text setting method
Chmod [who] [+ |-| =] [mode] File Name?
The meaning of each option in the command is:
The operator who is one of the following letters or their combination:
U indicates "user", that is, the owner of a file or directory.
G indicates "group users", that is, all users with the same group ID as the file owner.
O indicates "Other (Others) Users ".
A Indicates "All (all) Users ". It is the default value of the system.
The operation symbol can be:
+ Add a permission.
-Cancel a permission.
= Grant the given permission and cancel all other permissions (if any ).
Set the permissions indicated by mode to any combination of the following letters:
R readable.
W writable.
X executable.
X adds the X attribute only when the target file is executable to some users or the target file is a directory.
S sets the owner or group ID of the process to the file owner during file execution. In the format of "U + S" to set the user ID of the file, and "G + S" to set the group ID.
T save the program text to the swap device.
U has the same permissions as the file owner.
G. users in the same group have the same permissions as file owners.
O has the same permissions as other users.
File Name: list of files separated by spaces to change permissions. Wildcards are supported.
Multiple permission methods can be provided in a command line, separated by commas. Example: chmod g + R, O + r example
Grant the same group and other users the permission to read the file example.
2. Number setting method
First, we must understand the meaning of the property represented by numbers: 0 indicates no permission, 1 indicates executable permission, 2 indicates writable permission, 4 indicates readable permission, and then add it. Therefore, the format of the numeric attribute should be three Octal numbers from 0 to 7. The order is (u) (g) (o ).
For example, if you want the owner of a file to have the "read/write" permission, you need to set 4 (readable) + 2 (writable) to 6 (read/write ).
The number setting method is generally in the following format:
Chmod [mode] File Name?
Example:
(1) text setting method:
Example 1: $ chmod A + x sort
That is, set the property of the file sort:
File owner (u) adds execution permission
Add execution permission for users in the same group as the file owner (g)
Other users (o) add execution Permissions
Example 2: $ chmod ug + W, o-x text
That is, set the text attribute of the file:
Add write permission to file owner (u)
Add write permission for users in the same group as the file owner (g)
Other users (o) delete execution Permissions
Example 3: $ chmod U + s a. Out
Assume that the permission for a. out after chmod is executed is (you can use the LS-l a. out command to view the permission ):
-RWS -- X 1 inin users 7192 Nov 4 14:22 A. Out
In addition, this execution file uses a text file shiyan1.c with the "-RW -------" access permission, that is, only the owner of the file has the read and write permissions.
When other users execute. when the program is out, his identity is changed to inin (because the s option is used in the CHMOD command) for the moment ), therefore, he can read the shiyan1.c file (although this file is set to be inaccessible to others), which is the function of S.
Therefore, in the entire system, especially the root itself, it is best not to set too many files of this type (unless necessary) to ensure system security, avoid system intrusion due to bugs in some programs.
Example 4: $ chmod A-x mm.txt
$ Chmod-x mm.txt
$ Chmod Ugo-x mm.txt
The preceding three commands Delete the execution permission of the mm.txt file, and set the object to all users.
(2) number setting method:
Set all files and subdirectories in the current directory to be readable by anyone:
Chmod-r a + R *
In addition, chmod can use numbers to indicate permissions, such as chmod 777 file.
Syntax: chmod ABC File
Each A, B, and C is a number, indicating the permissions of the user, group, and other respectively.
R = 4, W = 2, x = 1
If you want the rwx attribute, 4 + 2 + 1 = 7;
If the RW-attribute is required, 4 + 2 = 6;
If you want the R-x attribute, 4 + 1 = 7.
Example:
Chmod A = rwx file and chmod 777 file have the same effect
Chmod ug = rwx, O = X file, and chmod 771 file have the same effect.
If chmod 4755 filename is used, the program can have root permissions.
Example 1: $ chmod 644 mm.txt
$ LS-l
Set the attributes of the file mm.txt:
-RW-r -- 1 inin users 1155 Nov 5 mm.txt
File owner (u) inin has read and write permissions
Users in the same group as the file owner (g) have read permission
Others (o) have read permission
Example 2: $ chmod 750 wch.txt
$ LS-l
-Rwxr-x --- 1 inin users 44137 Nov 12 9:22 wchtxt
That is, set the properties of the wchtxt file:
File owner (u) inin readable/writable/executable
Readable/executable permission of persons in the same group as the file owner
Others (o) do not have any Permissions