Every file and directory on Linux, macOS and other Unix-like systems has a mode that says who can read, change and run it. You see it as -rwxr-xr-x in ls -l and set it with chmod 755. Both notations encode the same nine bits plus three special ones, and once you can translate between them you can fix most "Permission denied" errors without guessing.
Step by step
- Open the chmod calculator.
- Tick Read, Write and Execute for Owner, Group and Others. Alternatively, type an octal mode such as
644or4755in Octal, or a symbolic mode asls -lprints it, such asrwxr-xr-x, in Symbolic. The other fields update to match. - Tick Setuid (4000), Setgid (2000) or Sticky (1000) if you need a special bit.
- Read the summary of who can read, write and execute.
- Enter your file name and copy the command. You get a numeric form such as
chmod 755 deploy.shand a symbolic form such aschmod u=rwx,g=rx,o=rx deploy.sh.
Read, write and execute, for three classes of user
Permissions are granted to three classes:
- Owner (
u): the user who owns the file. - Group (
g): members of the file's group. - Others (
o): everyone else.
Each class gets three permissions. What they mean depends on whether the target is a file or a directory:
| Permission | On a file | On a directory |
|---|---|---|
r read |
Read the contents | List the names of entries |
w write |
Change the contents | Create, delete and rename entries (needs x too) |
x execute |
Run it as a program | Enter it and access entries by name |
This explains two surprises: deleting a file depends on write permission on its directory, not on the file, and a directory with r but not x shows names you cannot open. Directories almost always have x wherever they have r.
In ls -l output the first character is the file type (- for a regular file, d for a directory, l for a symbolic link), followed by three groups of three:
-rw-r--r-- 1 ada staff 1204 Nov 23 09:14 notes.txt
drwxr-xr-x 5 ada staff 160 Nov 23 09:10 project
Octal notation
Each permission has a value: read is 4, write is 2 and execute is 1. Add them for each class and you get one digit per class, in the order owner, group, others.
| Digit | Permissions | Symbolic |
|---|---|---|
| 7 | read, write, execute | rwx |
| 6 | read, write | rw- |
| 5 | read, execute | r-x |
| 4 | read | r-- |
| 0 | none | --- |
So 754 is rwx for the owner, r-x for the group and r-- for others.
The modes you will use most:
| Mode | Symbolic | Typical use |
|---|---|---|
| 644 | rw-r--r-- |
Ordinary files: web pages, configuration, documents |
| 755 | rwxr-xr-x |
Programs, scripts and directories |
| 600 | rw------- |
Private files: SSH private keys, files containing secrets |
| 700 | rwx------ |
Private directories, such as ~/.ssh |
| 640 | rw-r----- |
Files a service group needs to read, such as logs |
| 775 | rwxrwxr-x |
Shared directories where a team group writes |
644 versus 755
The only difference is the execute bit. Use 644 for files that are read but never run, and 755 for programs and for directories, which need x to be entered. A shell script that fails with "Permission denied" when you run ./deploy.sh usually just needs chmod 755 deploy.sh, or chmod u+x deploy.sh to add execute for the owner alone.
New files are filtered by the umask: the common 022 removes write for group and others, so new files usually get 644 and directories 755.
Symbolic mode changes
Symbolic modes change specific bits and keep the rest, which is safer than an octal mode when you want to change one thing:
chmod u+x script.sh # add execute for the owner
chmod g-w report.csv # remove write for the group
chmod o= secrets.env # remove all permissions for others
chmod a+r index.html # add read for everyone (a = all)
chmod u=rw,go=r notes.txt # set exactly 644
Warning
chmod -R 755 directorymakes every file inside executable, which is wrong for ordinary files. Set directories and files separately:find directory -type d -exec chmod 755 {} +andfind directory -type f -exec chmod 644 {} +.
Setuid, setgid and the sticky bit
A fourth, leading octal digit holds three special bits:
- Setuid (4): an executable runs with the privileges of the file's owner instead of the user who started it.
/usr/bin/passwduses this to update the password database. Shown assin the owner's execute position:rwsr-xr-xis 4755. - Setgid (2): an executable runs with the file's group. On a directory, new files created inside inherit the directory's group, which is useful for shared project folders. Shown as
sin the group's execute position:rwxrwsr-xis 2775. - Sticky (1): on a directory, only a file's owner, the directory's owner or root can delete or rename files in it, even if others have write permission.
/tmpis the classic example at 1777, shown asrwxrwxrwt.
A capital letter means the special bit is set without the matching execute bit: rwSr--r-- is 4644, a setuid bit with no effect, and usually a mistake.
Setuid programs are a common target for privilege escalation, so avoid setting it unless you know exactly why. Linux ignores setuid on #! scripts.
Note that GNU chmod keeps a directory's setuid and setgid bits when given a mode like 755; clear them explicitly with chmod g-s directory.
Why chmod 777 is risky
777 gives every user on the system read, write and execute permission. Anyone, including a compromised service account such as a web server process, can change or replace the file, so a script can be made to run someone else's code the next time you execute it.
777 seems to fix "Permission denied" because it removes every check. A web server answering 403 Forbidden for a file it cannot read is a typical case. The real fix is almost always one of these:
- Change the owner or group with
chownso the right user or service owns the file. - Add only the missing permission, such as
g+wfor a group that needs to write. - For shared directories, use a group with setgid (2775).
Before applying a mode from a deployment script, paste it into the calculator and read the summary. The number base converter shows how an octal mode maps to binary bits, and how to calculate a subnet covers the networks those servers sit on.