Detailed explanation of chmod command in linux

chmodThe command is used to modify the permissions of a file or directory. It is very important for user rights management in Linux systems. chmodThe (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 chmodthe 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.

  1. 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 denied
    • 1:Execute permission
    • 2:write permission
    • 3:write and execute permissions
    • 4:Read permission
    • 5:Read and execute permissions
    • 6:Read and write permissions
    • 7: All permissions

    For example, the owner chmod 755 file.txtof a file will file.txtbe given read, write, and execute permissions, while the group and other users will only have read and execute permissions.

  2. Symbolic mode: Use u(owner), g(group), o(other users) and a(all users) with +, -, =and permission signs to represent permissions.

    • +:Add permissions
    • -:Remove permissions
    • =:Setting permissions

    The permission flag can be r(read), w(write), or x(execute). For example, write permission chmod u+w file.txtwill be added to the owner of the file .file.txt

Example:

  1. chmod 755 file.txt: Set the file  file.txt permissions to  -rwxr-xr-x.
  2. chmod u+x script.shscript.sh Add execution permissions to the owner of the script.
  3. chmod -R 644 dir/: Recursively  dir/ set the permissions of a folder and its sub-files to  -rw-r--r--.

Guess you like

Origin blog.csdn.net/jkzyx123/article/details/132172113