In Unix and Unix-like operating systems, chmod is the command and system call used to change the access permissions of file system objects (files and directories) sometimes known as modes. It is also used to change special mode flags such as setuid and setgid flags and a 'sticky' bit. The request is filtered by the umask. The name is an abbreviation of change mode. They are shown when listing files in long format.
|Original author(s)||AT&T Bell Laboratories|
|Developer(s)||Various open-source and commercial developers|
|Initial release||3 November 1971|
|Operating system||Unix, Unix-like, Plan 9, Inferno, IBM i|
In the stat structure, file type and permissions (the mode) are stored together in a
st_mode bit field, which has a size of at least 12 bits (3 bits to specify the type among the seven possible types of files; 9 bits for permissions). The layout for permissions is defined by POSIX to be at the least-significant 9 bits, but the rest is undefined.
Unix-like systems implement three specific permissions that apply to each class:
- The read permission is denoted by
rand has numerical value
- When set for a directory, the read permission may grant the right to read the names of files in the directory (depending on the implementation) . No access to information about the files is permitted.
- When set for a file, the read permission grants the right to read the file and it's attributes.
- The execute permission is denoted by
xand has value
- When set for a directory, the execute permission grants the ability to search : it grants the ability to access file contents and meta-information if its name is known, but not list contents of files inside the directory, unless read is set also.
- When set for a file, the execute permission grants the ability to execute the file. This permission must be set for executable programs and scripts invoked from a command prompt.
- The write permission is denoted by
wand numerical value
- When set for a directory, the write permission grants the ability to modify entries in the directory, which includes creating, deleting, and renaming files. The execute must also set.
- When set for a file, the write permission grants the right to modify the file. Programs which "modify" the file by actually creating a new file and upon saving, delete the old version and rename the new one will fail without write permission on the directory.
When a permission is not set, the corresponding access is denied.
Permissions on Unix-like systems are not inherited. Files created within a directory do not necessarily have the same permissions as that directory.
Permissions on Unix-like systems are managed in three distinct scopes or classes. These scopes are known as user (denoted by
u), group (denoted by
g), and others (denoted by
o). Files and directories are owned by a user which defines the file's user class. Files and directories have a group attribute and any user who is a member of that group is granted rights as defined by the group flags. Users that are not the owner nor a member of the file or directory's group are granted rights based on the
othere flags. The owner of the file will have the permissions given to the user class regardless of the rights granted to the group class or others class which may be more restrictive than the rights granted to the members of the group or others.
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Throughout this section, user refers to the owner of the file, as a reminder that the symbolic form of the command uses "u".
chmod [options] mode[,mode] file1 [file2 ...]
Usually implemented options include:
-RRecursive, i.e. include objects in subdirectories.
-vverbose, show objects changed (unchanged objects are not shown).
If a symbolic link is specified, the target object is affected. File modes directly associated with symbolic links themselves are typically not used.
$ ls -l findPhoneNumbers.sh -rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh $ stat -c %a findPhoneNumbers.sh 754
x specify the read, write, and execute access. The first character of the ls display denotes the object type; a hyphen represents a plain file. This script can be read, written to, and executed by the user dgerman; read and executed by members of the staff group; and only read by any other users.
The main parts of the chmod permissions:
The characters to the right of the "d" define permissions for each class:
- the three leftmost characters,
rwx, define permissions for the User class (i.e. the file owner).
- the middle three characters,
rwx, define permissions for the Group class (i.e. the group owning the file)
- the last three characters,
---, define permissions for the Others class. In this example, users who are not the owner of the file and who are not members of the Group (and, thus, are in the Others class) have no permission to access the file.
The chmod 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 setuid, setgid, and sticky 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.
||4(r) + 2(w) + 1(x)||
||read, write and execute|
||4(r) + 2(w)||
||read and write|
||4(r) + 1(x)||
||read and execute|
||2(w) + 1(x)||
||write and execute|
754 would allow:
- "read" (4), "write" (2), and "execute" (1) for the User class i.e. 7 (4+2+1).
- "read" (4) and "execute" (1) for the Group class i.e. 5 (4+1).
- Only "read" (4) for the Others class.
A numerical code permits execution if and only if it is odd (i.e.
Whereas a numerical code permits "read" if and only if it is greater than or equal to
4 , this means
In order to determine if "write" is permitted, subtract 1 from the numerical value if it is odd (and subtract nothing if it is even); then "write" is permitted if and only if this resulting number is
6. For example:
- If the value was
2) then (subtract nothing and) conclude that writing is permitted.
- If the numerical value is
3) then subtract 1 to get
2) and conclude that writing is permitted.
- If the value was
0) then (subtract nothing and) conclude that writing is not permitted.
- If the value was
1) then subtract 1 to get
0) and conclude that once again, writing is not permitted.
Change permissions to permit members of the programmers 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 setuid, setgid and sticky bits are not specified, this is equivalent to:
$ chmod 0664 sharedFile
The chmod 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 [references][operator][modes] 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:
|g||group||members of the file's group|
|o||others||users who are neither the file's owner nor members of the file's group|
|a||all||all three of the above, same as |
The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
|+||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:
|r||read||read a file or list a directory's contents|
|w||write||write to a file or directory|
|x||execute||execute a file or recurse a directory tree|
|X||special execute||which is not a permission in itself but rather can be used instead of x. 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 (either User, Group or Others). It is only really useful when used with |
|s||setuid/gid||details in Special modes section|
|t||sticky||details in Special modes section|
Multiple changes can be specified by separating multiple symbolic modes with commas (without spaces). 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.
- Add write permission (w) to the Group's (g) access modes of a directory, allowing users in the same group to add files:
$ ls -ld shared_dir # show access modes before chmod drwxr-xr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir $ chmod g+w shared_dir $ ls -ld shared_dir # show access modes after chmod drwxrwxr-x 2 nfinny eastregion 96 Apr 8 12:53 shared_dir
- Remove write permissions (w) for all classes (a), 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 ebowman northregion 96 Apr 8 12:53 ourBestReferenceFile
- Set the permissions for the user and the Group (ug) to read and execute (rx) only (no write permission) on referenceLib, preventing anyone to add 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 dhinkle northregion 96 Apr 8 12:53 referenceLib
- Add the read and write permissions to the user and group classes of a file or directory named sample:
$ chmod ug+rw sample $ ls -ld sample drw-rw---- 2 rsanchez budget 96 Dec 8 12:53 sample
- Change the permissions for the user and the group to read and execute only (no write permission) on sample.
$ # Sample file permissions before command $ ls -ld sample drw-rw---- 2 oschultz warehousing 96 Dec 8 12:53 NY_DBs $ chmod ug=rx sample $ ls -ld sample dr-xr-x--- 2 aolensky warehousing 96 Dec 8 12:53 NJ_DBs
The chmod command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use 's' to represent the setuid and setgid modes, and 't' 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 examplesEdit
||adds read permission for all classes (i.e. user, Group and Others)|
||removes execute permission for all classes|
||adds read and execute permissions for all classes|
||sets read and write permission for user, sets read for Group, and denies access for Others|
||adds write permission to the directory docs and all its contents (i.e. Recursively) for owner, and removes write permission for group and others|
||sets read and write permissions for user and Group|
||sets read and write permissions for user and Group, and provides read to Others.|
||sets read, write, and execute permissions for user, and sets read permission for Group and Others|
||sets sticky bit, sets read, write, and execute permissions for owner, and sets read and execute permissions for group and others (this suggests that the script be retained in memory)|
||sets UID, sets read, write, and execute permissions for user, and sets read and execute permissions for Group and Others|
||sets GID, sets read, write, and execute permissions for user, and sets read and execute permissions for Group and Others|
||Recursively (i.e. on all files and directories in personalStuff) adds read, write, and special execution permissions for user, removes read, write, and execution permissions for Group, and removes read and execution permissions for Others|
||Recursively (i.e. on all files and directories in publicDocs) removes execute permission for all classes and adds special execution permission for all classes|
int chmod(const char *path, mode_t mode);
The mode parameter is a bitfield composed of various flags:
|S_ISUID||04000||Set user ID on execution|
|S_ISGID||02000||Set group ID on execution|
|S_IRUSR, S_IREAD||00400||Read by user|
|S_IWUSR, S_IWRITE||00200||Write by user|
|S_IXUSR, S_IEXEC||00100||Execute/search by user|
|S_IRGRP||00040||Read by group|
|S_IWGRP||00020||Write by group|
|S_IXGRP||00010||Execute/search by group|
|S_IROTH||00004||Read by others|
|S_IWOTH||00002||Write by others|
|S_IXOTH||00001||Execute/search by others|
- File-system permissions
chattr, the command used to change the attributes of a file or directory on Linux systems
chown, the command used to change the owner of a file or directory on Unix-like systems
chgrp, the command used to change the group of a file or directory on Unix-like systems
cacls, a command used on Windows NT and its derivatives to modify the access control lists associated with a file or directory
umask, restricts mode (permissions) at file or directory creation on Unix-like systems
- User identifier
- Group identifier
- List of Unix commands
- "Tutorial for chmod". catcode.com.
- "<sys/stat.h>". The Open Group Base Specifications Issue 6. The Open Group. 21 July 2019.
- "AIX 5.3 System management". IBM knowledge Center. IBM. Retrieved 30 August 2015.
- "chmod(1): change file mode bits - Linux man page". linux.die.net.
- IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). Retrieved 5 September 2020.
- "chmod Man Page with examples and calculator - Linux - SS64.com". ss64.com.
- "AIX 5.5 Commands Reference". IBM Knowledge Center. IBM. Retrieved 30 August 2015.
- "Permissions masking with umask, chmod, 777 octal permissions". teaching.idallen.com.
- "chmod function". The Open Group Base Specifications Issue 7, 2013 Edition. The Open Group. Retrieved 30 August 2015.
|The Wikibook Guide to Unix has a page on the topic of: Commands|
- FreeBSD General Commands Manual : change file modes –
- Plan 9 Programmer's Manual, Volume 1 –
- Inferno General commands Manual –
chmod— manual page from GNU coreutils.
- GNU "Setting Permissions" manual
- CHMOD-Win 3.0 — Freeware Windows' ACL ↔ CHMOD converter.
- Beginners tutorial with on-line "live" example