Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Manage File Permissions in Linux

File permissions are a fundamental aspect of managing a Linux system. They determine who can read, write, or execute a file. Properly setting file permissions is crucial for system security and operational efficiency. This article will guide you through understanding and managing file permissions in Linux, ensuring you can control access to your files effectively.

Examples:

  1. Understanding File Permissions: Each file and directory in Linux has three types of permissions: read (r), write (w), and execute (x). These permissions are assigned to three categories of users: the owner, the group, and others. You can view the permissions of a file using the ls -l command.

    ls -l example.txt

    Output:

    -rw-r--r-- 1 user group 0 Jan 1 12:00 example.txt

    In this example:

    • -rw-r--r-- indicates the permissions.
    • The first character (-) indicates it is a regular file.
    • rw- means the owner has read and write permissions.
    • r-- means the group has read-only permission.
    • r-- means others have read-only permission.
  2. Changing File Permissions with chmod: The chmod command is used to change the permissions of a file or directory. You can use symbolic or numeric modes to set permissions.

    • Symbolic Mode:

      chmod u+x example.sh

      This command adds execute permission to the user (owner) for the file example.sh.

    • Numeric Mode:

      chmod 755 example.sh

      This command sets the permissions to rwxr-xr-x, where:

      • 7 (rwx) is for the owner.
      • 5 (r-x) is for the group.
      • 5 (r-x) is for others.
  3. Changing Ownership with chown: The chown command changes the ownership of a file or directory.

    sudo chown newuser:newgroup example.txt

    This command changes the owner to newuser and the group to newgroup for the file example.txt.

  4. Changing Group Ownership with chgrp: The chgrp command changes the group ownership of a file or directory.

    sudo chgrp newgroup example.txt

    This command changes the group to newgroup for the file example.txt.

  5. Recursive Permission Changes: To change permissions recursively for all files and directories within a directory, use the -R option.

    chmod -R 755 /path/to/directory

    This command sets the permissions to rwxr-xr-x for all files and directories within /path/to/directory.

  6. Special Permissions:

    • Setuid (Set User ID):

      chmod u+s example.sh

      This allows a file to be executed with the permissions of the file owner.

    • Setgid (Set Group ID):

      chmod g+s example_directory

      This ensures new files created within the directory inherit the group of the directory.

    • Sticky Bit:

      chmod +t /shared_directory

      This ensures only the file owner can delete files within a directory.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.