chmod and chown Cheat Sheet

Most used
chmod +x deploy.sh                       # make a script executable
chmod 644 notes.txt                      # rw-r--r--
chmod 600 ~/.ssh/id_ed25519              # private key: owner only
sudo chown alice:alice file              # change owner and group
sudo chown -R www-data:www-data /var/www/site
chmod -R u=rwX,go=rX project/            # sane recursive mode for files and dirs

Each file has an owner, a group and three permission sets: owner (u), group (g) and others (o), each with read (4), write (2) and execute (1). chmod changes those bits, chown and chgrp change who they apply to. Only root can give a file to another user; a normal user can only change the group to one they belong to. To convert between numbers and rwx letters, use the chmod calculator; for the concepts behind it, read the file permissions guide.

Reading permissions

CommandWhat it does
ls -l fileMode, owner, group: -rw-r--r-- 1 alice staff
ls -ld dirThe directory itself, not its contents
stat -c '%a %A %U:%G %n' fileOctal and symbolic mode with owner and group
namei -l /srv/app/config.ymlPermissions of every directory along a path (finds the one blocking access)
id / id aliceYour UID, GID and groups
getfacl fileShow ACL entries
lsattr fileExtended attributes such as immutable (i)
sudo -u www-data test -r file && echo yesTest whether another user can read a file

chmod: octal modes

CommandWhat it does
chmod 644 filerw-r--r--: normal files
chmod 755 filerwxr-xr-x: scripts, programs, directories
chmod 600 filerw-------: private files, SSH keys, credentials
chmod 700 dirrwx------: private directory such as ~/.ssh
chmod 664 file / chmod 775 dirGroup-writable files and directories
chmod 440 fileRead-only for owner and group (for example /etc/sudoers.d/*)
chmod 2775 dirsetgid directory: new files inherit the directory's group
chmod 1777 dirSticky world-writable directory, like /tmp
chmod 4755 binarysetuid: runs as the file's owner (rarely needed, security-sensitive)
chmod --reference=a.txt b.txtCopy the mode of another file

chmod: symbolic modes

CommandWhat it does
chmod u+x fileAdd execute for the owner
chmod +x fileAdd execute for everyone (respects umask)
chmod a+x fileAdd execute for all, ignoring umask
chmod go-w fileRemove write from group and others
chmod o= fileRemove every permission for others
chmod u=rw,g=r,o= fileSet exact permissions per class (= 640)
chmod g=u fileGive the group the same permissions as the owner
chmod g+s dirSet setgid on a directory
chmod +t dirSet the sticky bit
chmod -R u=rwX,go=rX dirX = execute only on directories and already-executable files
chmod -v / -cPrint every file processed / only files that changed

chown and chgrp

CommandWhat it does
sudo chown alice fileChange the owner only
sudo chown alice:devs fileChange owner and group
sudo chown alice: fileOwner alice, group = alice's login group
sudo chown :devs fileGroup only (same as chgrp devs file)
chgrp devs fileChange group (no sudo needed if you are in devs and own the file)
sudo chown -R alice:alice ~aliceRecursive: fix a home directory after copying files as root
sudo chown -h alice linkChange a symlink itself instead of its target
sudo chown --from=root:root -R alice: dirOnly change files currently owned by root:root
sudo chown --reference=a bCopy owner and group from another file
sudo chown 1001:1001 fileUse numeric IDs (useful for container volumes)

Users and groups themselves are created and changed with useradd, usermod -aG and groupadd; see the user management guide.

umask: default permissions for new files

CommandWhat it does
umaskShow the current mask, for example 0022
umask -SShow it symbolically: u=rwx,g=rx,o=rx
umask 022New files 644, new directories 755 (common default)
umask 002New files 664, directories 775 (shared group work)
umask 027New files 640, directories 750 (no access for others)
umask 077New files 600, directories 700 (private)
UMask=0027 in a systemd unitSet the mask for a service
UMASK in /etc/login.defsSystem-wide default for login sessions

Files start from 666 and directories from 777; the umask bits are removed. Try other values in the umask calculator. A umask command only affects the current shell and its children; put it in ~/.bashrc or ~/.profile to keep it.

ACLs: setfacl and getfacl

CommandWhat it does
getfacl fileShow owner, group, mask and extra user/group entries
setfacl -m u:alice:rw fileGive user alice read and write
setfacl -m g:auditors:r fileGive a group read access
setfacl -R -m u:alice:rwX dirRecursive, with capital X for directories
setfacl -d -m g:devs:rwX dirDefault ACL: new files in dir inherit this entry
setfacl -x u:alice fileRemove alice's entry
setfacl -b fileRemove all ACL entries
setfacl -k dirRemove the default ACL
getfacl -R dir > acl.txt / setfacl --restore=acl.txtBack up and restore ACLs

The acl package provides these commands; it is installed by default on Ubuntu, Fedora and openSUSE (sudo apt install acl or sudo pacman -S acl otherwise). ext4, XFS and Btrfs support ACLs out of the box.

Common recipes

Reset a web root: directories 755, files 644, owned by the web server user:

sudo chown -R www-data:www-data /var/www/site     # nginx/apache user on Debian/Ubuntu; "apache" or "nginx" on Fedora
sudo find /var/www/site -type d -exec chmod 755 {} +
sudo find /var/www/site -type f -exec chmod 644 {} +

Fix SSH key permissions that cause Permission denied (publickey):

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_* ~/.ssh/authorized_keys ~/.ssh/config
chmod 644 ~/.ssh/*.pub

Shared team directory where everyone in devs can edit everything:

sudo groupadd devs
sudo usermod -aG devs alice
sudo chgrp -R devs /srv/shared
sudo chmod -R g+rwX /srv/shared
sudo find /srv/shared -type d -exec chmod g+s {} +
sudo setfacl -R -d -m g:devs:rwX /srv/shared

Find world-writable files and setuid binaries (security audit):

sudo find / -xdev -type f -perm -0002 -ls 2>/dev/null
sudo find / -xdev -type f -perm -4000 -ls 2>/dev/null

Give your user back the files that sudo created in your home directory:

sudo find ~ -not -user "$USER" -exec chown "$USER": {} +

Gotchas

  • Never "fix" access with chmod 777. It lets every user and service modify the file. Fix the owner or group, or add an ACL entry.
  • chmod -R 755 or 644 on a tree breaks either files (all executable) or directories (none enterable). Use X or separate find -type d / -type f runs.
  • Directory permissions control access to the files inside. A 600 file in a directory without x for you is unreachable, and anyone with write on the directory can delete your file. Check the path with namei -l.
  • New group membership needs a new login. After usermod -aG, log out and in, or run newgrp devs.
  • The ACL mask limits named entries. A later chmod g-w on a file with ACLs changes the mask and silently removes write from every named user and group. Check getfacl for #effective:.
  • Some filesystems ignore all of this. FAT, exFAT and NTFS mounts get permissions from mount options (uid=, umask=), and chmod fails with Operation not permitted. Immutable files (chattr +i) refuse changes even from root.
  • setuid on scripts does nothing. Linux ignores setuid and setgid on interpreted scripts; use sudo rules instead.

References: chmod(1), chown(1), setfacl(1) and umask(2).

Frequently asked questions

How do I change the owner of a file and all its contents?

Use sudo chown -R user:group /path. The -R applies the change to every file and subdirectory. Leave out :group to change only the owner.

How do I make a script executable?

Run chmod +x script.sh (or chmod u+x for just yourself), then start it with ./script.sh. If it still fails, see the Permission denied page for noexec mounts and line-ending problems.

What does chmod -R 755 do to files?

It makes every file executable, which is usually wrong for documents and configs. Use chmod -R u=rwX,go=rX: capital X sets execute only on directories and on files that are already executable.

What does umask 022 mean?

It removes write permission for group and others from new files and directories. New files get 644 (rw-r--r--) and new directories 755 (rwxr-xr-x).

How do I give one extra user access without changing the group?

Use an ACL: setfacl -m u:alice:rwX file. Check it with getfacl file. A + after the mode in ls -l shows that a file has ACL entries.

Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.