Chmod


In Unix and Unix-like operating systems, is the command and system call which is used to change the access permissions of file system objects. It is also used to change [|special mode] flags. The request is filtered by the umask. The name is an abbreviation of change mode.

History

A command first appeared in AT&T Unix version 1.
As systems grew in number and types of users, access control lists were added to many file systems in addition to these most basic modes to increase flexibility.
The version of bundled in GNU coreutils was written by David MacKenzie and Jim Meyering.

Command syntax

Throughout this section, ser refers to the owner of the file, as a reminder that the symbolic form of the command uses "u".
chmod mode file1
Usually implemented options include:
If a symbolic link is specified, the target object is affected. File modes directly associated with symbolic links themselves are typically not used.
To view the file mode, the ls| or stat | commands may be used:

$ ls -l findPhoneNumbers.sh
-rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh
$ stat -c %a findPhoneNumbers.sh
754

The,, and specify the read, write, and execute access. The first character of the display denotes the object type; a hyphen represents a plain file. This script can be read, written to, and executed by the user ; read and executed by members of the group; and only read by any other users.

Octal modes

The main parts of the permissions:
For example:
The characters to the right of the "d" define permissions for each class:
The numerical format accepts up to four octal digits. The three rightmost digits define permissions for the file user, the group, and others. The optional leading digit, when 4 digits are given, specifies the special,, and flags. Each digit of the three rightmost digits represents a binary value, which controls the "read", "write" and "execute" permissions respectively. A value of 1 means a class is allowed that action, while a 0 means it is disallowed.
#PermissionrwxBinary
7read, write and executerwx111
6read and writerw-110
5read and executer-x101
4read onlyr--100
3write and execute-wx011
2write only-w-010
1execute only--x001
0none---000

For example, would allow:
Change permissions to permit members of the group to update a file:

$ ls -l sharedFile
-rw-r--r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile
$ chmod 664 sharedFile
$ ls -l sharedFile
-rw-rw-r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile

Since the, and bits are not specified, this is equivalent to:

$ chmod 0664 sharedFile

Symbolic modes

The command also accepts a finer-grained symbolic notation, which allows modifying specific modes while leaving other modes untouched. The symbolic mode is composed of three components, which are combined to form a single string of text:

$ chmod file...

Classes of users are used to distinguish to whom the permissions apply. If no classes are specified "all" is implied. The classes are represented by one or more of the following letters:
ReferenceClassDescription
userfile owner
groupmembers of the file's group
othersusers who are neither the file's owner nor members of the file's group
allall three of the above, same as

The program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
OperatorDescription
adds the specified modes to the specified classes
removes the specified modes from the specified classes
the modes specified are to be made the exact modes for the specified classes

The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions:
ModeNameDescription
readread a file or list a directory's contents
writewrite to a file or directory
executeexecute a file or recurse a directory tree
special executewhich is not a permission in itself but rather can be used instead of. It applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least one execute permission bit already set. It is only really useful when used with and usually in combination with the flag for giving Group or Others access to a big directory tree without setting execute permission on normal files, which would normally happen if you just used, whereas with you can do instead
setuid/giddetails in [|Special modes section]
stickydetails in Special modes section

Multiple changes can be specified by separating multiple symbolic modes with commas. If a user is not specified, chmod will check the umask and the effect will be as if "a" was specified except bits that are set in the umask are not affected.

Symbolic examples


$ ls -ld shared_dir # show access modes before chmod
drwxr-xr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir
$ chmod g+w shared_dir
$ ls -ld shared_dir # show access modes after chmod
drwxrwxr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir


$ ls -l ourBestReferenceFile
-rw-rw-r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile
$ chmod a-w ourBestReferenceFile
$ ls -l ourBestReferenceFile
-r--r--r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile


$ ls -ld referenceLib
drwxr----- 2 teamleader usguys 96 Apr 8 12:53 referenceLib
$ chmod ug=rx referenceLib
$ ls -ld referenceLib
dr-xr-x--- 2 teamleader usguys 96 Apr 8 12:53 referenceLib

Special modes

The command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use to represent the setuid and setgid modes, and to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.
Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used.

Command line examples

CommandExplanation
chmod a+r publicComments.txtadds read permission for all classes
chmod a-x publicComments.txtremoves execute permission for all classes
chmod a+rx viewer.shadds read and execute permissions for all classes
chmod u=rw,g=r,o= internalPlan.txtsets read and write permission for ser, sets read for Group, and denies access for Others
chmod -R u+w,go-w docsadds write permission to the directory docs and all its contents for owner, and removes write permission for group and others
chmod ug=rw groupAgreements.txtsets read and write permissions for ser and Group
chmod 664 global.txtsets read and write permissions for ser and Group, and provides read to Others.
chmod 744 myCV.txtsets read, write, and execute permissions for ser, and sets read permission for Group and Others
chmod 1755 findReslts.shsets sticky bit, sets read, write, and execute permissions for owner, and sets read and execute permissions for group and others
chmod 4755 setCtrls.shsets UID, sets read, write, and execute permissions for ser, and sets read and execute permissions for Group and Others
chmod 2755 setCtrls.shsets GID, sets read, write, and execute permissions for ser, and sets read and execute permissions for Group and Others
chmod -R u+rwX,g-rwx,o-rx personalStuffRecursively adds read, write, and special execution permissions for ser, removes read, write, and execution permissions for Group, and removes read and execution permissions for Others
chmod -R a-x+X publicDocsRecursively removes execute permission for all classes and adds special execution permission for all classes

System call

The POSIX standard defines the following function prototype:
int chmod;
The mode parameter is a bitfield composed of various flags:
FlagOctal valuePurpose
S_ISUID04000Set user ID on execution
S_ISGID02000Set group ID on execution
S_ISVTX01000Sticky bit
S_IRUSR, S_IREAD00400Read by ser
S_IWUSR, S_IWRITE00200Write by ser
S_IXUSR, S_IEXEC00100Execute/search by ser
S_IRGRP00040Read by group
S_IWGRP00020Write by group
S_IXGRP00010Execute/search by group
S_IROTH00004Read by others
S_IWOTH00002Write by others
S_IXOTH00001Execute/search by others