File permissions: chmod and chown
Every file and directory on Linux has an owner, a group and three sets of permissions: for the owner, for the group and for everyone else. chmod changes the permissions (the mode), and chown changes the owner and group. ls -l shows both.
1Reading permissions
In ls -l output, -rw-r--r-- means a regular file that the owner can read and write and others can only read. r is read, w is write and x is execute; for a directory, x allows entering it.
2Numeric modes
Each set is a digit: read is 4, write 2 and execute 1, added together. 644 (rw-r--r--) is typical for files, 755 (rwxr-xr-x) for directories and programs, and 600 (rw-------) for private files such as SSH keys.
3Changing the owner
sudo chown user:group file sets the owner and group. With -R both chmod and chown work recursively through a directory tree.
How to fix permissions of a web folder
- Check the current state:
ls -l /var/www/site - Set the owner:
sudo chown -R www-data:www-data /var/www/site - Set directories to 755:
sudo find /var/www/site -type d -exec chmod 755 {} + - Set files to 644:
sudo find /var/www/site -type f -exec chmod 644 {} +
Cautions
- Never use
chmod -R 777: it lets every user and process change the files. - A recursive chmod or chown on the wrong path, such as / or
/usr, can break the system.
Related articles
- SSH keysHow SSH key authentication works, how to create a key with ssh-keygen, install it on a server and protect it.
- Finding large and old files with findHow to use find to list big or old files on Linux, preview them safely and delete only what you checked.
- /tmp and temporary filesWhat /tmp and /var/tmp are for, when Linux cleans them, and how systemd-tmpfiles controls their age limits.
Sources
- chmod(1) manual page man7.org
- chown(1) manual page man7.org
Last reviewed: