#Why this matters
Navigating Linux directories with ls and find quickly shows you that you cannot read or edit every file on the system. Linux enforces discretionary access control on every file and directory through ownership and permission bits. In both system hardening and beginner Linux challenges, one misconfigured permission -- such as a world-readable SSH key or a world-writable script -- is often the entire difference between a secure host and a compromise.
#Reading ls -l output
Running ls -l prints a ten-character permission field followed by owner and group names:
-rw-r----- 1 appadmin auditors 420 Nov 10 09:15 report.confBreak that first field into four parts:
- File type (character 1):
-is a regular file,dis a directory, andlis a symbolic link. - Owner (
u, characters 2-4):rw-meansappadmincan read and write, but not execute. - Group (
g, characters 5-7):r--means members ofauditorscan read only. - Others (
o, characters 8-10):---means everyone else on the host has no access.
#Files versus directories
The same r, w, and x letters mean different things on a file than on a directory:
| Bit | On a regular file | On a directory |
|---|---|---|
r (4) |
Read the file's contents | List filenames inside the directory (ls) |
w (2) |
Modify or truncate file contents | Create, rename, or delete entries inside the directory |
x (1) |
Run the file as a program or script | Traverse (cd) into the directory and access file inodes |
Pay close attention to directory w: if a user has write permission on a directory, they can delete or replace a file inside it even if they do not own that file, unless the sticky bit protects the directory.
#Symbolic and numeric (octal) modes
You change permissions with chmod and ownership with chown. Note that changing a file's owner with chown normally requires root privileges.
In octal mode, add the weights r = 4, w = 2, and x = 1 for each class:
7(4+2+1) =rwx,6(4+2) =rw-,5(4+1) =r-x,4=r--,0=---.600(rw-------): private keys, tokens, personal secrets.644(rw-r--r--): normal non-secret configuration or documentation.750(rwxr-x---): scripts or project folders shared with one group.
In symbolic mode, adjust only the bit you want without recalculating the rest: chmod u+x deploy.sh adds execute for the owner, while chmod o-rwx secret.txt strips all access from others.
$ ls -la /tmp/perm-lab
total 8
drwxr-xr-x 2 student student 4096 Nov 12 10:15 .
drwxrwxrwt 9 root root 4096 Nov 12 10:14 ..
-rwxrwxrwx 1 student student 112 Nov 12 10:15 backup.sh
-rw-r--r-- 1 student student 38 Nov 12 10:15 api_token.txt
$ chmod 600 /tmp/perm-lab/api_token.txt
$ chmod 750 /tmp/perm-lab/backup.sh
$ stat -c "%a %U:%G %n" /tmp/perm-lab/api_token.txt /tmp/perm-lab/backup.sh
600 student:student /tmp/perm-lab/api_token.txt
750 student:student /tmp/perm-lab/backup.sh- Check classOwner (u), Group (g), or Others (o)?
- Pick bitsRead (4), Write (2), Execute (1)
- Applychmod 600 or chmod o-rwx
- Verifyls -l or stat -c "%a %U:%G %n"
#Special bits: SUID, SGID, and sticky bit
Occasionally ls -l shows s or t instead of x:
- SUID (
4xxx,sin owner execute): When executed, the program runs with the file owner's privileges. For example,/usr/bin/passwdis owned byrootwith-rwsr-xr-xso normal users can update their own password in/etc/shadow. Unnecessary SUID binaries owned byrootare a classic privilege-escalation risk. - SGID (
2xxx,sin group execute): On a directory, new files inherit the directory's group instead of the creator's primary group. - Sticky bit (
1xxx,tin others execute): Seen on/tmp(drwxrwxrwt). Any user can create files there, but only a file's owner (orroot) can delete or rename it.
#Common mistakes
- Using
chmod 777to fix permission errors: Grantingrwxto everyone lets any local account tamper with scripts or read sensitive files. Fix ownership (chown) or group membership instead. - Leaving secrets at
644: Defaultumaskvalues often create files readable by all users (644). Always verify SSH keys, API tokens, and backups withls -l. - Forgetting directory
x: Removingxon a folder blocks users from entering or accessing files inside even if they haver. - Running
chmod -Rcarelessly: Recursive changes on/etcor/varcan break system services and SSH authentication immediately.
#Summary
Linux controls file access using owner, group, and other classes combined with r, w, and x bits, where r, w, and x carry distinct meanings on directories versus regular files. Octal modes like 600 and 750 or symbolic adjustments with chmod let you enforce least privilege, while chown assigns the right owner and group. Recognizing special bits (s and t) and auditing world-readable secrets or world-writable scripts prevents common local exposures.