Chown is a utility that changes the owner and/or group for the specified files. The first argument, which is not an option, is taken as the owner/group name. If only the user name (or a numeric user ID) is given, that user becomes the owner of each of the specified files, and the group of those files is not changed. If the user name is followed by a colon and the group name (or numeric identifier of the group) without spaces between them, the file group is also changed
How to find out the owner and group of a file and folder (directory)
To find out information about files and folders (directories), you can use the command
ls -l
If you want to know the data on a particular file file.php
ls -l file.php
Below is an example of ls -l in the WordPress root directory
The owner and group (chown) are marked in columns 3 and 4.
How to use chown
The syntax looks like this
chown [-cfhvR] [--dereference] [--reference=rfile] user[:group] file..
Description of keys and options:
- -c, --changes -Detail action for each file whose owner does change.
- -f, --silent, --quiet - Do not give error messages for files whose owner cannot be changed.
- -h, --no-dereference - Handle the character references themselves, not the files they point to. This option is only available if there is a lchown system call.
- -R, --recursive - Recursively change the owners of directories and their contents.
- -v, --verbose - Detailed description of the action (or lack of action) for each file.
- --dereference - Change the owner of the file specified by the symbolic link instead of the symbolic link itself.
- --reference=rfile - Change the owner of the file to the one which is the owner of the rfile file.
Let's set the permissions for the directory with the files. Suppose that you want to set permissions on the directory where the site files are located, the group and owner of which is example. The directory is located at /var/www/example.com.
In the console we enter:
cd /var/www
chown -Rv example:example example.com
The first command transfers to the correct directory, the second recursively changes the owner and group for the directory and its attached files and subdirectories.