How chmod permissions work: 755, 644 and sticky bit
You upload a script and it will not run, or a web page answers “permission denied”. On Unix-like systems the fix usually involves chmod, the command that changes access permissions on files and directories. Read, write and execute are set separately for the owner, the group and everyone else, and on a directory the same three permissions mean something different.
Reading 755 and 644 as sums
In a numeric mode, read is 4, write is 2 and execute is 1. Add up what you want to grant and you get one digit; the three digits apply to the owner, the group and others, in that order. So 7 is 4+2+1, 5 is 4+1 and 6 is 4+2. That makes 755 the same as rwxr-xr-x: the owner can read, write and execute, while the group and others can read and execute. Mode 644 gives the owner read and write and everyone else read only.
The symbolic form changes one permission and leaves the rest alone. chmod u+x script.sh gives the owner permission to execute the file, which is often all a script that will not start needs.
644 is often suitable for an ordinary public file, but its suitability depends on the application. Do not treat a familiar number as a universal security setting.
What read, write and execute mean on a directory
On a directory, read allows listing the names inside it, write allows changing its entries, and execute allows traversing it to reach a path you already know. One consequence surprises many people. Removing a file normally depends on the permissions of its parent directory, not on write permission for the file itself. A file set to 444 can therefore still be removed by someone with sufficient access to the directory. The GNU Coreutils manual on mode structure explains this distinction.
The same logic explains a common puzzle. If access fails even though the final file looks readable, inspect every parent directory on the path, and check which user the service actually runs as.
Sensible permissions for a website directory
Imagine a directory of HTML files that a web server must read but only its owner may update. A reasonable starting point is 755 for directories and 644 for ordinary files. You still have to account for the service account, its groups and your hosting provider’s rules, and private configuration files may need tighter access.
Setting everything to 777 to silence an error grants every local user broad rights to modify your files and can introduce a security problem. Before you change anything, look at the current owner and permissions with stat and ls -l. Often the real fault is the owner, not the mode.
The fourth digit: setuid, setgid and the sticky bit
An extra octal digit in front of the usual three sets the special bits: 4 for setuid, 2 for setgid and 1 for sticky. For example, 4755 enables setuid on an executable; 455 does not, because it is simply a three-digit mode. A setuid program runs with its owner’s privileges, so use the bit carefully: a flaw in the program then operates with those privileges.
On a directory, setgid can make new entries inherit the directory’s group, which helps a team share files. A sticky bit on a shared directory such as /tmp restricts who may remove or rename another user’s files.
Before you run a recursive change
chmod -R applies one mode to a whole tree. Review the exact tree first, because ordinary files, executables and directories rarely all need identical modes. A numeric mode that suits directories makes every data file executable, and one that suits files locks users out of the directories.