This chapter focuses on the practical commands chmod and chown, which allow administrators and users to alter the permissions and ownership of files and directories.
1. Changing Ownership (chown)
The chown (change owner) command is used to change the owner and/or the group associated with a file or directory. This command often requires administrator privileges (sudo).
Changing Only the Owner
To change the owner of settings.conf to a user named alex:
chown alex settings.conf
Changing Only the Group
To change only the group of settings.conf to developers:
chown :developers settings.conf
Changing Both Owner and Group
To change both the owner to alex and the group to developers:
chown alex:developers settings.conf
Recursive Ownership Change (-R)
To change the owner and group of an entire directory and all files within it, you must use the -R (recursive) flag.
chown -R alex:developers website_project/
2. Changing Permissions (chmod)
The chmod (change mode) command is used to change the read, write, and execute permissions of a file or directory. This can be done using either the octal (numeric) method or the symbolic (letter) method.
A. Octal (Numeric) Method
This is the most common and precise way to set permissions. You specify the three-digit octal code (Owner, Group, Others) directly.
To set file permissions to 755 (Owner=rwx, Group=r-x, Others=r-x) for script.sh:
chmod 755 script.sh
To set file permissions to 644 (Owner=rw-, Group=r–, Others=r–) for config.yaml:
chmod 644 config.yaml
B. Symbolic (Letter) Method
The symbolic method allows you to incrementally add (+), subtract (-), or set (=) permissions without knowing the existing state.
| Component | Meaning |
| Who: | u (owner), g (group), o (others), a (all) |
| Action: | + (add), - (remove), = (set exactly) |
| Permission: | r (read), w (write), x (execute) |
- Granting Execute Permission to the Owner:Allows the owner to run the file.
chmod u+x start.sh - Removing Write Permission from Group and Others:Ensures nobody but the owner can modify the file.
chmod go-w important_file.txt - Setting Permissions Exactly:Sets the Group and Others permissions to read-only, regardless of what they were before.
chmod go=r data.csv
3. Recursive Permission Change (-R)
Similar to chown, the -R flag is necessary to apply permission changes to all files and subdirectories within a given path.
chmod -R 664 assets/images/
