File permissions: reading rwx, chmod (symbolic and numeric), chown, umask and special permissions
Linux is a multi-user system: a server may run a web server, a database and several people's accounts at the same time. Permissions decide who can read, change or run each file. Wrong permissions are one of the most common causes of "403 Forbidden" website errors, "Permission denied" messages, and serious security holes (like a configuration file with database passwords readable by everyone).
Three kinds of users
Every file has:
- an owner (user,
u): usually whoever created it - a group (
g): a set of users who share access - others (
o): everyone else
Three permissions
| Permission | On a file | On a directory |
|---|---|---|
| r (read) | View contents | List the names inside (ls) |
| w (write) | Change contents | Create, delete and rename files inside |
| x (execute) | Run it as a program/script | Enter it (cd) and access files inside |
Note: deleting a file depends on write permission on the folder, not on the file itself.
Reading permission strings
ls -l-rwxr-xr-- 1 wanjiku developers 512 Sep 7 10:15 deploy.sh- rwx r-x r--
│ │ │ └── others: read only
│ │ └──────── group: read + execute
│ └────────────── owner: read + write + execute
└─────────────────── type (- file, d directory, l link)So: wanjiku can do everything; members of developers can read and run it; everyone else can only read it.
chmod with symbols
chmod u+x deploy.sh # give the owner execute
chmod g+w notes.txt # give the group write
chmod o-r secret.txt # remove read from others
chmod a+r public.html # a = all (u, g and o)
chmod u=rw,go=r page.html # set exactly
chmod -R g+w shared/ # recursive+ adds, - removes, = sets exactly.
chmod with numbers (octal)
Each permission has a value: r = 4, w = 2, x = 1. Add them for each of owner, group, others:
| Number | Permissions |
|---|---|
| 7 | rwx (4+2+1) |
| 6 | rw- (4+2) |
| 5 | r-x (4+1) |
| 4 | r-- |
| 0 | --- |
chmod 755 deploy.sh # rwxr-xr-x: owner all, others read and run
chmod 644 index.html # rw-r--r--: owner edits, everyone reads
chmod 600 ~/.ssh/id_ed25519 # rw-------: private key, owner only
chmod 700 ~/private # rwx------: private folderCommon values:
| Value | Typical use |
|---|---|
| 644 | Normal files, web pages, images |
| 755 | Directories, scripts and programs |
| 600 | Private keys, files with passwords (.env, config with DB passwords) |
| 700 | Private directories (~/.ssh) |
| 777 | Everyone can do everything: avoid; it's a security risk |
You can calculate permissions in Python to check your understanding:
def octal(perm):
vals = {"r": 4, "w": 2, "x": 1, "-": 0}
return "".join(str(sum(vals[c] for c in perm[i:i+3])) for i in (0, 3, 6))
for p in ["rwxr-xr-x", "rw-r--r--", "rw-------", "rwxrwxrwx"]:
print(p, "=", octal(p))chown and chgrp: changing ownership
Usually needs sudo:
sudo chown wanjiku report.txt # change owner
sudo chown wanjiku:developers report.txt # owner and group
sudo chgrp developers report.txt # group only
sudo chown -R www-data:www-data /var/www/site # recursiveSafe permissions for a website
On Ubuntu with Apache or Nginx, the web server runs as the user www-data. A typical safe setup:
sudo chown -R deploy:www-data /var/www/site # you own it, web server's group
sudo find /var/www/site -type d -exec chmod 755 {} \; # folders
sudo find /var/www/site -type f -exec chmod 644 {} \; # files
sudo chmod 640 /var/www/site/config.php # secrets: owner rw, group read, others nothing
sudo chmod -R 775 /var/www/site/uploads # only where the app must writeSSH key permissions
SSH refuses to use keys that others can read:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/id_ed25519.pubumask: default permissions
New files and folders get default permissions based on the umask:
umask # often 0022 (or 0002)With umask 022, new files get 644 and new folders get 755 (the umask "removes" write for group and others). Set a stricter default for your session with umask 077 (files 600, folders 700).
Special permissions
| Bit | Example | Effect |
|---|---|---|
setuid (s in owner x) | /usr/bin/passwd (-rwsr-xr-x) | Program runs with the owner's privileges (lets users change their own password) |
| setgid on a directory | chmod g+s shared/ | New files inherit the folder's group: great for team folders |
sticky bit (t) | /tmp (drwxrwxrwt) | Everyone can create files, but only owners can delete their own |
Attackers look for unusual setuid programs, so security audits check them: find / -perm -4000 -type f 2>/dev/null.
Troubleshooting "Permission denied"
- Who am I?
whoamiandid(shows your groups). - Who owns it and what are the permissions?
ls -l fileandls -ld folder. - Check every folder in the path needs
xfor you:namei -l /var/www/site/index.htmlshows them all. - Running a script? It needs
x(chmod +x script.sh) or run it withbash script.sh. - Fix with the least access needed (add to a group, chown, or chmod a specific file), not 777.
Think about it: A website shows "403 Forbidden" for images in /var/www/site/images. ls -l shows the images as -rw------- deploy deploy. What's wrong and how do you fix it?Show answer
Only the owner (deploy) can read the images; the web server (www-data) is "others" and has no read permission. Fix: chmod 644 the image files (and make sure folders are 755), or set the group to www-data with group read.
Summary
- Every file has an owner, a group and others, each with read, write and execute.
- On directories, r lists, w creates/deletes inside, x lets you enter.
- chmod uses symbols (
u+x,go-w) or numbers (r=4, w=2, x=1): 644 files, 755 folders/scripts, 600 secrets. - chown/chgrp change ownership; web servers on Ubuntu run as www-data.
- Avoid 777; understand umask and special bits; troubleshoot with whoami, id, ls -l and namei -l.
Check yourself
What number represents rwx?
Show answer
7
What chmod number gives rw-r--r--?
Show answer
644
What permission number should a private SSH key have?
Show answer
600
Which user does the web server run as on Ubuntu?
Show answer
www-data
Which command changes a file's owner?
Show answer
chown
On a directory, which permission lets you cd into it? (one letter)
Show answer
x