The chmod command is used to change access rights for Linux system files or directories. Use it to control access to files or directories. There are two ways to use this command. One is a text-setting method that contains letters and operator expressions, and the other is a digital setting method that contains numbers.
Each file and directory in a Linux system has access permissions, which are used to determine who can access and manipulate files and directories.
Access to a file or directory is divided into read-only, write-only, and executable three types. As an example of a file, a read-only permission means that only the content is allowed to be read, and any changes to it are forbidden. Executable permission means that the file is allowed to be executed as a program. When a file is created, the file owner automatically has read, write, and execute permissions on the file to facilitate the reading and modification of the file. Users can also set access rights to any combination they want, as needed.
There are three different types of users who can access files or directories: The file owner, the same group of users, and other users. The owner is typically the creator of the file. The owner can allow the same group of users access to the file, as well as the access rights of the file to other users on the system. In this case, every user in the system can access the files or directories that the user owns.
Each file or directory has three groups of access rights, each group is represented by three bits, respectively, the read, write, and execute permissions of the file owner, the read, write, and execute permissions of the user belonging to the primary group, and the read, write, and execute permissions of other users in the system. When you use the LS-L command to display the details of a file or directory, the leftmost column is the file's access rights. For example:
Ls-al
-rw-r--r--1 root root 298K 11-16 09:01 001.log
The first column has 10 locations, and the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a horizontal line, it represents a non-directory file. If it is D, the representation is a directory. From the second character to the tenth, a total of 9 characters, a group of 3 characters, respectively, representing 3 groups of users of the file or directory permissions. The permission character is represented by a horizontal line for an empty license, R for Read only, W for write, and X for executable.
After determining the access rights of a file, users can use the chmod command provided by the Linux system to reset different access rights. You can also use the Chown command to change the owner of a file or directory. Using CHGRP
command to change the user group for a file or directory.
The chmod command is very important for changing the access rights of a file or directory. Users use it to control access to files or directories. The details of the chmod command are as follows.
1. Command format:
chmod [-CFVR] [--help] [--version] Mode file
2. Command function:
Used to change the access rights of a file or directory, using it to control access to a file or directory.
3. Command parameters:
Required Parameters:-C When a change occurs, the report processes the information
-F error Message not output
-R handles all files in the specified directory and its subdirectories
-V Run-time display verbose processing information
Select parameters:
--reference=< directory or File > set to have the same permissions as the specified directory or file
--version displaying version information
< permissions >+< permissions settings > Allow directories or files within a permission scope to have the specified permissions
< permission scope >-< permission settings > Delete permission scope of directory or file specified permission
< permission range >=< permissions settings > Set permissions for a directory or a file in a permission range for a specified value
Permission Range
U: The current user of a directory or a file (user)
G: The current group of directories or files (group)
O: Users or groups other than the current user or group of directories or files (other)
A: All user-level groups (all)
Permission Code
R: Read permission, denoted by the number 4
W: Write permission, denoted by the number 2
X: Execute permission, denoted by the number 1
-: Delete permission, denoted by the number 0
S: Special Permissions
There are two ways to use this command. One is a text-setting method that contains letters and operator expressions, and the other is a digital setting method that contains numbers.
1) Text Setting method:
chmod [who] [+|-|=] [mode] File name
2) Digital Setting Method:
We must first understand the meaning of the attributes represented by numbers: 0 means no permissions, 1 means executable permissions, 2 is writable, 4 is read, and then it is added. So the format of the numeric attribute should be 3 octal numbers from 0 to 7, in the Order of (U) (g) (O)
For example, if you want a file's properties to have "read/write" Two permissions, you need to put 4 (readable) +2 (writable) =6 (read and write)
The numbers correspond to characters as follows:
R=4, w=2, x=1
To rwx the property is 4+2+1=7
4. Usage examples:
Example 1: Add file All user groups executable permissions
Command: chmod a+x 001.log
Example 2: Modify different user permissions at the same time
Command: chmod ug+w, O-x 001.log
Example 3: Setting permissions with "="
Command: chmod u=x 001.log revokes All of the original permissions, and then makes the owner readable.
Example 4: Add permissions to all files in a directory and its subdirectories
Command:
Chmod-r u+x dir This command recursively assigns the owner of all files and subdirectories under directory dir
Other examples:
Command: chmod 751 file assigns permissions digitally, ===chmod u=rwx,g=rx,o=x file
One Linux command per day (--linux) chmod command