Linux File Permissions: The Essential Guide
In this article
- Background: Why Permissions Exist
- How Permissions Work: The Three Triads
- Changing Permissions: chmod and chown
- Special Permissions: setuid, setgid, and Sticky Bit
- Choosing the Right Permissions
- Common Problems and Misconceptions
- Alternatives and Advanced Access Control
- Safety and Best Practices
- Frequently Asked Questions
Linux file permissions control who can read, write, and execute files and directories, and understanding them is essential for system security and multi-user management. The core model is simple: three permission types (read, write, execute) applied to three user classes (owner, group, others), represented by symbols like rwxr-xr-- or numbers like 755. This article explains the model, how to read and change permissions, and common pitfalls.
Background: Why Permissions Exist
Linux is a multi-user operating system from the ground up. Unlike single-user systems where every process runs with the same authority, Linux assigns every file and directory an owner and a group, and it tracks three distinct permission sets. This design lets a system administrator grant different levels of access to different people or processes without giving everyone full control. For example, a web server process can be given read access to website files but no write access, so a compromise of that process does not let an attacker modify the content. The permission system also underpins the security model of every Linux distribution, from desktop Ubuntu to enterprise Red Hat.
How Permissions Work: The Three Triads
When you run ls -l in a directory, the first column looks like -rwxr-xr--. That string is ten characters: the first is the file type (a dash for a regular file, d for a directory, l for a symbolic link), and the remaining nine are three groups of three:
- Owner (user) – the account that created the file or was assigned ownership.
- Group – a named collection of users; every user belongs to at least one primary group.
- Others – everyone else on the system who is not the owner and not in the group.
Within each triad, the letters mean:
- r (read) – for a file, view contents; for a directory, list its entries.
- w (write) – for a file, modify or truncate it; for a directory, create, delete, or rename files inside it.
- x (execute) – for a file, run it as a program or script; for a directory, traverse into it (needed to access files inside).
A dash means that permission is not granted. So rwxr-xr-- means the owner can read, write, and execute; the group can read and execute; others can only read. The numeric representation converts each triad to a binary value: read=4, write=2, execute=1. Summing them gives a single digit per class: rwx = 7, r-x = 5, r-- = 4, hence 755.
Changing Permissions: chmod and chown
The primary command for changing permissions is chmod. You can use symbolic or numeric syntax. Symbolic is more intuitive for small changes:
chmod u+x file– add execute permission for the owner.chmod g-w file– remove write permission for the group.chmod o=r file– set others' permissions to read-only.chmod a+r file– add read for all three classes.
Numeric syntax sets all three at once, which is common in scripts and tutorials: chmod 750 myscript gives the owner full access, the group read and execute, and others nothing. To change ownership, use chown: chown alice:developers file sets the owner to alice and the group to developers. Both commands require appropriate privileges – typically you must be the owner or root to change permissions, and root to change ownership away from yourself.
Special Permissions: setuid, setgid, and Sticky Bit
Beyond the basic nine letters, there are three special permission bits that appear in the execute position:
- setuid (s on owner) – when set on an executable file, the program runs with the file owner's privileges, not the invoking user's. The classic example is
/usr/bin/passwd, which needs root privileges to update the password database. - setgid (s on group) – on an executable, the program runs with the group's privileges. On a directory, new files created inside inherit the directory's group, which is useful for collaborative project folders.
- Sticky bit (t on others) – set on directories like
/tmp, it restricts deletion so that only the file owner (or root) can remove a file, even if the directory is world-writable.
In numeric notation, these add a fourth digit before the usual three: setuid=4, setgid=2, sticky=1. For example, chmod 1777 /tmp sets the sticky bit plus full permissions for everyone. In ls -l output, a lowercase s or t means the execute bit is also set; an uppercase S or T means the special bit is set but execute is not.
Choosing the Right Permissions
There is no universal “best” set, but a few practical rules cover most cases:
- Executable files and scripts:
755is typical, letting everyone run the program but only the owner modify it. If only a specific group should run it, use750. - Personal documents:
600(owner read/write only) or644(owner read/write, everyone else read-only) if you want others to view them. - Directories for collaboration:
770with a shared group, plus the setgid bit so new files inherit the group. - Web server files: typically
644for files and755for directories, so the server can read but not modify them.
When in doubt, start with the most restrictive set that works and add permissions only as needed. Overly permissive settings like 777 (everyone can read, write, and execute) are a common cause of security issues.
Common Problems and Misconceptions
One frequent mistake is confusing directory execute permission with file execute permission. To read a file in a directory, you need read on the file and execute on every directory in its path. If you get “Permission denied” when trying to cd into a directory or access a file, the missing bit is often execute on a parent directory, not the file itself.
Another common issue is that chmod on a directory does not affect files already inside it. To change permissions recursively, you must use chmod -R, but be careful: applying 755 recursively will make all files executable, which is rarely what you want. A safer pattern is to run find . -type d -exec chmod 755 {} \) for directories and a separate command for files.
A third misconception is that the owner of a file can always read it. If the owner's read bit is off, even the owner is denied access, unless they are root. Root bypasses all normal permission checks, which is why system administration tasks are done as root or with sudo.
Alternatives and Advanced Access Control
The classic permission model is simple but coarse. For finer control, Linux offers two more layers:
- Access Control Lists (ACLs) – allow you to grant permissions to specific users or groups beyond the owner/group/others triad. For example,
setfacl -m u:alice:rw filegives Alice read/write access without changing the file's group. Usegetfaclto view ACLs. - SELinux or AppArmor – mandatory access control systems that can restrict processes even if the file permissions allow access. These are enabled by default on many distributions (SELinux on Fedora/RHEL, AppArmor on Ubuntu/Debian) and can cause confusing “Permission denied” errors when file permissions look correct. Check
ls -Zfor SELinux context oraa-statusfor AppArmor profiles if you suspect this.
For most users, mastering the basic rwx model and the chmod/chown commands is sufficient. ACLs and MAC systems are advanced topics that become relevant in enterprise environments or when troubleshooting stubborn access issues.
Safety and Best Practices
Always verify permissions before exposing a file to the network or to other users. A quick ls -l review is a good habit. Avoid using chmod 777 as a quick fix – it grants write and execute to everyone, which is almost never necessary. If a program needs write access to a directory, consider creating a dedicated group and using 770 with setgid instead. Also remember that permissions are only one layer of security; they do not protect against a compromised root account or a malicious process running as the file owner.
Frequently Asked Questions
What does chmod 755 mean?
It sets the owner to read, write, and execute (7), the group to read and execute (5), and others to read and execute (5). This is the standard permission for executable programs and directories that need to be accessible to everyone.
Why do I get “Permission denied” even though I own the file?
Check the owner's permissions in the first triad. If the read or write bit is off for the owner, you will be denied. Also check every parent directory's execute bit – you need execute on each directory in the path to reach the file.
What is the difference between chmod and chown?
chmod changes the permission bits (read, write, execute) on a file or directory. chown changes the owner and/or group of the file. They are independent: you can change permissions without changing ownership and vice versa.
How do I make a file executable?
Run chmod +x filename (or chmod 755 filename for a typical executable). If it is a script, ensure the first line is a shebang like #!/bin/bash so the system knows which interpreter to use.
What is the sticky bit used for?
The sticky bit on a directory prevents users from deleting files they do not own, even if the directory is world-writable. It is set on /tmp and is useful for shared directories where many users create files.
Can I set permissions for a specific user without using groups?
Yes, using POSIX ACLs. The command setfacl -m u:username:rwx /path/to/file grants that user specific permissions, and getfacl shows the full ACL. This does not alter the traditional owner/group/others bits but adds extra entries.