Understanding Linux File Permissions
One of the first concepts every Linux user needs to master is file permissions. Linux is a multi-user operating system, and its permission system controls exactly who can read, write, or execute any file or directory. Once you understand this system, you'll feel far more confident navigating and managing your Linux environment.
The Three Permission Types
Every file and directory in Linux has three types of permissions:
- Read (r) — Allows viewing the contents of a file, or listing a directory's contents.
- Write (w) — Allows modifying a file, or creating/deleting files within a directory.
- Execute (x) — Allows running a file as a program, or entering a directory with
cd.
The Three Permission Groups
These permissions are assigned to three distinct groups:
- Owner (u) — The user who owns the file.
- Group (g) — Users who belong to the file's assigned group.
- Others (o) — Everyone else on the system.
Reading Permission Output
When you run ls -l in a terminal, you'll see output like this:
-rwxr-xr-- 1 alice devs 4096 Jan 10 09:00 script.sh
Breaking down the first field -rwxr-xr--:
- The first character (
-) indicates file type:-for file,dfor directory,lfor symlink. - Next three characters (
rwx) — Owner permissions: read, write, execute. - Next three characters (
r-x) — Group permissions: read, no write, execute. - Last three characters (
r--) — Others permissions: read only.
Changing Permissions with chmod
The chmod command changes file permissions. You can use symbolic or numeric (octal) mode.
Symbolic Mode
Symbolic mode uses letters to add (+), remove (-), or set (=) permissions:
chmod u+x file.sh— Add execute permission for the owner.chmod g-w file.txt— Remove write permission from the group.chmod o=r file.txt— Set others to read-only.chmod a+x script.sh— Add execute for all (owner, group, others).
Numeric (Octal) Mode
Each permission type has a numeric value: read = 4, write = 2, execute = 1. Add them together for each group:
| Permission | Value |
|---|---|
| rwx | 7 |
| rw- | 6 |
| r-x | 5 |
| r-- | 4 |
| --- | 0 |
Example: chmod 755 script.sh gives the owner full permissions and everyone else read+execute.
Changing Ownership with chown
The chown command changes the owner and/or group of a file:
chown alice file.txt— Change owner to alice.chown alice:devs file.txt— Change owner to alice and group to devs.chown -R alice:devs /project/— Recursively change ownership of a directory.
Special Permissions: SUID, SGID, and Sticky Bit
Beyond the basic permissions, Linux has three special bits worth knowing:
- SUID (Set User ID) — When set on an executable, it runs as the file's owner. Example:
chmod u+s program. - SGID (Set Group ID) — Files created in an SGID directory inherit the directory's group. Example:
chmod g+s /shared/. - Sticky Bit — On directories, only the file's owner can delete it. Commonly used on
/tmp. Example:chmod +t /tmp/.
Key Takeaways
Understanding file permissions is essential for Linux security and system management. Start by practicing ls -l to read permissions, then experiment with chmod in a safe environment. Always be cautious when using chmod 777 as it grants full access to everyone — a common security mistake beginners make.