How to use the chmod and chown commands?
The chown command is used to change the file's user or group owner for each file. The chmod command changes file access permissions (read/write/execute).
Here's how you would assign user/group association with a directory:
# chown root:root /var
NOTE: Keep in mind that the first root you're associating belongs to user and the second 'root' belongs to group.
Here's how you would assign user/group association with a file:
# chown root:groupname /home/upload/filename.txt
chmod Command Groups
There are two separate groups (1. Role & 2. Permission) that dictate how a file will be owned and how it can be handled. Each group has 3 different assignments that can be customized to how you see fit.
Below you'll find the groups and each groups assignments:
- u stands for user
- g stands for group
- o stands for others
- r stands for read permission
- w stands for write permission
- x stands for execute permission
chmod Command Usage
Now that you understand what the roles and permissions are, let's put them to use. Here are some example commands to perform basic role/permission association:
1. Add a single permission to a file/directory
The + symbol adds a single permission. Here's an example of utilizing +:
# chmod u+x filename
You have associated that file with the user and also added the execute permission to that file. In the following commands you will find + utilized often, to get you used to using that extension.
2. Add multiple permission to a file/directory
# chmod u+r,g+x filename
NOTE: The comma separates the multiple permission sets.
3. Remove permission from a file/directory
The following example removes read and write permission for the user.
# chmod u-rx filename
4. Change permission for all roles on a file/directory
Following example assigns execute privilege to user, group and others. a equals 'all'.
# chmod a+x filename
5. Make permission for a file same as another file (using reference)
If you want to change a file permission same as another file, you'll need to use the reference option as shown below. In this example, file2 permission will be set exactly same as file1 permission.
# chmod --reference=file1 file2
6. Change execute permission only on the directories (files are not affected)
If you want to only set permissions to directories/sub-directories and not affect the files stored in them, you can set this up by following the example command below:
# chmod u+X *
NOTE: If the files has execute permission already for either the group or others, the above command will assign the execute permission to the user