The chmod command enables you to modify the permissions of a file or directory. Only the owner of the file or directory or the system administrator can change the permissions of the object.
Syntax
The syntax of the chmod command is:
# chmod [options] {mode} {file/ directory name}
chmod Command Options
The chmod command supports different options to modify permissions. One or more of these options may be used at a time.
Option | Description |
---|---|
-c | Report changes that are made in permissions. |
-f | Hide most error messages. |
-v | Display a diagnostic entry for every file processed. |
-R | Modify permissions of files and directories recursively. |
The permissions bits applied to a file system object correspond directly to the values which can be specified in the 4 digit tuple supplied to the chmod utility in the following command:
# chmod abcd [file system object]
Each value in the digit set abcd is made up of a sum of the values 1 2 and 4. By adding these values together for each digit, a value can be generate to set all file object attributes:
- a – This digit controls special attribute settings. the value 1 sets the setuid bit, the value 2 sets the setgid bit, and the value 4 sets the sticky bit on the object.
- b, c and d – These digits control read write and execute permissions for the file owner, the file owners primary group, and all other users. The value 4 enables read permission, the value 2 enables write permission, and the value 1 enables execute permission.
To set a file file to be sticky, readable and writeable by the owner, readable by their primary group and inaccessible by everyone else:
# chmod 4610 filename
To give all permission to everyone on the system:
# chmod 0777 filename
If you encounter below error while running the chmod command:
chmod: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
chmod Command Examples
1. Give the [u]ser who owns a file the right to e[x]ecute it:
# chmod u+x path/to/file
2. Give the [u]ser rights to [r]ead and [w]rite to a file/directory:
# chmod u+rw path/to/file_or_directory
3. Remove e[x]ecutable rights from the [g]roup:
# chmod g-x path/to/file
4. Give [a]ll users rights to [r]ead and e[x]ecute:
# chmod a+rx path/to/file
5. Give [o]thers (not in the file owner’s group) the same rights as the [g]roup:
# chmod o=g path/to/file
6. Remove all rights from [o]thers:
# chmod o= path/to/file
7. Change permissions recursively giving [g]roup and [o]thers the ability to [w]rite:
# chmod -R g+w,o+w path/to/directory
8. Recursively give [a]ll users [r]ead permissions to files and e[X]ecute permissions to sub-directories within a directory:
# chmod -R a+rX path/to/directory
Conclusion
On Unix and Linux systems, you would use the chmod utility to set permissions values on files and directories. You can set permissions for the user of the file or directory, the group that’s associated with the file or directory, and more. The three basic permissions are as follows:
- r: This indicates a read permission.
- w: This indicates a write permission.
- x: This is the executable permission. You can apply it to any type of program file, or to directories. If you apply an executable permission to a directory, authorized people will be able to cd into it.