How to change Linux file and directory permissions?

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:4
  • w:2
  • x: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 chmodcommands to change the permissions of files and directories. chmodThe basic syntax of the command is as follows:

chmod [选项] 权限模式 文件/目录
  • 选项: Used to specify chmodvarious 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.txtSet the permissions of the file to rwxr-xr-x(owner has read, write and execute permissions, group and other users have read and execute permissions).
  • chmod 644 file.txt: file.txtSet the permissions of the file to rw-r--r--(owner has read and write permissions, group and other users only have read permissions).
  • chmod 700 directory: directorySet the directory's permissions to rwx------(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.txtread permissions (r) to owner (u) to the file's permissions.

chmod g-w file.txt

file.txtRemoved the write permission (w) to the group (g) from the file's permissions.

chmod o+x directory

directoryAdded 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.txtthe 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 directorySet directorythe 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 chmodcommands to modify the permissions of files and directories.

3.1 Modify file permissions

  1. Open a terminal and execute the following command:
chmod 644 file.txt

Set file.txtthe file's permissions to rw-r--r--(owner has read and write permissions, group and others only read).

  1. 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

  1. Open a terminal and execute the following command:
chmod 755 directory

Set directorythe permissions of the directory to rwxr-xr-x(read, write, and execute for owner, read and execute for group and others).

  1. 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 chmodcommands, 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 chmodhow to use commands will help you better manage and protect your system and files.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/130929614