Overview

File permissions control who can read, write, and execute files on a Linux system. Misconfigured permissions are a leading cause of security incidents. This tutorial explains the permission model and shows how to use chmod, chown, and chgrp correctly.

The Permission Model

Every file and directory has three permission sets:

SetSymbolApplies to
UseruThe file owner
GroupgMembers of the file's group
OthersoEveryone else

Each set has three permission bits: read (r), write (w), and execute (x).

Reading Permission Strings

ls -l script.sh
-rwxr-xr-- 1 alice devs 1024 Jan 1 12:00 script.sh
PositionMeaning
-File type (- regular file, d directory, l symlink)
rwxOwner: read, write, execute
r-xGroup: read, execute (no write)
r--Others: read only

Numeric (Octal) Mode

Each permission maps to a number: read = 4, write = 2, execute = 1. Sum the values for each set.

OctalSymbolicMeaning
7rwxFull access
6rw-Read and write
5r-xRead and execute
4r--Read only
0---No permissions

Common Octal Modes

ModeSymbolicTypical use
755rwxr-xr-xExecutable scripts, web directories
644rw-r--r--Regular files, HTML, CSS
600rw-------Private keys, sensitive config
700rwx------Private directories

Symbolic Mode

Symbolic mode uses letters and operators instead of numbers.

# Add execute for owner
chmod u+x script.sh

# Remove write for group and others
chmod go-w file.txt

# Set exact permissions for all
chmod u=rwx,g=rx,o= file.sh

Changing Ownership

# Change owner
sudo chown alice file.txt

# Change owner and group
sudo chown alice:developers file.txt

# Change group only
sudo chgrp developers file.txt

# Recursively change ownership
sudo chown -R alice:developers /var/www/site

Special Permissions

BitOctalEffect
Setuid4xxxRuns as file owner, not the user who executed it
Setgid2xxxOn directories, new files inherit the group
Sticky bit1xxxOnly the file owner can delete files in the directory (used on /tmp)

Security Best Practices

  • Never use chmod 777 on production files or directories. It gives everyone full access.
  • Private keys should be 600. SSH refuses to use keys with looser permissions.
  • Directories need execute permission to be traversed. A directory with mode 644 cannot be entered even if it is readable.
  • Use groups instead of world permissions to share access among a team.
  • Audit regularly with find /var/www -perm -o+w to locate world-writable files.