Linux File Permissions Explained: chmod, chown and umask
Read, write, execute for owner, group and others. Learn the octal notation, the special bits, and how umask sets defaults.
By Velloreth
The three sets
Every file has a mode that describes three sets of permissions.
-rwxr-xr-x 1 user group 1234 Jan 1 12:00 script.sh
│└┬┘└┬┘└┬┘
│ │ │ └── others
│ │ └───── group
│ └──────── owner
└────────── file type (- file, d directory, l symlink)
The owner is the user who owns the file. The group is the group that owns it. Others is everyone else.
Each set has three bits: read (r), write (w), execute (x). A dash means the bit is not set.
The octal notation
Each set maps to a digit, and each bit has a value.
| Bit | Value |
|---|---|
| read | 4 |
| write | 2 |
| execute | 1 |
Add the values for the set. rwx is 7, rw- is 6, r-x is 5, r-- is 4, -wx is 3, -w- is 2, --x is 1, --- is 0.
The three digits are owner, group, others.
| Mode | Meaning |
|---|---|
| 644 | owner read/write, group read, others read |
| 755 | owner read/write/execute, group read/execute, others read/execute |
| 600 | owner read/write only |
| 700 | owner read/write/execute only |
| 777 | everyone read/write/execute |
644 is the common mode for a file, and 755 for a directory or an executable. 600 is the mode for a private key, and 700 for a private directory.
chmod
Symbolic mode names the set and the change.
chmod u+x script.sh # add execute for the owner
chmod g-w file.txt # remove write for the group
chmod o=r file.txt # set others to read only
chmod a+r file.txt # add read for everyone
The letters are u (owner), g (group), o (others), a (all). The operators are +, - and =.
Octal mode sets the whole mode at once.
chmod 644 file.txt
chmod 755 script.sh
chmod 600 ~/.ssh/id_ed25519
Recursive applies to a directory and its contents.
chmod -R 755 public/
The caution: a recursive chmod on a directory sets the same mode on files and directories, which is usually wrong. A directory needs the execute bit to be traversable, and a file usually does not need it.
chown and chgrp
chown changes the owner, and optionally the group.
chown alice file.txt
chown alice:developers file.txt
chown -R www-data:www-data /var/www
chgrp changes only the group.
chgrp developers file.txt
The caution: a recursive chown on a system directory can break a service. Check the target before running it.
umask
umask sets the bits removed from the default when a new file or directory is created.
umask
# 0022
A file is created with 666 minus the umask, and a directory with 777 minus the umask.
| umask | New file | New directory |
|---|---|---|
| 022 | 644 | 755 |
| 002 | 664 | 775 |
| 077 | 600 | 700 |
A umask of 077 is the strict choice, and it is what a security-conscious setup uses: a new file is readable only by its owner.
It is per-process, and it is usually set in a shell profile.
# In ~/.bashrc
umask 022
The special bits
The setuid bit on an executable means the program runs with the permissions of the file’s owner.
chmod u+s /usr/bin/program
# Shown as -rwsr-xr-x
It is how a program lets an ordinary user do a privileged operation. passwd is setuid root, which is how a user can change their own password in a root-owned file.
It is a security risk. A setuid program with a bug is a privilege escalation. Use it sparingly, and audit the setuid binaries on a system.
The setgid bit on an executable means the program runs with the file’s group. On a directory, it means a new file inherits the directory’s group rather than the creator’s, which is useful for a shared directory.
chmod g+s /shared/project
The sticky bit on a directory means only the owner of a file can delete or rename it.
chmod +t /shared/uploads
# Shown as drwxrwxrwt
It is what makes /tmp safe. Anyone can create a file there, and only the owner can remove it.
Permissions in a real deployment
A permission problem in production is usually a mismatch between the user that runs the process and the user that owns the files. A web server that runs as www-data cannot write to a directory owned by root, and the error is a permission denied that appears in the log rather than at deploy time. The pattern that avoids it is to decide the owner first: the service user owns the application directory, the deploy process runs as that user or changes the ownership after a copy, and the configuration file is readable by the service and writable only by the deploy user. A second common case is a shared directory where several users must write, which is what the setgid bit on the directory solves: a new file inherits the directory’s group rather than the creator’s, so every member of the group can read it. A third is a secret file, which should be mode 600 and owned by the service user, because a secret that another user can read is a secret that has leaked.
Common mistakes
chmod 777 to fix a permission error. It makes the file writable by everyone, which is a security problem. Find the actual cause instead.
A recursive chmod that sets the execute bit on every file. A data file should not be executable.
A private key with a mode other than 600. SSH refuses to use a key that others can read, and the error is explicit.
A web directory owned by root. The application cannot write to it. Own it by the service user.
A umask of 000. Every new file is world-writable.
A setuid binary that is not needed. It is a privilege escalation waiting to happen.
Forgetting that a directory needs the execute bit to be entered. A directory with mode 644 cannot be traversed, even if it is readable.
A quotable summary
A Linux file has three sets of read, write and execute bits for the owner, the group and others, and the octal notation maps each set to a digit where read is 4, write is 2 and execute is 1. chmod changes the mode, chown changes the owner, and umask sets the bits removed from the default for a new file. The setuid, setgid and sticky bits have specific uses: setuid for a privileged program, setgid for a shared directory, and sticky for a directory where only the owner can delete a file.
Related reading
For the shell around this, see linux-command-line-basics and for the process side, linux-process-management. For the broader context, ssh-and-keys-guide and zero-trust-for-small-teams.