chmod
Original author(s) | AT&T Bell Laboratories |
---|---|
Developer(s) | Various open-source and commercial developers |
Initial release | 3 November 1971 |
Written in | Plan 9: C |
Operating system | Unix, Unix-like, Plan 9, Inferno, IBM i |
Platform | Cross-platform |
Type | Command |
License | coreutils: GPLv3 Plan 9: MIT License |
chmod
is a shell command for changing access permissions and special mode flags of files (including special files such as directories). The name is short for change mode where mode refers to the permissions and flags collectively.[1][2]
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 (in UnxUtils)[3] and IBM i.[4]
In Unix and Unix-like operating systems, a system call with the same name as the command, chmod(), 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[5] were added to many file systems to augment the modes controlled via chmod
.
The implementation of chmod
bundled in GNU coreutils was written by David MacKenzie and Jim Meyering.[6]
Use
[edit]Although the syntax of the command varies somewhat by implementation, it generally accepts either a single octal value to which to set the permission value or a comma-delimited list of symbolic specifiers that describe how to change the current settings. A command ends with a space-delimited list of paths to files to be modified.[7]
Changing permissions is only allowed for the superuser (root) and the owner of a file.
If a symbolic link is specified, the linked file is affected. Permissions directly associated with a symbolic link file system entry are typically not used.
Options
[edit]Optional, command-line options may include:
-R
recursive; include contained files and subdirectories of specified directories-v
verbose; log changed file names
Octal notation
[edit]Given an octal notation permissions value, the chmod
command sets the stored permissions field for the file; replacing the entire, existing value.
The permissions field structure is a collection of twelve single-bit fields. Each permission bit grants a type of access if set (1) or denies the access if clear (0). The bits are grouped into four 3-bit fields – one for special modes then operation permissions for the user, group and others classes. The special modes are setuid, setgid, and sticky. A classes bits are for read, write and execute.
As an octal digit represents a 3-bit value, the four 3-bit fields can be represented as four octal digits. chmod
accepts up to four digits and uses 0 for left digits not specified (as is normal for numeric representation). In practice, 3 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 | 4 + 2 + 1 | rwx
|
read, write and execute |
6 | 4 + 2 | rw-
|
read and write |
5 | 4 + 1 | r-x
|
read and execute |
4 | 4 | r--
|
read only |
3 | 2 + 1 | -wx
|
write and execute |
2 | 2 | -w-
|
write only |
1 | 1 | --x
|
execute only |
0 | ---
|
none |
The command stat
can report a file's permissions as octal. For example:
$ stat -c %a findPhoneNumbers.sh
754
The reported value, 754
indicates the following permissions:
- user class: read, write, and execute; 7 => (4 + 2 + 1)
- group class: read and execute; 5 => (4 + 1)
- others class: read only; (4)
A code permits execution if and only if it is odd (i.e. 1, 3, 5, or 7). A code permits read if and only if it is greater than or equal to 4 (i.e. 4, 5, 6, or 7). A code permits write if and only if it is 2, 3, 6, or 7.
Symbolic notation
[edit]The chmod
command accepts symbolic notation that specifies how to modify the existing permissions.[8] The command accepts a comma-separate list of specifiers like: [classes]+|-|=operations
Classes 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 if not specified, then all classes are selected and further the bits of umask mask will be unchanged.[9] Class specifiers include:
symbol | description |
---|---|
u | user: file owner |
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 three classes; same as ugo
|
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 |
---|---|
r | read a regular file or list a directory's contents |
w | write to a file |
x | execute a regular file or recurse a directory tree |
X | special execute: selects to apply execute to directories (regardless of their current permissions) and apply execute to files that already have at least one execute permission granted (for any class); only useful with operation + and usually in combination with option -R for giving group or others access to a directory tree without setting execute permission on regular files, which would normally happen if with chmod -R a+rx . ; instead use chmod -R a+rX .
|
s | setuid mode or setgid mode |
t | sticky mode |
Most chmod
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 chmod
. ls -l
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 -rwxr-xr--
starts with a dash which indicates that findPhoneNumbers.sh
is a regular file; not a directory. The next three letters rwx
indicate that the file can be read, written, and executed by the owning user dgerman
. The next three letters r-x
indicate that the file can be read and executed by members of the staff
group. And the last three letters r--
indicate that the file is read-only for other users.
Examples
[edit]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; 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 file
To set user for read, write, and execute only and group and others for read only:
chmod 744 file
To set the sticky bit in addition to user, group and others permissions:
chmod 1755 file
To set UID in addition to user, group and others permissions:
chmod 4755 file
To set GID in addition to user, group and others permissions:
chmod 2755 file
See also
[edit]attrib
cacls
, modifies access control listschattr
, changes the attributes of a filechgrp
, changes the group of a filechown
, changes the owner of a file- Group identifier – Unix/POSIX system account group number; numeric value used to represent a specific group
- List of POSIX commands
- User identifier – Value identifying a user account in Unix and Unix-like operating systems
umask
, restricts permissions at file creation
References
[edit]- ^ The modes/permissions are shown when listing files in long format.
- ^ "Tutorial for chmod". catcode.com.
- ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
- ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 5 September 2020.
- ^ "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.
- ^ "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.
External links
[edit]- 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