chmod — Linux basic command
Linux / Basic Linux commands / chmod.html
A command that changes the access permissions of files and directories.
Basic Information
| Command Name | chmod (change mode) |
|---|---|
| One-line Introduction | A command that changes the access permissions of files and directories. |
| Provided by | GNU coreutils (pre-installed on all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/chmod.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Status | Pre-installed (no separate installation required) |
| Classification | Linux basic command (tags: permissions, access control, Linux, basic) |
What is this command?
chmod is an abbreviation for change mode, a command that changes access permissions for files and directories. In Linux, permissions for reading (r), writing (w), and executing (x) are granted individually for the owner, group, and others.
Permissions can be specified in two ways: numeric (octal) mode and symbolic mode, and it is most frequently used for granting execute permissions to scripts.
What problem does it solve?
- Grants execute permission to script files.
- Strengthens file access permissions.
- Sets shared directory permissions.
- Corrects files with incorrectly granted permissions.
Key Features
Numeric (octal) Mode
Permissions are specified numerically using the combination r=4, w=2, x=1.
- Usage scenario: When setting exact permissions at once.
- Result: Example: 755 = owner (rwx), group (r-x), others (r-x)
- Limitations: If numbers are specified incorrectly, permissions may become too open.
Symbolic Mode
Uses +, -, = operators for u (owner), g (group), o (others), a (all).
- Usage scenario: When changing permissions for a specific target.
- Result: Example: chmod u+x script.sh (grant execute permission to owner)
- Limitations: Symbolic mode may be harder to read than numeric.
Recursive Change (-R)
Applies permissions to the directory and all files beneath it.
- Usage scenario: When changing permissions for an entire directory.
- Result: All sub-files are changed as well.
- Limitations: Excessive recursive changes may cause system issues.
Reference File Copy (--reference)
Copies permissions from another file as-is.
- Usage scenario: When consistent permission settings are needed.
- Result: The same permissions as the reference file are applied.
- Limitations: May not be supported on some file systems.
Installation and Execution
Check Pre-installation
Included by default in all Linux distributions.
which chmod
chmod --version
Command to Check Permissions (ls -l)
To check current permissions, use ls -l.
ls -l file.txt
# -rw-r--r-- 1 user group 1024 Oct 18 12:00 file.txt
# ^^^ Owner permissions
# ^^^ Group permissions
# ^^^ Others' permissions
Real-world Usage Examples
Change Permissions in Numeric Mode
Set permissions in octal.
# Grant execute permission to script (755)
chmod 755 script.sh
# Read/write only (600)
chmod 600 secret.key
# Read for all (644)
chmod 644 public-file.txt
# Directory traverse permission (755)
chmod 755 /var/www/html
Change Permissions in Symbolic Mode
Change permissions for a specific target only.
# Add execute permission to owner
chmod u+x script.sh
# Remove execute permission from others
chmod o-x script.sh
# Add read permission to group
cat /etc/hosts | sudo tee /var/log/shared.txt
group_name="developers"
chmod g+r /var/log/shared.txt
chgrp $group_name /var/log/shared.txt
# Set read permission for all
chmod a=r file.txt
Recursive Permission Change
Apply permissions to the entire directory.
# Grant execute permission to directory and sub-files
chmod -R a+x /opt/scripts/
# Change only directory permissions (keep files)
chmod 755 /var/www/html/
find /var/www/html -type f -exec chmod 644 {} \;
find /var/www/html -type d -exec chmod 755 {} \;