Rain WikiWindows · Linux · Android

File permissions: chmod and chown

Command-line toolAll Linux distributions

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

  1. Check the current state: ls -l /var/www/site
  2. Set the owner: sudo chown -R www-data:www-data /var/www/site
  3. Set directories to 755: sudo find /var/www/site -type d -exec chmod 755 {} +
  4. 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.

Sources

  1. chmod(1) manual page man7.org
  2. chown(1) manual page man7.org

Last reviewed: