Chmod
' is a shell command for changing access permissions and special mode flags of files. The name is short for change mod'e where mode refers to the permissions and flags collectively.
The command originated in AT&T Unix version 1 and was exclusive to Unix and Unix-like operating systems until it was ported to other operating systems such as Windows and IBM i.
In Unix and Unix-like operating systems, a system call with the same name as the command,, provides access to the underlying access control data. The command exposes the capabilities of the system call to a shell user.
As the need for enhanced file-system permissions grew, access-control lists were added to many file systems to augment the modes controlled via.
The implementation of bundled in GNU coreutils was written by David MacKenzie and Jim Meyering.
Use
Although the syntax of the command varies somewhat by implementation, it generally accepts either a single octal value, or a comma-delimited list of symbolic specifiers. The remaining arguments are a list of paths to files to be modified.Changing permissions is only allowed for the superuser and the owner of a file.
If a symbolic link is specified, the target of the link has its mode bits adjusted. Permissions directly associated with a symbolic link file system entry are typically not used.
Options
Optional, command-line options may include:- recursive; include contained files and subdirectories of specified directories
- verbose; log changed file names
Octal notation
Given a numeric permissions argument, the command treats it as an octal number, and replaces all the mode bits for each file.There are twelve standard mode bits, comprising three special bits, and three permission groups of 3 bits each ; each permission bit grants access if set or denies access if clear.
As an octal digit represents a 3-bit value, the twelve mode bits can be represented as four octal digits. accepts up to four digits and uses 0 for left digits not specified. In practice, three digits are commonly specified since the special modes are rarely used and the user class is usually specified.
In the context of an octal digit, each operation bit represents a numeric value: read: 4, write: 2 and execute: 1. The following table relates octal digit values to a class operations value.
| # | bits | rwx | granted operations |
| 7 | read, write and execute | ||
| 6 | read and write | ||
| 5 | read and execute | ||
| 4 | read only | ||
| 3 | write and execute | ||
| 2 | write only | ||
| 1 | execute only | ||
| 0 | none |
The command [stat (Unix)|] can report a file's permissions as octal. For example:
$ stat -c %a findPhoneNumbers.sh
754
The reported value, indicates the following permissions:
- user class: read, write, and execute; 7 =>
- group class: read and execute; 5 =>
- others class: read only;
Symbolic notation
The command accepts symbolic notation that specifies how to modify the existing permissions. The command accepts a comma-separate list of specifiers like:+|-|=operationsClasses map permissions to users. A change specifier can select one class by including its symbol, multiple by including each class's symbol with no delimiter, or all classes by not specifying a symbol; when using the last method, the bits of the umask mask will remain unchanged. Class specifiers include:
| symbol | description |
| user: file owner | |
| group: members of the file's group | |
| others: users who are neither the file's owner nor members of the file's group | |
| all three classes; same as |
As ownership is key to access control, and since the symbolic specification uses the abbreviation o, some incorrectly think that it means owner, when, in fact, it is short for others.
The change operators include:
| symbol | description |
| add operations/flags | |
| remove operations/flags | |
| set the entire operations/flags field; grants the specified operations and denies others |
Operations can be specified as follows:
| symbol | description |
| read a regular file or list a directory's contents | |
| write to a file | |
| execute a regular file or recurse a directory tree | |
| special execute: selects to apply execute to directories and apply execute to files that already have at least one execute permission granted ; only useful with operation and usually in combination with option for giving group or others access to a directory tree without setting execute permission on regular files, which would normally happen with ; instead use | |
| setuid mode or setgid mode | |
| sticky mode |
Most implementations support the specification of the special modes in octal, but some do not which requires using the symbolic notation.
The [ls|] command can report file permissions in a symbolic notation that is similar to the notation used with. reports permissions in a notation that consists of 10 letters. The first indicates the type of the file system entry, such as dash for regular file and 'd' for directory. Following that are three sets of three letters that indicate read, write and execute permissions grouped by user, group and others classes. Each position is either dash to indicate lack of permission or the single-letter abbreviation for the permission to indicate that it's granted. For example:
$ ls -l findPhoneNumbers.sh
-rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh
The permission specifier starts with a dash, which indicates that is a regular file, not a directory. The next three letters indicate that the file can be read, written, and executed by the owning user. The next three letters indicate that the file can be read and executed by members of the group. And the last three letters indicate that the file is read-only for other users.
Examples
Add write permission to the group class of a directory, allowing users in the same group to add files:$ ls -ld dir # before
drwxr-xr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir
$ chmod g+w dir
$ ls -ld dir # after
drwxrwxr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir
Remove write permission for all classes, preventing anyone from writing to the file:
$ ls -l ourBestReferenceFile
-rw-rw-r-- 2 tmiller northregion 96 Apr 8 12:53 ourBestReferenceFile
$ chmod a-w ourBestReferenceFile
$ ls -l ourBestReferenceFile
-r--r--r-- 2 tmiller northregion 96 Apr 8 12:53 ourBestReferenceFile
Set the permissions for the user and group classes to read and execute only, with no write permission, preventing anyone from adding files:
$ ls -ld referenceLib
drwxr----- 2 ebowman northregion 96 Apr 8 12:53 referenceLib
$ chmod ug=rx referenceLib
$ ls -ld referenceLib
dr-xr-x--- 2 ebowman northregion 96 Apr 8 12:53 referenceLib
Enable write for the user class while making it read-only for group and others:
$ chmod u=rw,go=r sample
$ ls -ld sample
drw-r--r-- 2 oschultz warehousing 96 Dec 8 12:53 sample
To recursively set access for the directory docs/ and its contained files:
chmod -R u+w docs/To set user and group for read and write only and set others for read only:
chmod 664 fileTo set user for read, write, and execute only and group and others for read only:
chmod 744 fileTo set the sticky bit in addition to user, group and others permissions:
chmod 1755 fileTo set UID in addition to user, group and others permissions:
chmod 4755 fileTo set GID in addition to user, group and others permissions:
chmod 2755 file