nicetool.dev logo
#linux#security#devops

Linux File Permissions and chmod: 755, 644, 600 and When to Use Each

How Linux read, write and execute bits work for files and directories, how octal numbers like 755 and 644 are built, what umask, setuid and the sticky bit do, and safe defaults for web servers and SSH keys.

6 min read

chmod 777 is probably the most copied command on Stack Overflow. It also turns many small problems into security incidents. Linux permissions take about ten minutes to understand properly, and after that you will rarely need to guess again. This article covers the model, the numbers, the special bits, and safe defaults for the situations developers meet most often.

Reading ls -l

-rwxr-xr-- 1 deploy www-data 2048 Oct 12 09:30 backup.sh

The first column has 10 characters:

  • Character 1 is the type: - file, d directory, l symbolic link.
  • Characters 2-4 are permissions for the owner (deploy): rwx.
  • Characters 5-7 are for the group (www-data): r-x.
  • Characters 8-10 are for others, meaning everyone else: r--.

Linux checks only one of these sets: the owner set if you own the file, otherwise the group set if you are in the group, otherwise the others set. If you own a file with permissions ---rwxrwx, you cannot read it, even though everyone else can.

What r, w and x mean

For files the meaning is intuitive:

  • r: read the contents.
  • w: change the contents.
  • x: run it as a program or script.

For directories the meaning is different, and this causes most confusion:

  • r: list the names inside (ls).
  • w: create, delete and rename entries inside. Deleting a file depends on the directory's w, not on the file's permissions.
  • x: enter the directory and access entries by name (cd, opening dir/file).

A few consequences:

  • A directory with r but no x lets you see file names but not open the files.
  • A directory with x but no r lets you open a file if you already know its exact name, but not list the contents.
  • A read-only file inside a directory you can write to can still be deleted (unless the sticky bit is set, see below).

Octal numbers: 4, 2, 1

Each permission has a value: r = 4, w = 2, x = 1. Add them for each of the three sets:

Digit Bits Meaning
7 rwx 4+2+1
6 rw- 4+2
5 r-x 4+1
4 r-- 4
0 --- none

So chmod 754 backup.sh means owner rwx (7), group r-x (5), others r-- (4), which is exactly the listing above. To check the current number, use stat -c '%a %n' file on Linux (macOS uses stat -f '%Lp %N' file).

The values you actually need

Mode Symbolic Typical use
644 rw-r--r-- Normal files: HTML, CSS, images, source code
755 rwxr-xr-x Directories, executable scripts and binaries
600 rw------- Secrets: SSH private keys, .env, credentials
700 rwx------ Private directories such as ~/.ssh
640 rw-r----- Config readable by a service group, like a web server
775 / 664 Shared directories and files for a team group
777 rwxrwxrwx Almost never

Why 777 is dangerous

With 777, any user and any process on the machine can modify the file. On a web server that includes every PHP script, every compromised plugin and every other site on shared hosting. If an attacker can write to a file that your server executes, they can run their own code. When something "only works with 777", the real problem is almost always ownership: the process runs as a different user than the file owner. Fix it with chown (or by adding the process user to the right group), not with 777.

Symbolic mode: changing one bit at a time

Numbers set all permissions at once. Symbolic mode changes only what you name:

chmod u+x deploy.sh       # owner may execute
chmod g-w config.yml      # remove write from group
chmod o= secrets.txt      # others: no permissions at all
chmod a+r public.html     # everyone may read
chmod u=rw,go=r file.txt  # same as 644

Here u is the user (owner), g the group, o others and a all.

The capital X trick for recursive changes

chmod -R 755 site/ makes every file executable, which is usually wrong. chmod -R 644 site/ removes x from directories, which makes them impossible to enter. Use one of these instead:

# directories 755, files 644
find site/ -type d -exec chmod 755 {} +
find site/ -type f -exec chmod 644 {} +

# or in one command: capital X adds x only to directories
# (and to files that are already executable)
chmod -R u=rwX,go=rX site/

umask: why new files are 644

New files are not created with 777. The program asks for a mode (usually 666 for files and 777 for directories), and the umask removes bits from it. With the common umask 022:

  • Files: 666 minus 022 → 644
  • Directories: 777 minus 022 → 755

Some systems use 002 for regular users so that group members can write, which gives 664 and 775. Run umask to see yours. Technically the umask is a bit mask, not a subtraction, but for these common values the result is the same.

Special bits: setuid, setgid, sticky

A fourth, leading digit holds three special bits:

Value Bit On files On directories
4 setuid Runs with the owner's privileges (ignored on Linux)
2 setgid Runs with the group's privileges New files inherit the directory's group
1 sticky (ignored) Only the owner can delete or rename their files

Real examples:

  • /usr/bin/passwd is 4755 (-rwsr-xr-x). It is owned by root and has setuid so normal users can update the password database. Every setuid-root binary is a potential privilege escalation, so audit them with find / -perm -4000 -type f 2>/dev/null.
  • /tmp is 1777 (drwxrwxrwt). Everyone can create files, but the sticky bit stops users from deleting each other's files.
  • A shared project folder with 2775 keeps every new file in the team's group, so permissions stay consistent no matter who creates the file.

Safe defaults for common situations

SSH keys

OpenSSH refuses to use a private key that other users can read:

WARNING: UNPROTECTED PRIVATE KEY FILE!
Permissions 0644 for '/home/me/.ssh/id_ed25519' are too open.

Fix it with:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/config
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys

On the server side, sshd (with the default StrictModes yes) also rejects authorized_keys if your home directory or ~/.ssh is writable by others. That is a frequent cause of "key login stopped working".

Web applications

  • Code and static files: 644, directories 755, owned by the deploy user.
  • The web server user (often www-data or nginx) only needs read access to code.
  • Only upload, cache and log directories should be writable by the web server user. Make them group-writable (775 with the web server's group), never 777.
  • Config files with database passwords: 600 or 640, depending on whether the app process runs as the owner or reads through the group.

Git and Docker

Git only stores the executable bit (100644 or 100755), not the full mode. If a script loses its x after cloning on Windows, fix it in the repository with git update-index --chmod=+x script.sh. In Docker, COPY --chmod=755 entrypoint.sh / (BuildKit) sets permissions without an extra layer.

Summary

  • Three sets (owner, group, others) × three bits (r=4, w=2, x=1).
  • On directories, x means "enter" and w means "create or delete entries".
  • 644 for files, 755 for directories, 600/700 for secrets.
  • Fix "permission denied" with the right owner or group, not with 777.
  • Use capital X or find for recursive changes.

If you would rather not calculate octal digits by hand, the chmod Calculator converts between checkboxes, numbers like 2775 and symbolic strings like rwxrwsr-x, and generates the exact chmod command to copy.

Written by

Nguyễn Thành Nam

Full-Stack Engineer · Backend & Platform

Full-stack engineer with 9+ years of experience building high-traffic APIs and platforms with Go, NestJS, PHP (Laravel, Phalcon) and Node.js. Specializes in GraphQL API design, real-time systems and performance optimization, and builds NICETOOL.dev for the developer community.

Try it yourself

Related articles