chmod
The command is used to modify the permissions of a file or directory. It is very important for user rights management in Linux systems. chmod
The (Change Mode) command can change the read, write, and execute permissions of a file or directory, and can set different permissions for the owner, the group to which it belongs, and other users.
The following is chmod
the basic syntax of the command:
chmod [选项] 模式 文件/目录
Options:
-R
: Recursively modify the permissions of a folder and its subfolders.-v
: Display detailed information of the operation.-c
: Only displayed file permissions that have changed.
model:
Permission modes are represented by numbers or symbols.
-
Numeric mode: Use three-digit octal numbers from 0 to 7 to represent permissions. Each digit represents a group of users, the first digit represents the owner's permissions, the second digit represents the group's permissions, and the third digit represents other user's permissions.
0
:permission denied1
:Execute permission2
:write permission3
:write and execute permissions4
:Read permission5
:Read and execute permissions6
:Read and write permissions7
: All permissions
For example, the owner
chmod 755 file.txt
of a file willfile.txt
be given read, write, and execute permissions, while the group and other users will only have read and execute permissions. -
Symbolic mode: Use
u
(owner),g
(group),o
(other users) anda
(all users) with+
,-
,=
and permission signs to represent permissions.+
:Add permissions-
:Remove permissions=
:Setting permissions
The permission flag can be
r
(read),w
(write), orx
(execute). For example, write permissionchmod u+w file.txt
will be added to the owner of the file .file.txt
Example:
chmod 755 file.txt
: Set the filefile.txt
permissions to-rwxr-xr-x
.chmod u+x script.sh
:script.sh
Add execution permissions to the owner of the script.chmod -R 644 dir/
: Recursivelydir/
set the permissions of a folder and its sub-files to-rw-r--r--
.