Linux file permissions cheat sheet

This cheat sheet covers what you need to know about Linux file and directory permissions, from reading ls -l output and numeric values, through chmod, chown, and umask, to special bits, ACLs, and finding risky permissions. You can search for a command, filter by topic, and copy it with one click.

Reading permissions

  • ls -l file.php

    Shows the file's permissions, owner, and group. The first character is the file type, followed by three sets of rwx: for the owner, the group, and all other users. r means read, w means write, x means execute, and a hyphen means the permission is missing.

    Example: -rw-r--r-- 1 user group 2048 Nov 28 10:15 file.php

  • ls -ld /var/www/example/public

    Shows the permissions of the directory itself rather than the list of files inside it. On a directory, r lets you list the files, w lets you create, delete, and rename files, and x lets you enter the directory and access the files in it.

    Example: drwxr-xr-x 5 user group 4096 Nov 28 10:15 /var/www/example/public

  • stat -c '%a %A %U:%G %n' file.php

    Shows the permissions in both numeric and symbolic form, along with the owner, the group, and the file name, on one line.

    Example: 644 -rw-r--r-- user:group file.php

  • id user

    Shows the user's ID and the groups the user belongs to, so you can tell whether the owner, group, or other users' permissions apply to them.

  • - d l

    The first character in the ls -l output shows the file type: a hyphen is a regular file, d is a directory, and l is a symbolic link. Other types are b and c for devices, p for a named pipe, and s for a socket.

  • rwsr-xr-x rwxr-sr-x rwxrwxrwt

    An s in place of x marks the setuid bit (in the owner's set) or the setgid bit (in the group's set), and a t in the last set marks the sticky bit. An uppercase S or T means the special bit is set but the execute permission in that position is missing.

  • -rw-r--r--+

    A + after the permissions means the file has additional ACL permissions, and a dot marks an SELinux security context. To see the ACL permissions, use the getfacl command.

Common numeric values

  • r=4 w=2 x=1

    Each digit in a numeric value is the sum of the permissions for one class of users: read is 4, write is 2, and execute is 1. The first digit is for the owner, the second for the group, and the third for other users. For example, 7 = 4+2+1 (rwx) and 5 = 4+1 (r-x).

  • chmod 644 file.php

    rw-r--r--: read and write for the owner, and read-only for the group and other users. The common setting for website files.

  • chmod 755 /var/www/example/public

    rwxr-xr-x: full access for the owner, and enter and read access for the group and other users. The common setting for website directories and for scripts you run.

  • chmod 600 config.php

    rw-------: read and write for the owner only. Suitable for configuration files with passwords and for private keys.

  • chmod 700 /home/user/scripts

    rwx------: full access for the owner only. Suitable for private directories, such as ~/.ssh.

  • chmod 640 config.php

    rw-r-----: read and write for the owner, read for the group, and no access for other users. Suitable for a configuration file that another service needs to read through the group.

  • chmod 750 /var/www/example/private

    rwxr-x---: full access for the owner, enter and read access for the group, and no access for other users.

  • chmod 775 /var/www/example/shared

    rwxrwxr-x: full access for the owner and the group, and enter and read access for other users. Suitable for a directory that several users in the same group work in.

  • chmod 664 file.txt

    rw-rw-r--: read and write for the owner and the group, and read-only for other users. Suitable for files shared by group members.

  • chmod 400 private.key

    r--------: read-only for the owner, and no access for anyone else. Suitable for files that must not change, such as a private key.

  • chmod 777 file.php

    Caution: rwxrwxrwx lets every user on the server read, change, and run the file. Don't use it on a website, not even for an uploads directory. When a plugin or script fails to write, check the file's ownership first.

chmod in symbolic mode

  • chmod u+x script.sh

    Adds execute permission for the owner only. The letters u, g, o, and a stand for the owner, the group, other users, and everyone, and the + and - signs add or remove a permission.

  • chmod g-w file.php

    Removes write permission from the group without changing the other permissions.

  • chmod o-rwx secret.txt

    Removes all permissions from other users, so only the owner and group members can access the file.

  • chmod u=rw,go=r file.php

    The = sign sets the permissions exactly and replaces whatever was there before. This command is equivalent to chmod 644.

  • chmod a+r file.txt

    Adds read permission for everyone: the owner, the group, and other users.

  • chmod --reference=index.php new.php

    Copies the permissions from one file to another.

  • chmod -v 644 file.php

    Prints a message for every file processed. The -c option prints a message only for files whose permissions actually changed.

Changing permissions for a whole directory

  • find /var/www/example -type d -exec chmod 755 {} +

    Applies 755 to all directories without changing file permissions. Ending with {} + runs chmod on batches of files, so it's faster than {} \; in large directories.

  • find /var/www/example -type f -exec chmod 644 {} +

    Applies 644 to all files without changing directory permissions. Run it together with the previous command to fix the permissions of an entire website.

  • chmod -R u=rwX,go=rX /var/www/example

    Sets permissions for files and directories in a single command. The uppercase X adds execute permission only to directories and to files that are already executable, so directories get 755 and regular files get 644.

  • chmod -R 755 /var/www/example

    Caution: this command gives execute permission to every file too, not just to directories. For a website, use the pair of find commands, one for directories and one for files, or symbolic mode with X.

  • chmod -R g-w /var/www/example

    Removes write permission from the group on all files and directories, without changing the other permissions.

Ownership: chown and chgrp

  • sudo chown user file.php

    Changes the file's owner. Changing the owner requires administrator privileges.

  • sudo chown user:group file.php

    Changes the owner and the group in one command. When no group name follows the colon (user:), the group changes to the user's login group.

  • sudo chown -R user:group /var/www/example

    Changes the owner and the group of the directory and everything in it. The usual fix when files uploaded or created under another user can't be edited.

  • chown :group file.php

    Changes only the group and leaves the owner as is. A file's owner can move it to a group they belong to, even without administrator privileges.

  • chgrp -R group /var/www/example/shared

    Changes the group of the directory and everything in it. The chgrp command is equivalent to changing only the group with chown.

  • sudo chown -h user:group link

    Changes the ownership of the symbolic link itself. Without -h, the command changes the file the link points to.

  • sudo chown -R --from=olduser newuser /var/www/example

    Changes the owner only on files currently owned by olduser and leaves all other files as they are.

  • sudo chown --reference=index.php new.php

    Copies the owner and the group from one file to another.

umask: permissions for new files

  • umask

    Shows the permission mask, which removes permissions from new files and directories: a new file usually starts from 666 and a directory from 777, and the permissions in the mask are removed from them.

    Example: 0022

  • umask -S

    Shows the mask in symbolic form, meaning the permissions new files will get rather than the ones that will be removed.

    Example: u=rwx,g=rx,o=rx

  • umask 022

    The common value: new files are created with 644 and new directories with 755. The change applies only to the current shell session.

  • umask 027

    New files are created with 640 and directories with 750, with no access for other users.

  • umask 077

    New files are created with 600 and directories with 700, so only the owner can access them. Suitable for scripts that create sensitive files.

Special bits

  • sudo chmod u+s /usr/local/bin/tool

    Caution: the setuid bit (4000) makes a program run with its owner's permissions rather than those of the user who runs it. Such a program owned by root can allow a takeover of the server, so set the bit only when there is a proven need. On Linux, the bit has no effect on scripts.

  • chmod g+s /var/www/example/shared

    The setgid bit (2000) on a directory: new files created in it get the directory's group, and new subdirectories also get the bit itself. Useful in a directory that several users work in.

  • chmod 2775 /var/www/example/shared

    A shared directory in numeric form: setgid together with full access for the owner and the group. The extra digit, to the left of the three regular digits, sets the special bits: 4 for setuid, 2 for setgid, and 1 for the sticky bit.

  • chmod +t /var/www/example/uploads

    The sticky bit (1000) on a directory: only the file's owner, the directory's owner, and root can delete or rename a file in it, even when others have write permission on the directory.

  • ls -ld /tmp

    The /tmp directory is usually set to 1777: any user can create files in it, and the sticky bit stops users from deleting other users' files.

    Example: drwxrwxrwt 12 root root 4096 Nov 28 10:15 /tmp

  • chmod g-s /var/www/example/shared

    Removes the setgid bit from a directory. With GNU chmod, a three-digit numeric value such as 755 doesn't clear setuid and setgid on a directory, so remove them in symbolic mode or with a five-digit value, such as chmod 00755.

Extended permissions (ACL)

  • getfacl file.php

    Shows all of the file's permissions, including ACL permissions for additional users and groups.

  • setfacl -m u:deploy:rwx /var/www/example

    Gives another user permissions on the directory without changing its owner and group. ACLs let you grant permissions to several users and groups, beyond the three standard classes.

  • setfacl -m g:developers:rx /var/www/example

    Gives another group enter and read permissions on the directory.

  • setfacl -d -m u:deploy:rwx /var/www/example

    Sets a default ACL on the directory: new files and directories created in it get the permission automatically. When a default ACL is set, the umask doesn't apply to new files in the directory.

  • setfacl -R -m u:deploy:rX /var/www/example

    Applies the permission to the directory and all of its existing contents. The uppercase X gives enter permission on directories and execute permission only to files that are already executable.

  • setfacl -x u:deploy /var/www/example

    Removes the ACL permission of a specific user.

  • setfacl -b file.php

    Removes all additional ACL permissions and keeps the permissions of the owner, the group, and other users. The -k option removes only a directory's default ACL.

  • getfacl file1 | setfacl --set-file=- file2

    Copies all ACL permissions from one file to another.

File attributes: chattr and lsattr

  • lsattr file.php

    Shows the file's special attributes at the file system level. Attributes work in addition to the regular permissions, and not every file system supports all of them.

  • lsattr -d /var/www/example

    Shows the attributes of the directory itself rather than those of the files inside it.

  • sudo chattr +i config.php

    Caution: the i attribute locks the file, so it can't be changed, deleted, or renamed, not even by root, until the attribute is removed. Software updates that try to change a locked file will fail. Only root can set and remove the attribute.

  • sudo chattr -i config.php

    Removes the lock, and the regular permissions once again decide who can change the file.

  • sudo chattr +a /var/log/app.log

    The a attribute only allows appending to the end of the file, not changing or deleting existing content. Useful for log files. Remove it with chattr -a.

Finding risky permissions

  • find /var/www/example -type f -perm -0002

    Finds files that any user on the server can change. A website doesn't need this permission, so change it to 644.

  • find /var/www/example -type d -perm -0002

    Finds directories that any user on the server can write to, such as directories set to 777.

  • find /var/www/example -perm 777

    Finds files and directories set to exactly 777. A - before the value finds files that have all of the permissions in the value, and a / finds files that have at least one of them.

  • sudo find / -xdev -type f -perm -4000

    Finds files with the setuid bit on the root file system. A standard distribution has a small number of such files, such as passwd and sudo, and any unfamiliar file on the list needs checking.

  • sudo find / -xdev -type f -perm /6000

    Finds files with the setuid bit, the setgid bit, or both. The -xdev option limits the search to the file system you start in.

  • sudo find / -xdev -type d -perm -0002 ! -perm -1000

    Finds directories that any user can write to and that lack the sticky bit, so users can delete other users' files.

  • sudo find / -xdev \( -nouser -o -nogroup \)

    Finds files whose owner or group no longer exists on the system, usually leftovers from a deleted user.

  • find /var/www/example ! -user user

    Finds website files that don't belong to the website's user, a common cause of write errors during updates and file uploads.

Recommended website permissions

  • find /home/user/public_html -type d -exec chmod 755 {} +

    Website directories: 755, or 750 when other users don't need access. The uploads directory gets the same permissions, not 777.

  • find /home/user/public_html -type f -exec chmod 644 {} +

    Website files: 644, or 640 when other users don't need access. PHP files don't need execute permission.

  • chmod 600 /home/user/public_html/wp-config.php

    Configuration files with passwords, such as wp-config.php, get 600 or 640. On a server where the website runs under its own user, the WordPress documentation recommends 440 or 400 for wp-config.php.

  • chmod 640 /home/user/app/.env

    An application's .env file holds passwords and keys. Limit it to the owner and the group, and preferably place it outside the website's public directory.

  • chmod 644 /home/user/public_html/.htaccess

    The .htaccess file must be readable by the web server, and only the owner may write to it.

  • chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

    The ~/.ssh directory and the authorized_keys file are limited to the owner only. When other users have write permission on them, the SSH service may reject key-based logins.

Common mistakes

  • Setting 777: When a plugin or script fails to write, the cause is usually wrong ownership, not missing permissions. Fix the owner with chown, and keep directories at 755 and files at 644.
  • chmod -R 755 on an entire website: Every file gets execute permission. Apply separate permissions to files and directories with find.
  • A directory without x: Without permission to enter a directory, you can't access the files in it, even when the files themselves are readable.
  • Ownership changed to root: Files created or copied with sudo belong to root, and the website can't change them. Give ownership back to the website's user.
  • Permission on the file but not on the path: To read a file, you need x permission on every directory along the way. Check the permissions along the path with ls -ld.
  • A forgotten chattr +i: A locked file can't be changed even with full permissions. When a file can't be changed for no clear reason, check it with lsattr.

The file paths and the user and group names on this page are examples, so replace them with your real values. Before a recursive change in a large directory, first run the find command without -exec and review the list of files. To convert a value like 750 to letters and back, you can use the chmod calculator, and more Linux commands for managing a server are on the Linux commands page.

Did you know?

A VPS comes with full root access over SSH, so you can manage permissions, ownership, ACLs, and file attributes with every command on this page. On Linux hosting plans, you change file and directory permissions from the file manager in the control panel.

VPS plans

Linux file permissions FAQ

What is the difference between 644 and 755 permissions?

In both settings, the owner can read and write, and the group and other users can only read. The difference is the execute permission (x): 755 gives it to everyone, and 644 leaves it out. On a directory, x lets you enter it and access the files in it, so directories get 755 and regular files get 644.

What permissions should website files and directories have?

The common recommendation is 644 for files and 755 for directories, or 640 and 750 when other users on the server don't need access. Configuration files with passwords, such as wp-config.php or .env, get tighter permissions, such as 600 or 640. This cheat sheet includes find commands that apply the permissions to the whole website at once, separately for files and directories.

What should I do instead of setting a directory to 777?

When a plugin, a content management system, or a script can't write to a directory, the usual cause is that the directory belongs to another user. Fix the ownership with chown so the directory belongs to the user the website runs as, and keep it at 755. When several users need to write to the same directory, use a shared group with the setgid bit, or ACL permissions.

How do I change permissions for all files in a directory without changing its subdirectories?

Use the find command with the -type f option, for example find /var/www/example -type f -exec chmod 644 {} +, which applies the permissions to files only. For directories, run the same command with -type d and 755. Another way is chmod -R u=rwX,go=rX, where the uppercase X gives directories enter permission and adds no execute permission to regular files.

What is umask, and how does it set permissions for new files?

The umask is a mask that sets which permissions are removed from every new file and directory. With the common value 022, new files are created with 644 and directories with 755. The value 027 removes access for other users and creates 640 and 750, and the value 077 creates files that only the owner can access.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement