In Linux systems, file and directory permissions are a key component of security and access control. Correctly setting permissions on files and directories ensures that only authorized users can read, write, or execute those files and directories.
This article will detail how to change the permissions of files and directories in Linux system.
1. Overview of file and directory permissions
In a Linux system, each file and directory has a set of permissions that determine access to them.
Permissions are divided into three categories: user (Owner), user group (Group) and other users (Others).
Each category has three permission flags:
r
(read): Allows reading the contents of a file, viewing a list of files in a directory.w
(Write): Allows modifying the contents of files, creating, deleting or renaming files in directories.x
(execute): Allows to run a file or enter a directory as an executable.
Permission flags can be represented numerically:
r
:4w
:2x
:1
Adding these numbers gives the permission value in octal. For example:
rwx
: 4+2+1=7 (full permissions)rw-
: 4+2=6 (read and write permissions)r--
:4 (read-only permissions)
2. Use the chmod command to change file and directory permissions
In Linux systems, you can use chmod
commands to change the permissions of files and directories. chmod
The basic syntax of the command is as follows:
chmod [选项] 权限模式 文件/目录
选项
: Used to specifychmod
various options of the command, such as recursively modifying permissions, verbose output, etc.权限模式
: Used to specify the permission mode to be set. Either numeric (eg 755) or symbolic (eg u+rwx) can be used.文件/目录
: Path to the file or directory whose permissions are to be modified.
2.1 Modify permissions in digital form
Using the numerical form, you can directly assign permission values to files and directories. Permissions for each category are represented by three-digit numbers.
The following are examples of edit permissions in numerical form:
chmod 755 file.txt
:file.txt
Set the permissions of the file torwxr-xr-x
(owner has read, write and execute permissions, group and other users have read and execute permissions).chmod 644 file.txt
:file.txt
Set the permissions of the file torw-r--r--
(owner has read and write permissions, group and other users only have read permissions).chmod 700 directory
:directory
Set the directory's permissions torwx------
(only the owner has read, write, and execute permissions).
2.2 Use symbols to modify permissions
Using symbolic form, you can increase or decrease based on the current permissions.
The following is an example of modifying permissions in symbolic form:
- `chmod u+r file
chmod u+r file.txt
Added file.txt
read permissions (r) to owner (u) to the file's permissions.
chmod g-w file.txt
file.txt
Removed the write permission (w) to the group (g) from the file's permissions.
chmod o+x directory
directory
Added execute permission (x) to other users (o) to the directory.
2.3 Modify permissions using symbolic forms and logical operators
The symbolic form also supports logical operators, which can modify multiple permissions at the same time.
The following are examples of logical operators modifying permissions:
chmod u=rw,g+r,o-rwx file.txt
Set file.txt
the permissions of the file as follows: the owner (u) has read and write permissions, the group (g) has read permissions, and other users (o) have no permissions.
chmod u+w,g+x,o=rw directory
Setdirectory
the permissions of the directory as follows: owner (u) has write permissions, group (g) has execute permissions, and other users (o) have read and write permissions.
3. Example of modifying file and directory permissions
Below are some examples showing how to use chmod
commands to modify the permissions of files and directories.
3.1 Modify file permissions
- Open a terminal and execute the following command:
chmod 644 file.txt
Set file.txt
the file's permissions to rw-r--r--
(owner has read and write permissions, group and others only read).
- Verify the file permission changes with the following command:
ls -l file.txt
Terminal displays the details of the file, including permissions.
3.2 Modify directory permissions
- Open a terminal and execute the following command:
chmod 755 directory
Set directory
the permissions of the directory to rwxr-xr-x
(read, write, and execute for owner, read and execute for group and others).
- Verify the directory permission changes with the following command:
ls -ld directory
Terminal will display details of the directory, including permissions.
4. Summary
This article explains in detail how to change the permissions of files and directories in a Linux system. Using chmod
commands, you can set permissions on files and directories numerically or symbolically. Ensuring that file and directory permissions are set correctly keeps your data safe and ensures that only authorized users can access and modify files and directories. Knowing chmod
how to use commands will help you better manage and protect your system and files.