SyntaxStudy
Sign Up
Linux / Bash Understanding Linux File Permissions
Linux / Bash Beginner 1 min read

Understanding Linux File Permissions

Every file and directory in Linux has an associated set of permissions that control who can read, write, or execute it. Permissions are divided into three classes: the owning user (u), the owning group (g), and all others (o). Each class has three permission bits: read (r = 4), write (w = 2), and execute (x = 1). The `ls -l` command displays permissions as a ten-character string. The first character indicates the file type (`-` for regular file, `d` for directory, `l` for symbolic link). The next nine characters are three groups of three: user, group, and other permissions. For example, `-rwxr-xr--` means the owner can read/write/execute, the group can read/execute, and others can only read. For directories, the execute bit has a special meaning: it controls whether you can `cd` into the directory or access files within it. A directory with read but no execute permission lets you list filenames but not access them. This nuance is important when configuring web server document roots and shared directories.
Example
# View file permissions
ls -l /etc/passwd
# -rw-r--r-- 1 root root 2847 Apr  1 10:00 /etc/passwd
# ^          ^    ^    ^
# type+perms links owner group

# View permissions in octal
stat -c "%a %n" /etc/passwd
# 644 /etc/passwd

# Permission bit reference:
# r = 4, w = 2, x = 1
# rwx = 7 | rw- = 6 | r-x = 5 | r-- = 4 | -wx = 3 | -w- = 2 | --x = 1 | --- = 0

# Common permission patterns:
# 644  (-rw-r--r--)  regular files
# 755  (drwxr-xr-x)  directories & executables
# 600  (-rw-------)  private keys, sensitive files
# 700  (drwx------)  private directories
# 777  (drwxrwxrwx)  world-writable (avoid in production)

# Check permissions on several files
ls -l /etc/shadow /etc/sudoers /etc/ssh/ssh_host_rsa_key
# ---------- 1 root shadow  ...  /etc/shadow
# -r--r----- 1 root root   ...  /etc/sudoers
# ---------- 1 root root   ...  /etc/ssh/ssh_host_rsa_key

# Display all files in a dir with permissions
ls -la /etc/ | head -20

# Find world-writable files (security audit)
find /var/www -perm -002 -type f