# Linux File Permissions

# A Deep Dive into Linux File Permissions: Understanding and Managing Access

Understanding Linux file permissions is essential for system security and effective file management. Permissions control who can access or modify files and directories. In Linux, file permissions can be represented in two ways: symbolic mode and octal mode. This guide will help you grasp these concepts with clear examples and explanations.

# 1. Symbolic Mode Permissions

In symbolic mode, file permissions are displayed in a string of characters. This format shows the type of file and the access rights for the owner, group, and others.

# Structure of File Permissions

Here’s how a typical file listing might look:

drwxr-xr-x 2 root root 4.0K Sep 18 10:46 example_dir
-rw-r--r-- 1 root root   19 Sep 18 10:45 example_file.txt

1. File Type

  • First Character: Indicates the file type.
    • d: Directory
    • -: Regular file
    • l: Symbolic link
    • b: Block device
    • c: Character device
    • p: Named pipe
    • s: Socket

2. Owner Permissions

  • Next Three Characters: Permissions for the file owner.
    • r: Read (view contents)
    • w: Write (modify contents)
    • x: Execute (run as a program or enter a directory)
    • -: No permission

3. Group Permissions

  • Next Three Characters: Permissions for the file’s group.
    • Same as owner permissions (r, w, x, -)

4. Other Users' Permissions

  • Last Three Characters: Permissions for all other users.
    • Same as owner permissions (r, w, x, -)
# Examples
  1. -rw-r--r-- (for example_file.txt):

    • -: Regular file
    • rw-: Owner (root) can read and write.
    • r--: Group (root) can read only.
    • r--: Others can read only.
  2. drwxr-xr-x (for example_dir):

    • d: Directory
    • rwx: Owner can read, write, and execute.
    • r-x: Group can read and execute.
    • r-x: Others can read and execute.

# 2. Octal (Numeric) Mode Permissions

In octal mode, permissions are represented by a three-digit number. Each digit corresponds to the permissions for the owner, group, and others.

# Understanding Octal Values
  • r (Read) = 4
  • w (Write) = 2
  • x (Execute) = 1

To represent different permissions, you add these values together:

  • rwx = 4 + 2 + 1 = 7
  • rw- = 4 + 2 + 0 = 6
  • r-- = 4 + 0 + 0 = 4
# Converting Symbolic to Octal
  1. -rw-r--r--:

    • Owner (rw-): 4 + 2 = 6
    • Group (r--): 4 + 0 = 4
    • Others (r--): 4 + 0 = 4
    • Octal Representation: 644
  2. drwxr-xr-x:

    • Owner (rwx): 4 + 2 + 1 = 7
    • Group (r-x): 4 + 0 + 1 = 5
    • Others (r-x): 4 + 0 + 1 = 5
    • Octal Representation: 755

# 3. Changing Permissions

You can modify file permissions using both symbolic and octal modes.

# Using Symbolic Mode
  • Add Permission: chmod u+x file.sh

    • u: User (owner)
    • +x: Adds execute permission
  • Remove Permission: chmod g-w file.sh

    • g: Group
    • -w: Removes write permission
  • Set Exact Permissions: chmod u=rwx,g=rx,o=r file.sh

    • Sets specific permissions for user, group, and others.
# Using Octal Mode
  • Set Permissions: chmod 755 file.sh
    • 755 translates to rwxr-xr-x:
      • Owner: rwx (7)
      • Group: r-x (5)
      • Others: r-x (5)

# 4. Summary of File Listing Columns

In a file listing:

drwxr-xr-x 2 root root 4.0K Sep 18 10:46 example_dir
-rw-r--r-- 1 root root   19 Sep 18 10:45 example_file.txt
  • Column 1: Permissions (drwxr-xr-x or -rw-r--r--)
  • Column 2: Number of hard links (2 or 1)
  • Column 3: Owner (root)
  • Column 4: Group (root)
  • Column 5: Size (4.0K or 19)
  • Column 6: Last modification date and time (Sep 18 10:46)
  • Column 7: File or directory name (example_dir or example_file.txt)

Grasping Linux file permissions is vital for maintaining system security and managing file access. By understanding both symbolic and octal modes, you can effectively control who has access to your files and directories. Mastering these concepts ensures you can protect your data and manage your Linux environment efficiently.