Linux File Permissions

Linux uses a permission system to control who can read, write, and execute files and directories. Understanding permissions is essential for system administration, security, and software development.

Every file and directory has three types of permissions assigned to three categories of users: owner, group, and others.

1. Understanding Permission Types

PermissionSymbolValueDescription
Readr4View file contents
Writew2Modify file contents
Executex1Run file as a program
BASH
Viewing file permissions
ls -l

-rwxr-xr-- 1 user staff 1024 Jul 20 notes.sh

2. Changing Permissions with chmod

The chmod command changes file permissions using either symbolic or numeric notation.

BASH
Using chmod
# Numeric mode
chmod 755 script.sh
chmod 644 notes.txt

# Symbolic mode
chmod +x script.sh
chmod u+w file.txt
chmod g-r file.txt

3. Changing File Ownership

The chown command changes the owner or group of a file.

BASH
Using chown
sudo chown john file.txt
sudo chown john:developers project.c
sudo chown -R john website/

4. Practical Examples

BASH
Making a script executable
nano hello.sh

#!/bin/bash
echo "Hello Linux"

chmod +x hello.sh
./hello.sh

5. Best Practices

  • Use 644 for regular files.
  • Use 755 for executable scripts and directories.
  • Avoid using chmod 777 unless absolutely necessary.
  • Use least privilege principle.
  • Verify permissions with ls -l after changes.