What it is
chmod is a command-line utility used to change the file mode bits (permissions) of a file or directory. You reach for it when you need to control who can read, write, or execute a file.
Installation
chmod is a standard Unix/Linux/macOS utility and is pre-installed on most systems. No installation is typically required.
Core Concepts
File permissions in Unix-like systems are divided into three categories:
- User (u): The owner of the file.
- Group (g): Members of the group that owns the file.
- Others (o): Everyone else.
For each category, three types of permissions can be granted or denied:
- Read ®: Allows viewing the contents of a file or listing the contents of a directory.
- Write (w): Allows modifying the contents of a file or creating/deleting files within a directory.
- Execute (x): Allows running a file as a program or entering a directory.
Permissions can be represented in two ways:
- Symbolic notation: Uses letters (u, g, o, a for all) and symbols (+, -, =).
- Octal notation: Uses a three-digit number where each digit represents permissions for user, group, and others, respectively.
4: read ®2: write (w)1: execute (x)- Sum these values for combined permissions (e.g.,
7forrwx,6forrw-,5forr-x).
Commands / Usage
Changing Permissions with Symbolic Notation
Granting Permissions:
chmod u+x script.shGrant execute permission to the user (owner) ofscript.sh.chmod g+w data.txtGrant write permission to the group ofdata.txt.chmod o+r config.ymlGrant read permission to others forconfig.yml.chmod a+r public_file.txtGrant read permission to all (user, group, others) forpublic_file.txt.chmod ug+rw report.docxGrant read and write permissions to both the user and the group forreport.docx.chmod +x executable_programGrant execute permission to user, group, and others (ifa+xis not specified,+xdefaults toa+xin many shells, but it’s safer to be explicit witha+xif that’s the intent).
Removing Permissions:
chmod u-w private.keyRemove write permission from the user (owner) ofprivate.key.chmod g-r shared_document.pdfRemove read permission from the group ofshared_document.pdf.chmod o-wx sensitive_data.csvRemove write and execute permissions from others forsensitive_data.csv.chmod -w file.txtRemove write permission from all (user, group, others) forfile.txt.
Setting Specific Permissions (Overwriting existing):
chmod u=rwx,g=rx,o=r file.txtSet user permissions to read, write, and execute; group to read and execute; and others to read forfile.txt. This overwrites any previous permissions for these categories.chmod u=rw,g=,o= file.txtSet user permissions to read and write; remove all permissions for group and others forfile.txt.chmod a=rwx directorySet read, write, and execute permissions for all (user, group, others) on thedirectory.
Changing Permissions with Octal Notation
Granting Permissions (Adding to existing): Note: Octal notation typically overwrites permissions. To add, you would first read the current permissions and then calculate the new octal value.
Setting Specific Permissions (Overwriting existing):
chmod 755 script.shSet permissions forscript.shto:- User:
rwx(4+2+1 = 7) - Group:
r-x(4+0+1 = 5) - Others:
r-x(4+0+1 = 5) This is common for executable scripts and directories.
- User:
chmod 644 data.txtSet permissions fordata.txtto:- User:
rw-(4+2+0 = 6) - Group:
r--(4+0+0 = 4) - Others:
r--(4+0+0 = 4) This is common for regular data files that should be readable by everyone but only writable by the owner.
- User:
chmod 600 private.keySet permissions forprivate.keyto:- User:
rw-(4+2+0 = 6) - Group:
---(0+0+0 = 0) - Others:
---(0+0+0 = 0) This makes the file readable and writable only by the owner, a common setting for sensitive files.
- User:
chmod 700 directorySet permissions fordirectoryto:- User:
rwx(4+2+1 = 7) - Group:
---(0+0+0 = 0) - Others:
---(0+0+0 = 0) This allows the owner full access and no access for anyone else.
- User:
Recursive Operations
chmod -R 755 project_folder/Recursively change permissions forproject_folderand all its contents to755(userrwx, groupr-x, othersr-x).chmod -R u+w docs/Recursively grant write permission to the user (owner) for all files and subdirectories withindocs/.chmod -R o-rwx private_files/Recursively remove all permissions (read, write, execute) for others fromprivate_files/and its contents.
Special Permissions
chmod u+s file.shSet the SetUID bit forfile.sh. When this file is executed, it will run with the permissions of the file’s owner, not the user executing it. (Octal:4755if base is755)chmod g+s directory/Set the SetGID bit fordirectory/. When a file is created within this directory, it will inherit the group ownership of the directory, not the primary group of the user creating the file. (Octal:2775if base is775)chmod +t temp_directory/Set the sticky bit fortemp_directory/. On directories, this means that only the owner of a file (or the owner of the directory, or root) can delete or rename that file. (Octal:1777if base is777)
Displaying Permissions
While chmod changes permissions, ls -l is used to view them:
ls -l script.shOutput like-rwxr-xr-x 1 user group 1234 Jan 1 10:00 script.shThe first set ofrwxrepresents the owner’s permissions. The second set represents the group’s permissions. The third set represents others’ permissions. A-indicates a denied permission. Special bits:sinstead ofxfor SetUID/SetGID (e.g.,-rwsr-xr-x),tfor sticky bit (e.g.,drwxrwxrwt).
Common Patterns
Make a script executable:
chmod +x deploy.sh
Set standard read-write for owner, read-only for group/others:
chmod 644 config.json
Make a file private to the owner:
chmod 600 secrets.txt
Create a directory that everyone can read/write/enter, but only owners of files can delete/rename:
chmod 1777 shared_folder/
Recursively change ownership and permissions (often combined with chown):
sudo chown -R www-data:www-data /var/www/html/sudo find /var/www/html/ -type d -exec chmod 755 {} \;(Directories: rwx for owner, rx for group/others)sudo find /var/www/html/ -type f -exec chmod 644 {} \;(Files: rw for owner, r for group/others)
Set permissions for files and directories differently recursively:
chmod -R u+w my_project/find my_project/ -type d -exec chmod o-w {} \;(Remove write for others on directories)
Gotchas
- Octal vs. Symbolic: Be mindful of whether you’re using octal or symbolic notation. Octal notation replaces permissions, while symbolic notation adds or removes them (unless you use
=). -Ris powerful: Usingchmod -Rcan quickly change permissions on many files. Double-check your command before executing it, especially withsudo. You can accidentally lock yourself out of files or expose sensitive data.- Directories need
x: Tocdinto a directory or access files within it, you need execute (x) permission on the directory itself. - Windows Subsystem for Linux (WSL): File permissions on WSL can sometimes be tricky, especially when interacting with the Windows filesystem (e.g.,
/mnt/c). Permissions might not translate perfectly. - Default permissions: The
umaskcommand determines the default permissions that are removed when a new file or directory is created.chmodoverrides this for existing files. - SetUID/SetGID/Sticky Bit: These special permissions can have significant security implications. Use them with caution and understanding. For example, SetUID on a script could allow a user to gain root privileges if the script is flawed.
+xbehavior: While often defaulting toa+x, explicitly usinga+x(oru+x,g+x,o+x) is clearer if you intend to grant execute to everyone.