On this page
- How do I read the permission string?
- What do read, write and execute mean for directories?
- How do octal modes like 644 and 755 work?
- How do I use chmod with letters instead of numbers?
- How do I change the owner and group?
- What are umask, setuid, setgid and the sticky bit?
- When should I use ACLs?
- Why is access still denied when the bits look right?
- Questions
How do I read the permission string?
Run ls -l and look at the first column. It holds ten characters: a type marker followed by three groups of three bits.
$ ls -l /etc/hostname /home/deploy
-rw-r--r-- 1 root root 18 Dec 3 09:12 /etc/hostname
drwxr-x--- 4 deploy deploy 4096 Dec 3 09:40 /home/deployThe first character is - for a regular file, d for a directory and l for a symbolic link. Then come the bits for the owner, the group and others, in that order. The two names after the link count are the owning user and group. So /etc/hostname can be changed only by root and read by everyone, and /home/deploy is fully open to its owner, readable by the deploy group and closed to everybody else.
For a single file, stat prints the same data in octal as well:
stat -c '%A %a %U:%G %n' /etc/hostnameWhat do read, write and execute mean for directories?
On files the bits are intuitive: read the content, change the content, run it as a program. On directories they mean something different, and most permission puzzles come from that:
- r lets you list the names inside the directory
- w lets you create, rename and delete entries inside it (together with x)
- x lets you enter the directory and reach files in it by name
Deleting a file therefore depends on write permission on the directory, not on the file. To reach /var/www/example.net/index.html, a process needs x on every directory along the path. namei shows the whole chain at once, which is the fastest way to find the one directory that blocks access:
namei -l /var/www/example.net/index.htmlHow do octal modes like 644 and 755 work?
Each permission bit has a value: read is 4, write is 2 and execute is 1. Add them per group to get one digit, and write the three digits in owner, group, others order. A few values cover nearly every case on a server:
600: private file, for example an SSH key or a file with database passwords640: config that a service group may read644: normal file readable by all, such as web pages700: private directory, for example~/.ssh750: directory shared with one group755: normal directory or executable script
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 755 /usr/local/bin/backup.shSSH is strict about these: when ~/.ssh or authorized_keys can be written by the group or others, sshd refuses the key and falls back to a password prompt.
How do I use chmod with letters instead of numbers?
Symbolic mode changes single bits and leaves the rest alone, which is safer when you do not know the current mode. The pattern is who (u, g, o, a), an operator (+, -, =) and the bits:
chmod u+x deploy.sh # owner may run it
chmod g+w shared.log # group may write
chmod o-rwx secrets.env # others lose everything
chmod u=rw,g=r,o= app.conf # exact result: 640Recursive changes need care, because files and directories want different bits. Use find to treat them separately, or the capital X, which adds execute only to directories and to files that are already executable:
sudo find /var/www/example.net -type d -exec chmod 755 {} +
sudo find /var/www/example.net -type f -exec chmod 644 {} +
chmod -R u=rwX,g=rX,o= /srv/projectHow do I change the owner and group?
Use chown for the owner and group together, and chgrp when only the group changes. Changing the owner always needs sudo:
sudo chown deploy:deploy /srv/app/config.yml
sudo chown -R deploy:www-data /var/www/example.net
sudo chgrp www-data /var/www/example.net/uploads
ls -ld /var/www/example.netA common web layout gives your own account ownership so you can deploy without sudo, and gives the web server group read access. On Ubuntu and Debian the web server group is www-data; on Rocky Linux and AlmaLinux it is nginx or apache, depending on what you installed. Only the directories the application really writes to, such as an uploads folder, should be writable by that group. To add yourself to a group and use it immediately: sudo usermod -aG www-data deploy, then log in again.
What are umask, setuid, setgid and the sticky bit?
The umask decides the default mode of new files. With the common value 022, new files get 644 and new directories 755; with 027 others get nothing. Check and change it for the current shell:
umask
umask 027Add the line to ~/.bashrc to keep it, or set UMask= in a systemd unit for a service. Three special bits sit in front of the normal ones:
- setuid (4000) runs a program as its owner;
passwduses it. Avoid setting it on your own files. - setgid (2000) on a directory makes new files inherit the directory group, ideal for team folders.
- sticky (1000) on a directory lets users delete only their own files, which is how
/tmpworks.
sudo chmod 2775 /srv/shared
ls -ld /srv/shared /tmp
sudo find / -xdev -perm -4000 -type f 2>/dev/nullThe last command lists setuid programs, a useful audit after installing software.
When should I use ACLs?
Access control lists add rules for extra users or groups when one owner and one group are not enough. Typical case: a backup account needs read access to a folder owned by an application user, without joining its group. The getfacl and setfacl tools come with the acl package; install it if missing (sudo apt install acl or sudo dnf install acl).
sudo setfacl -R -m u:backup:rX /srv/app/data
sudo setfacl -R -d -m u:backup:rX /srv/app/data
getfacl /srv/app/data
sudo setfacl -R -b /srv/app/data # remove all ACLsThe -d line sets a default ACL so new files inherit the rule. A + at the end of the ls -l mode string tells you a file carries an ACL.
Why is access still denied when the bits look right?
Work through the usual suspects in order:
- a parent directory lacks x for that user; check with
namei -l - the process runs as a different user than you think; check with
ps -o user,cmd -C nginx - a new group membership is not active yet; log in again and run
id - an ACL overrides the group bits; check with
getfacl - on Rocky Linux and AlmaLinux, SELinux labels block access
For the last case, look at the context with ls -Z and fix labels with sudo restorecon -Rv /var/www. The guide on SELinux basics explains contexts and booleans. Resist chmod -R 777: it hides the real cause and lets any local process, including a compromised web app, rewrite your files.
Questions about File and directory permissions
What does chmod 777 do and why avoid it?
It gives every user on the system read, write and execute rights. Any process that is compromised can then change your code or data. Find the real owner or group that needs access instead.
What permissions should a web root use?
Directories 755 and files 644, owned by your deploy user, with only upload or cache folders writable by the web server group (www-data on Ubuntu and Debian, nginx or apache on Rocky and Alma).
Why does SSH ignore my authorized_keys file?
sshd refuses keys when ~/.ssh is not 700 or authorized_keys is writable by others, or when the files belong to root. Run chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys and fix the owner.
How do I copy permissions from one file to another?
Use chmod --reference=source target and chown --reference=source target with sudo.
Do permissions survive a backup and restore?
Tools like rsync -a, tar -p and restic keep modes and owners. Add -A -X to rsync to keep ACLs and extended attributes as well.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release