What is Linux File Permissions?

What is Linux File Permissions?

Linux file permissions are important for managing access to files and folders. There are three main permission types: read (r), write (w), and execute (x). These permissions are given to three groups: the owner, the group, and others (everyone else).

Here's a breakdown of Linux file permissions:

Symbolic Representation:

  • r: Read permission

  • w: Write permission

  • x: Execute permission

  • -: No permission

Numeric Representation:

  • 4: Read permission

  • 2: Write permission

  • 1: Execute permission

  • 0: No permission

Permission Categories:

  1. Owner (u):

    • r: Read permission (4)

    • w: Write permission (2)

    • x: Execute permission (1)

  2. Group (g):

    • r: Read permission (4)

    • w: Write permission (2)

    • x: Execute permission (1)

  3. Others (o):

    • r: Read permission (4)

    • w: Write permission (2)

    • x: Execute permission (1)

Examples:

  1. Symbolic Representation:

    • rwxr-xr--: Read, write, and execute for the owner; read and execute for the group; read-only for others.

    • rw-r--r--: Read and write for the owner; read-only for the group and others.

    • rwxrwxr-x: Read, write, and execute for the owner and group; read and execute for others.

  2. Numeric Representation:

    • 700: Read, write, and execute for the owner; no permissions for the group and others.

    • 644: Read and write for the owner; read-only for the group and others.

    • 775: Read, write, and execute for the owner and group; read and execute for others.

Changing Permissions:

  • chmod Command:

    • To add permission, use +.

    • To remove permission, use -.

    • To set permission explicitly, use =.

Example:

    chmod +x file.txt     # Add execute permission to file.txt
    chmod u=rw,go=r file.txt  # Set explicit permissions
  • chown Command:

    • Change file owner: chown new_owner:new_group file.txt

    • Change only the group: chown :new_group file.txt

  • chgrp Command:

    • Change the group: chgrp new_group file.txt

View Permissions:

  • ls Command:

    • ls -l: Displays detailed information, including permissions.

    • ls -la: Displays hidden files as well.

Special Permissions:

  • Setuid (SUID):

    • Allows a user to execute a file with the permissions of the file owner.

    • Displayed as an s in the owner's execute position: -rwsr-xr-x.

  • Setgid (SGID):

    • Similar to SUID but applies to directories. Files created in the directory inherit the group of the parent directory.

    • Displayed as an s in the group's execute position: -rwxr-sr-x.

  • Sticky Bit:

    • Restricts the deletion of files in a directory to the file owner, the directory owner, or the root user.

    • Displayed as a t in the others' execute position: -rwxr-xr-t.

Examples of Setting Special Permissions:

  • chmod u+s file.txt: Set the setuid bit.

  • chmod g+s directory: Set the setgid bit for a directory.

  • chmod +t directory: Set the sticky bit for a directory.

These basic ideas are important for knowing how to handle file access in Linux systems. Be careful when changing permissions, especially for important system files and folders.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!