The chmod
command is one of the most fundamental and frequently used commands in Linux and Unix-based operating systems. Short for "change mode," chmod allows users to modify file and directory permissions, controlling who can read, write, or execute files on the system. Understanding chmod is essential for anyone working with Linux, from system administrators to developers and DevOps engineers.
In this comprehensive guide, we'll explore everything you need to know about the chmod command, including its syntax, permission modes, practical examples, and best practices for securing your Linux system.
Before diving into the chmod command itself, it's crucial to understand how Linux handles file permissions. Every file and directory in Linux has three types of permissions assigned to three different categories of users:
- Read (r) - Allows viewing the contents of a file or listing directory contents
- Write (w) - Permits modifying or deleting a file, or adding/removing files in a directory
- Execute (x) - Enables running a file as a program or accessing a directory
- Owner (u) - The user who owns the file
- Group (g) - Users who are members of the file's group
- Others (o) - All other users on the system
When you run ls -l
in a terminal, you'll see permissions displayed like this:
-rwxr-xr-x 1 user group 4096 Jan 15 10:30 script.sh
The first ten characters represent the file type and permissions:
- First character: File type (- for regular file, d for directory)
- Next three: Owner permissions (rwx)
- Next three: Group permissions (r-x)
- Last three: Others permissions (r-x)
The basic syntax of the chmod command is:
chmod [options] mode file
Where:
- options: Optional flags that modify chmod's behavior
- mode: The permissions to set (numeric or symbolic)
- file: The file or directory to modify
Numeric notation is the most common way to use chmod. It uses three or four digits, where each digit represents permissions for owner, group, and others respectively.
Each permission type has a numeric value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
- No permission (-) = 0
To calculate the permission digit for each user category, add the values together:
- 7 (4+2+1) = Read, Write, Execute (rwx)
- 6 (4+2) = Read, Write (rw-)
- 5 (4+1) = Read, Execute (r-x)
- 4 = Read only (r--)
- 3 (2+1) = Write, Execute (-wx)
- 2 = Write only (-w-)
- 1 = Execute only (--x)
- 0 = No permissions (---)
# Give owner full permissions, group and others read/execute
chmod 755 script.sh
# Owner read/write, group and others read only
chmod 644 document.txt
# Owner full permissions, no permissions for group/others
chmod 700 private_script.sh
# Everyone has full permissions (use with caution!)
chmod 777 shared_file.txt
# Owner read/write, group read, others no permissions
chmod 640 config.conf
For a quick and easy way to calculate chmod permissions, you can use our interactive chmod calculator which provides instant conversion between numeric and symbolic notation.
Symbolic notation provides a more intuitive way to modify permissions by using letters instead of numbers.
- Who: u (user/owner), g (group), o (others), a (all)
- Operation: + (add), - (remove), = (set exactly)
- Permission: r (read), w (write), x (execute)
# Add execute permission for owner
chmod u+x script.sh
# Remove write permission for group
chmod g-w file.txt
# Set exact permissions for others to read only
chmod o=r document.txt
# Add read and execute for everyone
chmod a+rx program
# Remove execute for group and others
chmod go-x file.sh
# Set owner to read/write, group to read, others to nothing
chmod u=rw,g=r,o= private.txt
Beyond the basic read, write, and execute permissions, Linux supports three special permission types:
When set on an executable file, the program runs with the permissions of the file's owner rather than the user executing it.
chmod 4755 program
chmod u+s program
For files: Program runs with the group permissions of the file. For directories: New files inherit the directory's group.
chmod 2755 directory
chmod g+s directory
Commonly used on directories like /tmp. Only the file owner can delete or rename files within the directory, even if others have write permissions.
chmod 1777 /tmp
chmod +t shared_directory
When using special permissions with numeric notation, use four digits:
# SETUID + 755
chmod 4755 program
# SETGID + 775
chmod 2775 directory
# Sticky bit + 777
chmod 1777 temp_directory
The -R
(recursive) option applies permissions to all files and subdirectories:
# Apply 755 to directory and all contents
chmod -R 755 /path/to/directory
# Add execute permission recursively
chmod -R +x /path/to/scripts
Warning: Be careful with recursive chmod, especially with commands like chmod -R 777
, as they can create serious security vulnerabilities.
Display detailed information about changes:
chmod -v 644 file.txt
# Output: mode of 'file.txt' changed from 0755 (rwxr-xr-x) to 0644 (rw-r--r--)
Only display output when changes are actually made:
chmod -c 644 *.txt
Prevent recursive operations on the root directory:
chmod --preserve-root -R 755 /
Copy permissions from one file to another:
chmod --reference=file1.txt file2.txt
# HTML/CSS files - readable by web server
chmod 644 *.html *.css
# PHP/Python scripts - executable by web server
chmod 755 *.php *.py
# Configuration files - owner only
chmod 600 config.ini
# Make script executable
chmod +x script.sh
# Owner can execute, others cannot
chmod 744 admin_script.sh
# Shared directory with sticky bit
chmod 1777 /shared
# Group collaboration directory
chmod 2775 /team_project
# Private key - owner read/write only
chmod 600 ~/.ssh/id_rsa
# Public key - owner read/write, others read
chmod 644 ~/.ssh/id_rsa.pub
# .ssh directory
chmod 700 ~/.ssh
# Application logs - owner read/write, group read
chmod 640 /var/log/app.log
# System logs - root only
chmod 600 /var/log/secure
Always grant the minimum permissions necessary for functionality:
# Good: Only owner can modify
chmod 644 data.txt
# Bad: Everyone can modify
chmod 666 data.txt
Never use chmod 777
in production environments unless absolutely necessary and you understand the security implications:
# Dangerous - anyone can read, write, execute
chmod 777 file.txt
# Better - specific permissions
chmod 755 file.txt
# Private keys and credentials
chmod 600 ~/.ssh/id_rsa
chmod 600 ~/.aws/credentials
# Password files
chmod 600 /etc/shadow
Periodically review file permissions:
# Find world-writable files
find / -type f -perm -002
# Find SETUID files
find / -type f -perm -4000
chmod: changing permissions of 'file.txt': Permission denied
Solution: Use sudo
if you have administrative privileges:
sudo chmod 644 file.txt
chmod: invalid mode: '888'
Solution: Use valid octal digits (0-7):
chmod 644 file.txt # Correct
chmod: changing permissions of 'file.txt': Operation not permitted
Solution: Check if the file has immutable attribute:
lsattr file.txt
sudo chattr -i file.txt # Remove immutable flag
chmod 644 file.txt
While chmod changes permissions, other commands modify ownership:
- chmod: Changes file permissions (read, write, execute)
- chown: Changes file owner and group
- chgrp: Changes file group only
# Change permissions
chmod 755 file.txt
# Change owner
chown user:group file.txt
# Change group only
chgrp developers file.txt
# Only chmod if file exists
[ -f file.txt ] && chmod 644 file.txt
# Chmod multiple files with find
find . -type f -name "*.sh" -exec chmod +x {} \;
# All .txt files to 644
chmod 644 *.txt
# All directories to 755
find . -type d -exec chmod 755 {} \;
# All files to 644
find . -type f -exec chmod 644 {} \;
For more granular control beyond traditional permissions:
# Set ACL
setfacl -m u:username:rwx file.txt
# View ACL
getfacl file.txt
The chmod command is an essential tool for managing file permissions in Linux and Unix systems. Whether you prefer numeric or symbolic notation, understanding how to properly set permissions is crucial for system security and functionality.
Key takeaways:
- Use numeric notation (chmod 755) for quick, absolute permission changes
- Use symbolic notation (chmod u+x) for relative permission modifications
- Always follow the principle of least privilege
- Be extremely cautious with recursive operations and 777 permissions
- Regularly audit file permissions for security vulnerabilities
For a visual and interactive way to calculate chmod permissions, try our chmod calculator tool which provides instant conversion between numeric and symbolic notation, along with detailed explanations and common presets.
Mastering chmod is a fundamental step in becoming proficient with Linux system administration. With practice and careful consideration of security implications, you'll be able to confidently manage file permissions across your systems.