The chmod (change mode) command is used to change the file permissions for users, groups, and others in a Linux filesystem.
-
Syntax:
chmod [options] mode file...
-
Examples:
chmod 755 script.sh
- Sets the permissions of
script.shtorwxr-xr-x(owner can read, write, and execute; group and others can read and execute).
chmod +x script.sh
- Adds execute permissions to
script.shfor all users.
chmod u=rw,go=r file.txt
- Sets the permissions of
file.txttorw-r--r--(owner can read and write; group and others can read).
chmod -R 644 /var/www/
- Recursively sets the permissions of all files in
/var/www/torw-r--r--.
- Sets the permissions of
-
Permissions:
- r: Read (4)
- w: Write (2)
- x: Execute (1)
-
Permission Sets:
- u: User (owner)
- g: Group
- o: Others
- a: All (user, group, and others)
-
Numerical Mode:
- Permissions can be represented numerically with a 3-digit code.
- Example:
755- 7:
rwx(owner) - 5:
r-x(group) - 5:
r-x(others)
- 7:
-
Symbolic Mode:
- Permissions can also be set using symbolic notation.
- Example:
u=rwx,g=rx,o=rx- Sets read, write, and execute for the owner, and read and execute for group and others.
-
-Ror--recursive:- Recursively changes permissions for directories and their contents.
chmod -R 755 /mydir
- Applies
755permissions to/mydirand all files and subdirectories within it.
-
--reference=[rfile]:- Sets the permissions of a file to match those of another file.
chmod --reference=reference.txt file.txt
- Sets the permissions of
file.txtto match those ofreference.txt.
-
--help:- Displays help information about the
chmodcommand.
chmod --help
- Shows usage information and options.
- Displays help information about the
-
Changing Only Specific Permissions:
- Use
+,-, or=to add, remove, or set specific permissions.
chmod u+x file.sh
- Adds execute permission to the owner of
file.sh.
- Use
-
Common Permission Values:
644: Owner can read/write, others can read (files).755: Owner can read/write/execute, others can read/execute (directories).700: Owner can read/write/execute, others have no permissions.777: All users can read/write/execute (use with caution).
-
View Current Permissions:
- Use
ls -lto view the current permissions of a file or directory.
ls -l file.txt
- Use
The chmod command is essential for managing file and directory permissions in Linux. It allows you to specify who can read, write, or execute a file using either numeric or symbolic modes. Understanding and correctly applying file permissions is crucial for maintaining security and proper access control in your system.