chmod and chown Cheat Sheet
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
| Command | What it does |
|---|---|
ls -l file | Mode, owner, group: -rw-r--r-- 1 alice staff |
ls -ld dir | The directory itself, not its contents |
stat -c '%a %A %U:%G %n' file | Octal and symbolic mode with owner and group |
namei -l /srv/app/config.yml | Permissions of every directory along a path (finds the one blocking access) |
id / id alice | Your UID, GID and groups |
getfacl file | Show ACL entries |
lsattr file | Extended attributes such as immutable (i) |
sudo -u www-data test -r file && echo yes | Test whether another user can read a file |
chmod: octal modes
| Command | What it does |
|---|---|
chmod 644 file | rw-r--r--: normal files |
chmod 755 file | rwxr-xr-x: scripts, programs, directories |
chmod 600 file | rw-------: private files, SSH keys, credentials |
chmod 700 dir | rwx------: private directory such as ~/.ssh |
chmod 664 file / chmod 775 dir | Group-writable files and directories |
chmod 440 file | Read-only for owner and group (for example /etc/sudoers.d/*) |
chmod 2775 dir | setgid directory: new files inherit the directory's group |
chmod 1777 dir | Sticky world-writable directory, like /tmp |
chmod 4755 binary | setuid: runs as the file's owner (rarely needed, security-sensitive) |
chmod --reference=a.txt b.txt | Copy the mode of another file |
chmod: symbolic modes
| Command | What it does |
|---|---|
chmod u+x file | Add execute for the owner |
chmod +x file | Add execute for everyone (respects umask) |
chmod a+x file | Add execute for all, ignoring umask |
chmod go-w file | Remove write from group and others |
chmod o= file | Remove every permission for others |
chmod u=rw,g=r,o= file | Set exact permissions per class (= 640) |
chmod g=u file | Give the group the same permissions as the owner |
chmod g+s dir | Set setgid on a directory |
chmod +t dir | Set the sticky bit |
chmod -R u=rwX,go=rX dir | X = execute only on directories and already-executable files |
chmod -v / -c | Print every file processed / only files that changed |
chown and chgrp
| Command | What it does |
|---|---|
sudo chown alice file | Change the owner only |
sudo chown alice:devs file | Change owner and group |
sudo chown alice: file | Owner alice, group = alice's login group |
sudo chown :devs file | Group only (same as chgrp devs file) |
chgrp devs file | Change group (no sudo needed if you are in devs and own the file) |
sudo chown -R alice:alice ~alice | Recursive: fix a home directory after copying files as root |
sudo chown -h alice link | Change a symlink itself instead of its target |
sudo chown --from=root:root -R alice: dir | Only change files currently owned by root:root |
sudo chown --reference=a b | Copy owner and group from another file |
sudo chown 1001:1001 file | Use 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
| Command | What it does |
|---|---|
umask | Show the current mask, for example 0022 |
umask -S | Show it symbolically: u=rwx,g=rx,o=rx |
umask 022 | New files 644, new directories 755 (common default) |
umask 002 | New files 664, directories 775 (shared group work) |
umask 027 | New files 640, directories 750 (no access for others) |
umask 077 | New files 600, directories 700 (private) |
UMask=0027 in a systemd unit | Set the mask for a service |
UMASK in /etc/login.defs | System-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
| Command | What it does |
|---|---|
getfacl file | Show owner, group, mask and extra user/group entries |
setfacl -m u:alice:rw file | Give user alice read and write |
setfacl -m g:auditors:r file | Give a group read access |
setfacl -R -m u:alice:rwX dir | Recursive, with capital X for directories |
setfacl -d -m g:devs:rwX dir | Default ACL: new files in dir inherit this entry |
setfacl -x u:alice file | Remove alice's entry |
setfacl -b file | Remove all ACL entries |
setfacl -k dir | Remove the default ACL |
getfacl -R dir > acl.txt / setfacl --restore=acl.txt | Back 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 755or644on a tree breaks either files (all executable) or directories (none enterable). UseXor separatefind -type d/-type fruns.- Directory permissions control access to the files inside. A 600 file in a directory without
xfor you is unreachable, and anyone with write on the directory can delete your file. Check the path withnamei -l. - New group membership needs a new login. After
usermod -aG, log out and in, or runnewgrp devs. - The ACL mask limits named entries. A later
chmod g-won a file with ACLs changes the mask and silently removes write from every named user and group. Checkgetfaclfor#effective:. - Some filesystems ignore all of this. FAT, exFAT and NTFS mounts get permissions from mount options (
uid=,umask=), andchmodfails 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.