Technology Encyclopedia Home >How to set file and directory permissions on Linux?

How to set file and directory permissions on Linux?

To set file and directory permissions on Linux, you can use the chmod command. This command allows you to modify the access rights of files and directories. Permissions are defined for three types of users: the owner, the group, and others.

Permissions are categorized into three types:

  1. Read (r): Allows viewing the contents of a file or directory.
  2. Write (w): Allows modifying the contents of a file or adding/removing files within a directory.
  3. Execute (x): Allows executing a file as a program or script, or accessing a directory.

The chmod command uses a symbolic mode or an absolute mode to set permissions.

Symbolic Mode

In symbolic mode, you specify the permissions using letters:

  • u for user (owner)
  • g for group
  • o for others
  • a for all (user, group, and others)

Example:

chmod u+x script.sh

This command adds execute permission for the owner of script.sh.

Absolute Mode

In absolute mode, permissions are specified using a three-digit number where each digit represents the permissions for the user, group, and others respectively. Each permission type is assigned a value:

  • Read (r) = 4
  • Write (w) = 2
  • Execute (x) = 1

Example:

chmod 755 script.sh

This command sets the permissions to:

  • Owner: Read (4) + Write (2) + Execute (1) = 7
  • Group: Read (4) + Execute (1) = 5
  • Others: Read (4) + Execute (1) = 5

Setting Directory Permissions

For directories, execute permission is crucial as it allows accessing the directory contents.
Example:

chmod 755 mydirectory

Using Tencent Cloud for File Storage

If you need scalable and secure file storage solutions, consider using Tencent Cloud's services like CFS (Cloud File Storage). CFS offers a highly available and scalable file system that can be mounted to multiple CVM instances, making it easier to manage and share files across your applications.

By using CFS, you can leverage Tencent Cloud's infrastructure to handle large volumes of data and ensure high availability and reliability for your file storage needs.