Tolz.me
LinuxchmodPermissionsShell

chmod 755 vs 644 (and 600, 700, 777): Linux File Permissions Explained

What the owner, group and others bits mean, how 644 and 755 are built from the r, w and x values, how directories differ from files, and which mode to pick.

Tolz.me team9 min read
Illustration of folders secured with padlocks beside a terminal window showing file permissions

Two numbers decide who can read a file and who can run it. The owner, the group and everyone else each get one digit from 0 to 7, and 644 and 755 are just those digits written out. Once read is 4, write is 2 and execute is 1, the two modes stop being magic and start being arithmetic.

Three classes, three permissions

  • Owner (u): the user who owns the file.
  • Group (g): the users who belong to the file's owning group.
  • Others (o): every other user on the system.
  • Read (r): open a file and see its contents. On a directory, list the names inside it.
  • Write (w): change a file's contents. On a directory, create, delete and rename the entries inside it.
  • Execute (x): run a file as a program. On a directory, enter it and reach the files inside.

Wikipedia's write-up of file system permissions makes one point worth repeating: the effective permissions are the permissions of the class a user falls into first, in the order user, then group, then others. A user who owns a file uses the owner bits even when they are also in the owning group.

Reading an ls -l listing

The long listing prints the file type, then the three permission groups, then ownership and size.

-rwxr-xr-x  1 alice  www-data  4096  Oct 21 09:14  deploy.sh

Take it in pieces:

  • The leading dash is the file type. A dash is a regular file, d is a directory, l is a symbolic link, c is a character device, b is a block device.
  • `rwx` is what the owner may do.
  • `r-x` is what the group may do.
  • `r-x` is what everyone else may do.
  • `alice` is the owner, `www-data` is the group, `4096` is the size in bytes, then the timestamp and the name.

In this line the owner can read, change and run the file. The group and everyone else can read and run it but cannot change it. When a setuid, setgid or sticky bit is present, the third character of a group changes, so s or S takes the place of x in the owner and group columns, and t or T takes its place in the others column.

The octal math: 4, 2, 1

Each permission is a single bit and each bit has a value. Read is 4, write is 2, execute is 1. The digit for a class is the sum of its bits.

600  -rw-------  owner can read and write
640  -rw-r-----  the group can also read
644  -rw-r--r--  everyone can read
700  -rwx------  owner can do everything
755  -rwxr-xr-x  everyone can read and traverse
777  -rwxrwxrwx  everyone can do everything

Work through 644. The owner digit is 6, which is 4 plus 2, read plus write. The group digit is 4, read only. The others digit is 4, read only. So 644 means the owner can edit the file and everyone else can only read it.

755 uses 7 for the owner, which is 4 plus 2 plus 1, so read, write and execute. The group and others digits are 5, which is 4 plus 1, so read and execute. That is the standard mode for a program anyone can run and nobody but you can change.

Files and directories store the same letters

The bits are stored the same way for both, but the same letter does different work on a directory.

  • r on a directory lets you list the names of the files inside it.
  • w on a directory lets you create, delete and rename files inside it.
  • x on a directory lets you enter it and reach the files inside. The chmod(2) manual page calls this search permission: entries within the directory can be accessed.

Two rules follow. Without x on a directory, w does nothing useful, because you still cannot reach the entries. With r but no x, you can see the names in the listing but opening them is a guessing game, as Wikipedia puts it.

This is why web servers want 755 on directories and 644 on the files inside them. The server needs x to walk into the directory and r to read the files. A 644 directory is a dead end: the names are visible and nothing inside can be opened.

Why 777 is a bad idea

777 gives read, write and execute to every account on the machine, including accounts that do not exist yet. On a shared host, any local user can read your files, change them and delete them. On a directory, any local user can drop a file into it.

The dangerous part is what happens next. If a service account or a more privileged user later runs something from that directory, the code is already under someone else's control.

There is one common exception, and it explains a mode you will see on every Linux box:

drwxrwxrwt  18 root  root  4096  /tmp

/tmp is usually 1777, world writable with the sticky bit set. The sticky bit is what makes it safe. In a directory with the sticky bit, a user can only remove or rename files they own, which stops one user from deleting another user's uploads. World writable with a sticky bit is a deliberate pattern. World writable without one is 777.

Symbolic chmod

The octal form replaces the whole mode. The symbolic form changes specific bits, which is safer when you only want to add one thing.

The format is who, then the operator, then the permissions. Who is u for the owner, g for the group, o for others, and a for all three. The operator is + to add, - to remove, or = to set.

  • `chmod u+x` adds execute for the owner only.
  • `chmod g-w` removes write for the group.
  • `chmod o=` clears every permission for others. The = operator sets the permissions you list and removes the ones you do not.
  • `chmod u=rw,go=r` sets the owner to read plus write and the group and others to read, which is 644.
  • `chmod a+x` adds execute for everyone.

If you leave out the who, the change applies to all three classes and the umask decides which of them actually change. Writing out the class, as in u+x, is the habit worth keeping.

umask: why new files are 644 and new directories are 755

The umask is a set of permissions that get turned off when a file or directory is created. The umask(2) manual page gives the typical default as 022, which is write for the group and write for others.

Files are created with a request of 0666 and directories with 0777. The umask is subtracted from that request bit by bit:

files:       0666 & ~022 = 0644
directories: 0777 & ~022 = 0755

That is why a text editor that saves a new file leaves it at 644, and a new directory lands at 755, without anyone typing chmod. If you want new files to stay private to your group, run umask 027 and the same file comes out as 640. Check your current value with the umask command. Since Linux 4.7, the umask of any running process is visible in the Umask field of /proc/pid/status.

setuid, setgid and the sticky bit

Three extra bits live in a fourth octal digit in front of the three permission digits.

  • setuid, 4000: a program with this bit runs with the privileges of the file's owner. The chmod(2) manual page describes it as setting the process effective user ID on exec.
  • setgid, 2000: on a directory, new files and directories created inside it inherit the directory's group. On an executable it sets the process group ID on exec.
  • sticky, 1000: on a directory, it stops users from removing or renaming files owned by other users even when they have write permission on the directory. That is the /tmp rule above.

Set them with the leading digit:

chmod 4755 script       # setuid
chmod 2775 shared-dir  # setgid on a directory
chmod 1777 tmp         # sticky bit

In ls -l output the setuid bit shows as s in the owner column, setgid as s in the group column and sticky as t in the others column. A capital letter means the execute bit is off in that position, so -rwsr-Sr-t is a setuid file that the owner can run and the group cannot.

Modes for a web server

A typical deployment uses a small set of modes. The web server needs to read files and walk through directories, and it rarely needs to change them.

  • Static files: 644. The server reads them and your deploy user can replace them.
  • Directories holding static files: 755, so the server can list and traverse them.
  • Secrets: 600 for the file and 700 for its directory. A config file with a database password or a private key should not be readable by every account on the box.
  • Upload or cache directories the application writes to: 775 with the group set to the application group, so the app user can write without giving write access to everyone.
  • Deploy scripts and admin tooling: 750 or 700, so only your group can run them.
  • Executables nobody should touch: 755 for the program, with the directory above it at 755 rather than 777.

The pattern behind all of it: give every account the least it needs, keep write access off the directories a server reads from, and keep 777 away from anything that holds real data.

Sources

  • GNU coreutils manual page, chmod(1): the symbolic and numeric mode formats, the u, g, o and a classes, the umask interaction and the sticky bit on directories. man7.org
  • Linux manual page, chmod(2): the permission bit values, the setuid, setgid and sticky bit definitions, and the execute or search permission on directories. man7.org
  • Linux manual page, umask(2): how the mask is applied to newly created files and directories, and the typical default of 022. man7.org
  • File-system permissions, Wikipedia: the permission classes, the behaviour of directories, and the symbolic and octal notations. en.wikipedia.org

Tools used in this guide

Open a tool, make the change, and copy the result. No account needed.