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.
-rwxr-xr-- = 754 ||| ||+-- others: r-- = 4 |+--- group: r-x = 5 +---- owner: rwx = 7First character: - file, d directory, l symlinkWhat Permissions Mean for Files vs Directories
Regular file: r read contents w modify contents x run as programDirectory: r list names (ls) w create/rename/delete entries x enter (cd) and access items insideTo 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
chmod u+x script.sh # add execute for ownerchmod go-w file.txt # remove write for group and otherschmod 644 config.txt # rw-r--r--chmod 755 script.sh # rwxr-xr-xchmod 600 ~/.ssh/id_ed25519 # private keychmod 700 ~/.ssh chown rahul:devops file # owner and groupchown -R rahul:devops dir/ # Set files and directories separatelyfind /var/www/html -type f -exec chmod 644 {} +find /var/www/html -type d -exec chmod 755 {} + umask # default permission maskSpecial Bits: setuid, setgid, and Sticky
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 groupls -ld /tmp # drwxrwxrwt : t = sticky, only owners delete their files # Audit privileged binariesfind / -perm -4000 -type f 2>/dev/null # setuidfind / -perm -2000 -type f 2>/dev/null # setgidfind / -perm -0002 -type f 2>/dev/null # world-writable filesTroubleshooting 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/pathprints the permissions of every component in a path, which finds the blocking directory immediately.
Common MistakeRunning
chmod 777to make an error go away. It lets any user modify or run the file. Fix ownership withchownor group membership, then grant the minimum bits needed.
SecurityAudit 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.