How to set permissions for files and directories in Linux
In Linux, the command used to set permissions for files and directories is chmod. The basic syntax for the chmod command is as follows:
chmod [选项] 模式 文件或目录
Options can include some parameters, commonly used parameters include: -R (recursive setting permissions, including subdirectories and files), -v (display operation information), -c (display operation information only when changing permissions), etc.
A mode is a string of numbers that represent permission settings, including three parts: owner permission, group permission, and other user permission. Each part can be set as read (r), write (w), and execute (x) permissions, and are represented by numbers 4, 2, and 1 respectively. For example, if you want to set the permissions of a file as rw-r–r–, the corresponding mode would be 644.
Here are some common examples of the chmod command:
- Read, write, read, read, read, read, read.
- Change the permission of the file.txt to be 644.
- read, write, execute – read, execute – read, execute
- Change the permissions of the directory to 755.
- read, write, execute – read, execute – read, execute
- Give read, write, and execute permissions to directory and all its contents.
- Make the file.txt executable for the owner.
- Remove write permission for others on file.txt.
The chmod command allows for flexible adjustment of file and directory permissions to protect system security.