Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Read This First

A recursive chmod changes thousands of files in a second, and there is no undo. The only way back is a backup or a snapshot taken before. Run against the wrong path, such as /, /etc, /usr, or a whole home tree, it can stop sudo and SSH from working and leave the server unbootable.

  • Check the path: type the full path, never a variable or a path that depends on the current folder, and look at it with ls -ld first.
  • Take a snapshot: on a cloud server, take one in your provider's control panel before large changes. Snapshot vs backup explains the difference.
  • Add a guard: chmod does not refuse to work recursively on / by default. With --preserve-root it does.
  • Never 777: chmod -R 777 lets every user and every process on the server change and replace your files. It is not a fix for anything.

The Basic Command

ls -ld /var/www/example.com
sudo chmod -R --preserve-root 755 /var/www/example.com

-R applies the mode to the folder and everything below it. The catch: 755 makes every file executable as well, which ordinary files such as HTML, images, and configuration should not be. That is why the next method is the one to use.

Folders 755, Files 644

Folders need the execute bit so they can be entered; files normally do not. Set them separately with find:

sudo find /var/www/example.com -type d -exec chmod 755 {} +
sudo find /var/www/example.com -type f -exec chmod 644 {} +

-type d selects directories and -type f regular files. The + passes many paths to each chmod call, which is much faster than one call per file. To see what would be changed before you change it, run the find part alone:

find /var/www/example.com -type d | head
find /var/www/example.com -type f | wc -l

What the Numbers Mean

ModeOwnerGroup and others
755read, write, executeread, execute
644read, writeread
775read, write, executegroup all three; others read, execute
600read, writenothing

Capital X: One Command for Both

In symbolic mode, a capital X sets execute only on folders and on files that are already executable for someone:

sudo chmod -R u=rwX,go=rX /var/www/example.com

The result matches 755 for folders and 644 for normal files, while scripts that were executable stay executable.

Symbolic Modes

  • chmod -R g+w uploads: adds group write and keeps everything else.
  • chmod -R o-rwx private: removes all access for others.
  • chmod -R a-x docs: removes execute from everyone; this also locks folders, so use it only on files with find.

Symbolic modes change one part of the permissions; numbers replace all of them.

A Typical Web Root

For a site whose files belong to a deploy user and are read by the web server, 755 and 644 are enough. Only folders the application writes to, such as uploads or cache, need group write (775 and 664) together with the right group owner, which chown sets. Keep configuration files with database passwords at 640 or 600. A 403 error after a permission change usually means a folder lost its execute bit; see 403 Forbidden in Nginx.

Check the Result

stat -c '%a %n' /var/www/example.com/index.html
find /var/www/example.com -perm -o+w

The second command lists anything still writable by others; on a web root it should print nothing.

If You Ran It on the Wrong Path

Stop and do not reboot or close your SSH session. A few system folders can be repaired by hand, but a full recovery means restoring from the snapshot or backup. On a large system path, restoring the snapshot is usually faster and safer than trying to rebuild every mode.

Was this answer helpful?
Loading...