Skip to main content

File Permissions

Linux file permissions control who can read, write, or execute a file using three classes: owner, group, and others. They are shown as rwxr-xr-- or as an octal number such as 754.

Understanding Linux File Permissions

What Do rwx and 755 Mean?

Every file has three sets of permission bits, one each for the owner, the group, and everyone else. Each set has read (r), write (w), and execute (x). Octal notation adds them up: r=4, w=2, x=1.

◈ DIAGRAM
-rwxr-xr-- = 754
|||
||+-- others: r-- = 4
|+--- group: r-x = 5
+---- owner: rwx = 7
First character: - file, d directory, l symlink

What Permissions Mean for Files vs Directories

Bash
Regular file: r read contents w modify contents x run as program
Directory: r list names (ls) w create/rename/delete entries
x enter (cd) and access items inside

To reach /var/www/html/app/index.html, a user needs execute on every directory in the path, not only on the file.

How to Change Permissions and Ownership

Bash
chmod u+x script.sh # add execute for owner
chmod go-w file.txt # remove write for group and others
chmod 644 config.txt # rw-r--r--
chmod 755 script.sh # rwxr-xr-x
chmod 600 ~/.ssh/id_ed25519 # private key
chmod 700 ~/.ssh
chown rahul:devops file # owner and group
chown -R rahul:devops dir/
# Set files and directories separately
find /var/www/html -type f -exec chmod 644 {} +
find /var/www/html -type d -exec chmod 755 {} +
umask # default permission mask

Special Bits: setuid, setgid, and Sticky

Bash
ls -l /usr/bin/passwd # -rwsr-xr-x : s = setuid, runs as owner (root)
chmod g+s /shared/team-dir # new files inherit the directory's group
ls -ld /tmp # drwxrwxrwt : t = sticky, only owners delete their files
# Audit privileged binaries
find / -perm -4000 -type f 2>/dev/null # setuid
find / -perm -2000 -type f 2>/dev/null # setgid
find / -perm -0002 -type f 2>/dev/null # world-writable files

Troubleshooting Permissions

Symptom Command What to Look For
Permission denied reading ls -la file Missing r bit
Cannot run script ls -la script.sh Missing x bit
SSH key rejected ls -la ~/.ssh Key 600, directory 700
Web server 403 namei -l /var/www/html/index.html Execute on each directory
Tip

namei -l /full/path prints the permissions of every component in a path, which finds the blocking directory immediately.

Common Mistake

Running chmod 777 to make an error go away. It lets any user modify or run the file. Fix ownership with chown or group membership, then grant the minimum bits needed.

Security

Audit setuid and setgid binaries regularly. An unexpected setuid file is a red flag because it runs with its owner's privileges regardless of who starts it.

Frequently Asked Questions

What does each digit in 754 represent?

Digits are owner, group, then others, each the sum of read (4), write (2), and execute (1). So 754 means owner rwx, group r-x, others r--.

Why is chmod 777 considered bad practice?

It grants every user on the system full control of the file, which is a real risk on shared or web-facing paths. The right fix is correct ownership or group access plus minimal permission bits.

What is the difference between the x bit on a file and on a directory?

On a file it allows running it as a program. On a directory it allows entering it and accessing items inside by name, even if you cannot list the contents.

Found a mistake, an outdated step or a broken link?