Skip to content
Velloreth
Developer Tools 6 min read

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

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.

BitValue
read4
write2
execute1

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.

ModeMeaning
644owner read/write, group read, others read
755owner read/write/execute, group read/execute, others read/execute
600owner read/write only
700owner read/write/execute only
777everyone 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.

umaskNew fileNew directory
022644755
002664775
077600700

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.

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.

Frequently asked questions

What does chmod 755 mean?
It sets the mode to rwxr-xr-x: the owner can read, write and execute; the group can read and execute; others can read and execute. The digits are 7 (4+2+1) for the owner, 5 (4+1) for the group, and 5 for others. It is the common mode for a directory or an executable script.
What is the difference between chmod and chown?
chmod changes the permission bits, which control who can read, write or execute a file. chown changes the owner and optionally the group, which controls which set of permission bits applies. A file can be readable by its owner and not by anyone else, and changing the owner changes who that is.
What does umask do?
umask sets the permission bits that are removed from the default when a new file or directory is created. A umask of 022 means a new file gets 644 and a new directory gets 755, because the write bit for group and others is removed. It is a per-process setting, usually set in a shell profile.
What is the sticky bit?
The sticky bit on a directory means that only the owner of a file can delete or rename it, even if the directory is writable by others. It is what makes /tmp safe: anyone can create a file there, but only the owner can remove it. It is set with chmod +t.
What is setuid?
The setuid bit on an executable means the program runs with the permissions of the file's owner rather than the user who ran it. It is how a program lets an ordinary user do a privileged operation, such as passwd changing a password. It is a security risk and should be used sparingly.
Why can't I execute my script?
The execute bit is not set, or the filesystem is mounted with the noexec option. Check the mode with ls -l and add the execute bit with chmod +x. If the mode is correct, check the mount options with mount or findmnt.