ToolsWhisper

chmod 755 vs 644: What the Numbers Mean and When to Use Each

755 is for directories and programs, 644 for ordinary files. How the three digits are built, why those two are the defaults, and the find command to fix a tree.

Frank4 min read

755 and 644 are the two permission modes you will see most on a Linux or macOS system. The short version: 644 for files, 755 for directories and programs. The reason they differ is the execute bit, and it means something different on a file than on a directory.

How the digits are built

A mode is three digits, one each for the owner, the group and everyone else. Each digit is the sum of the permissions it grants:

Permission Value
Read 4
Write 2
Execute 1

So 7 is 4+2+1, read, write and execute. 5 is 4+1, read and execute. 6 is 4+2, read and write, and 4 is read only.

Mode Owner Group Others Symbolic
755 read, write, execute read, execute read, execute rwxr-xr-x
644 read, write read read rw-r--r--

The owner can change the thing in both cases. Everyone else can look at it, and under 755 they can also run it or enter it.

Reading ls -l

-rwxr-xr-x  1 deploy deploy  1204 Oct 12 09:14 backup.sh
drwxr-xr-x  2 deploy deploy  4096 Oct 12 09:10 assets

The first character is the type: - for a file, d for a directory, l for a symbolic link. The next nine characters are three groups of three, for owner, group and others. To see the number instead of the letters, use stat -c '%a %n' backup.sh on Linux or stat -f '%Lp %N' backup.sh on macOS.

Why directories need the execute bit

On a file, execute means "run this as a program". An HTML page or an image has no business being executable, so 644 is correct for them.

On a directory, the bits mean something else:

  • Read lets you list the names inside it.
  • Write lets you create, delete and rename entries in it.
  • Execute lets you enter it and reach the files inside by name.

Without execute, a directory cannot be entered, even if you can read it. That is why a web directory set to 644 produces "Forbidden" errors, and why directories are almost always 755.

Why these two are the defaults

New files and directories start from a base and have the process's umask removed. Files start at 666 and directories at 777, and the common umask is 022, which strips write from group and others:

  • Files: 666 minus 022 gives 644.
  • Directories: 777 minus 022 gives 755.

Nobody picked 644 and 755 for files and directories. They fall out of the arithmetic.

One correction to the shortcut: the umask removes bits, it does not subtract numbers. For 022 the two give the same answer, which is why "666 minus 022" works. With a umask of 027, subtraction would give 637, which is not even a sensible mode. Removing the bits gives 640: owner read and write, group read, others nothing.

Fixing a whole tree

Running chmod -R 755 over a project makes every data file executable. Running chmod -R 644 makes every directory un-enterable. Set them separately:

find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +

Or in one go, with the capital X, which grants execute only to directories and to files that are already executable:

chmod -R u=rwX,go=rX .

Scripts and the execute bit

A script needs the execute bit to be run as ./backup.sh, so a mode like 755 (or 750, or 700 for a private one) is right. A 644 script still runs if you hand it to its interpreter, as in bash backup.sh, because the interpreter only needs to read the file. When a script fails with "Permission denied", a missing execute bit is the usual reason.

A typical website

For most sites, files should be readable by the web server and writable only by the account that deploys them: 644 for files and 755 for directories, owned by the deploy user. A folder the server must write to, such as uploads, should be owned by the server's user, or be group-writable (775) through a shared group. It should not be 777.

When to use something else

  • 600 (rw-------) for private keys and credentials. SSH refuses to use a private key that others can read.
  • 700 (rwx------) for private directories such as ~/.ssh.
  • 664 and 775 when a team shares files through a common group.
  • 777 almost never. It lets any user on the machine modify the file. If you reach for it to make an error go away, you are masking a problem with ownership, not fixing it. Find the user or group that needs access and give it to them.

A fourth digit

You will sometimes see four digits, such as 4755 or 1777. The extra leading digit holds three special bits: 4 is setuid (run as the file's owner), 2 is setgid (run as the file's group, or, on a directory, new files inherit its group) and 1 is the sticky bit. The sticky bit is why /tmp is 1777: everyone can create files there, but only a file's owner can delete or rename it. If you do not recognise a leading digit, find out what it does before you copy the command.

Check a mode quickly

If you are looking at a number and want to know what it grants, or you know what you want and need the number, the chmod calculator does both ways. Tick read, write and execute for owner, group and others, and it shows the octal, the symbolic form, the command, and a plain-English description. It also warns when a mode is risky, such as anything world-writable.

The octal notation is just base 8 grouped by three bits, if you want to see why: the number base converter shows the same value in binary, octal and decimal side by side.

Tools used in this article

Read next