Jump to content

Chmod: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tags: Mobile edit Mobile web edit
 
(46 intermediate revisions by 20 users not shown)
Line 10: Line 10:
| latest release version =
| latest release version =
| latest release date =
| latest release date =
| programming language = Plan 9: [[C (programming language)|C]]
| operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]], [[IBM i]]
| operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]], [[IBM i]]
| platform = [[Cross-platform]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| genre = [[Command (computing)|Command]]
| license = [[coreutils]]: [[GPLv3]]
| license = [[coreutils]]: [[GPLv3]]<br />Plan 9: [[MIT License]]
| website =
| website =
}}
}}
In [[Unix]] and [[Unix-like]] [[operating system]]s, '''{{Mono|chmod}}''' is the [[command (computing)|command]] and [[system call]] used to change the [[File-system permissions|access permissions]] of [[file system]] objects ([[Computer file|files]] and [[Directory (computing)|directories]]) sometimes known as '''modes'''. It is also used to change [[#Special modes|special mode]] flags such as [[setuid|''setuid'' and ''setgid'']] flags and a [[sticky bit|'sticky' bit]]. The request is filtered by the [[umask]]. The name is an abbreviation of ''change mode''.<ref>{{Cite web|url=http://catcode.com/teachmod/|title=Tutorial for chmod|website=catcode.com}}</ref> They are shown when [[ls|listing files]] in long format.
In [[Unix]] and [[Unix-like]] [[operating system]]s, '''{{Mono|chmod}}''' is the [[command (computing)|command]] and [[system call]] used to change the [[File-system permissions|access permissions]] and the [[#Special modes|special mode]] flags (the [[setuid|''setuid'', ''setgid'']], and [[sticky bit|''sticky'']] flags) of [[file system]] objects ([[Computer file|files]] and [[Directory (computing)|directories]]). Collectively these were originally called its '''modes''',<ref>The modes/permissions are shown when [[ls|listing files]] in long format.</ref> and the name {{Mono|chmod}} was chosen as an abbreviation of '''''ch'''ange '''mod'''e''.<ref>{{Cite web|url=http://catcode.com/teachmod/|title=Tutorial for chmod|website=catcode.com}}</ref>


==History==
==History==
A {{Mono|chmod}} command first appeared in [[AT&T Corporation|AT&T]] [[Unix]] version 1.
A {{Mono|chmod}} command first appeared in [[Research Unix|AT&T Unix]] version 1, along with the {{Mono|chmod}} system call.


As systems grew in number and types of users, [[access-control list]]s<ref>{{cite web|title=AIX 5.3 System management|url=http://www-01.ibm.com/support/knowledgecenter/#!/ssw_aix_53/com.ibm.aix.baseadmn/doc/baseadmndita/acl.htm?cp=ssw_aix_53|website=IBM knowledge Center|publisher=IBM|access-date=30 August 2015}}</ref> were added to many file systems in addition to these most basic modes to increase flexibility.
As systems grew in number and types of users, [[access-control list]]s<ref>{{cite web|title=AIX 5.3 System management|url=http://www-01.ibm.com/support/knowledgecenter/#!/ssw_aix_53/com.ibm.aix.baseadmn/doc/baseadmndita/acl.htm?cp=ssw_aix_53|website=IBM knowledge Center|publisher=IBM|access-date=30 August 2015}}</ref> were added to many file systems in addition to these most basic modes to increase flexibility.


The version of {{Mono|chmod}} bundled in [[GNU]] [[coreutils]] was written by David MacKenzie and Jim Meyering.<ref>{{Cite web|url=https://linux.die.net/man/1/chmod|title=chmod(1): change file mode bits - Linux man page|website=linux.die.net}}</ref> The command is available as a separate package for [[Microsoft Windows]] as part of the [[UnxUtils]] collection of [[Native (computing)|native]] [[Windows API|Win32]] [[porting|ports]] of common GNU Unix-like utilities.<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref> The {{Mono|chmod}} command has also been ported to the [[IBM i]] operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}</ref>
The version of {{Mono|chmod}} bundled in [[GNU Core Utilities|GNU coreutils]] was written by David MacKenzie and Jim Meyering.<ref>{{Cite web|url=https://linux.die.net/man/1/chmod|title=chmod(1): change file mode bits - Linux man page|website=linux.die.net}}</ref> The command is available as a separate package for [[Microsoft Windows]] as part of the [[UnxUtils]] collection of native [[Windows API|Win32]] ports of common GNU Unix-like utilities.<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref> The {{Mono|chmod}} command has also been ported to the [[IBM i]] operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=[[IBM]] |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}</ref>



<!-- I agree with the comment that the detailed discussion about how permissions work does not belong here and most have been be removed. 8/26/21 DGerman -->
<!-- I agree with the comment that the detailed discussion about how permissions work does not belong here and most have been be removed. 8/26/21 DGerman -->
Line 31: Line 31:
==Command syntax==
==Command syntax==


Throughout this section, ''{{strong|u}}ser'' refers to the owner of the file, as a reminder that the symbolic form of the command uses "'''u'''".
Throughout this section, ''{{strong|u}}ser'' refers to the owner of the file, as a reminder that the symbolic form of the command uses "'''u'''", to avoid confusion with "other".<!-- Please do not change to owner. DGerman -->

<!-- Please do not change to owner. DGerman -->
Note that only the '''u'''ser or the superuser (root) is able to change file permissions.


<code>chmod [options] mode[,mode] file1 [file2 ...]</code><ref>{{Cite web|url=https://ss64.com/bash/chmod.html|title=chmod Man Page with examples and calculator - Linux - SS64.com|website=ss64.com}}</ref>
<code>chmod [options] mode[,mode] file1 [file2 ...]</code><ref>{{Cite web|url=https://ss64.com/bash/chmod.html|title=chmod Man Page with examples and calculator - Linux - SS64.com|website=ss64.com}}</ref>


Usually implemented options include:
Usually implemented options include:
* {{code|-R}} Recursive, i.e. include objects in subdirectories.
* {{code|-R}} Recursive, i.e. include objects in subdirectories.<!--
<!-- not in Mac OS * {{code|-f}} forge ahead; processing continues if errors occur. NOT force -->
not in Mac OS* {{code|-f}} forge ahead; processing continues if errors occur. NOT force
-->
* {{code|-v}} verbose, show objects changed (unchanged objects are not shown).
* {{code|-v}} verbose, show objects changed (unchanged objects are not shown).


Line 51: Line 53:
</syntaxhighlight>
</syntaxhighlight>


The {{code|r}}, {{code|w}}, and {{code|x}} specify the read, write, and execute access (the first character of the {{Mono|ls}} display denotes the object type; a [[hyphen]] represents a plain file). The script {{Mono|findPhoneNumbers.sh}} can be read, written to, and executed by the user {{Mono|dgerman}}; read and executed by members of the {{Mono|staff}} group; and only read by any other users.
The {{code|r}}, {{code|w}}, and {{code|x}} specify the read, write, and execute access respectively. The first character of the {{Mono|ls -l}} display denotes the object type; a [[hyphen]] represents a plain file. The script {{Mono|findPhoneNumbers.sh}} can be read, written to, and executed by the user {{Mono|dgerman}}; read and executed by members of the {{Mono|staff}} group; and only read by any other users.


The main parts of the {{Mono|chmod}} permissions:
The main parts of the {{Mono|chmod}} permissions:
Line 113: Line 115:


For example, {{code|754}} would allow:
For example, {{code|754}} would allow:
* "read" (4), "write" (2), and "execute" (1) for the ''User'' class i.e. 7 (4+2+1).
* "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).
* "read" (4) and "execute" (1) for the ''Group'' class; i.e., 5 (4 + 1).
* Only "read" (4) for the ''Others'' class.
* Only "read" (4) for the ''Others'' class.


A numerical code permits execution if and only if it is [[Odd number|odd]] (i.e. {{code|1}}, {{code|3}}, {{code|5}}, or {{code|7}}).
A numerical code permits execution if and only if it is [[Odd number|odd]] (i.e. {{code|1}}, {{code|3}}, {{code|5}}, or {{code|7}}). A numerical code permits "read" if and only if it is greater than or equal to {{code|4}} (i.e. {{code|4}}, {{code|5}}, {{code|6}}, or {{code|7}}). A numerical code permits "write" if and only if it is {{code|2}}, {{code|3}}, {{code|6}}, or {{code|7}}.
A numerical code permits "read" if and only if it is greater than or equal to {{code|4}} (i.e. {{code|4}}, {{code|5}}, {{code|6}}, or {{code|7}}).
A numerical code permits "write" if and only if it is {{code|2}}, {{code|3}}, {{code|6}}, or {{code|7}}.


=== Numeric example ===
=== Numeric example ===
Change permissions to permit members of the {{Mono|programmers}} group to update a file:
Change permissions to permit the {{Mono|programmers}} update of a file:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ ls -l sharedFile
$ ls -l File
-rw-r--r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile
-rw-r--r-- 1 jsmith programmers 57 Jul 3 10:13 File
$ chmod 664 sharedFile
$ chmod 664 File
$ ls -l sharedFile
$ ls -l File
-rw-rw-r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile
-rw-rw-r-- 1 jsmith programmers 57 Jul 3 10:13 File
</syntaxhighlight>
</syntaxhighlight>


Since the {{Mono|setuid}}, {{Mono|setgid}} and {{Mono|sticky}} bits are not specified, this is equivalent to:
Since the {{Mono|setuid}}, {{Mono|setgid}} and {{Mono|sticky}} bits are not specified, this is equivalent to:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ chmod 0664 sharedFile
$ chmod 0664 File
</syntaxhighlight>
</syntaxhighlight>


Line 158: Line 158:
|-
|-
| {{Mono|a}} || all || all three of the above, same as {{code|ugo}}
| {{Mono|a}} || all || all three of the above, same as {{code|ugo}}
|-
| ''(empty)'' || default || same as "all", except that bits in the [[umask]] will be unchanged
|}
|}


Line 185: Line 187:
| {{Mono|X}} || special execute || which is not a permission in itself but rather can be used instead of {{Mono|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 {{code|+}} and usually in combination with the {{code|-R}} flag for giving ''Group'' or ''Others'' access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used {{code|chmod -R a+rx .}}, whereas with {{Mono|X}} you can do {{code|chmod -R a+rX .}} instead
| {{Mono|X}} || special execute || which is not a permission in itself but rather can be used instead of {{Mono|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 {{code|+}} and usually in combination with the {{code|-R}} flag for giving ''Group'' or ''Others'' access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used {{code|chmod -R a+rx .}}, whereas with {{Mono|X}} you can do {{code|chmod -R a+rX .}} instead
|-
|-
| {{Mono|s}} || setuid/gid || details in [[#Special modes|Special modes section]]
| {{Mono|s}} || setuid/gid || {{see|#Special modes}}
|-
|-
| {{Mono|t}} || sticky || details in [[#Special modes|Special modes section]]
| {{Mono|t}} || sticky || {{see|#Special modes}}
|}
|}


Line 195: Line 197:
* Add write permission ({{Mono|w}}) to the Group's ({{Mono|g}}) access modes of a directory, allowing users in the same group to add files:
* Add write permission ({{Mono|w}}) to the Group's ({{Mono|g}}) access modes of a directory, allowing users in the same group to add files:
<syntaxhighlight lang="console" highlight="3">
<syntaxhighlight lang="console" highlight="3">
$ ls -ld shared_dir # show access modes before chmod
$ ls -ld dir # show access modes before chmod
drwxr-xr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir
drwxr-xr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir
$ chmod g+w shared_dir
$ chmod g+w dir
$ ls -ld shared_dir # show access modes after chmod
$ ls -ld dir # show access modes after chmod
drwxrwxr-x 2 nfinny eastregion 96 Apr 8 12:53 shared_dir
drwxrwxr-x 2 jsmitt northregion 96 Apr 8 12:53 shared_dir
</syntaxhighlight>
</syntaxhighlight>

* Remove write permissions ({{Mono|w}}) for all classes ({{Mono|a}}), preventing anyone from writing to the file:
* Remove write permissions ({{Mono|w}}) for all classes ({{Mono|a}}), preventing anyone from writing to the file:
<syntaxhighlight lang="console" highlight="3">
<syntaxhighlight lang="console" highlight="3">
Line 208: Line 209:
$ chmod a-w ourBestReferenceFile
$ chmod a-w ourBestReferenceFile
$ ls -l ourBestReferenceFile
$ ls -l ourBestReferenceFile
-r--r--r-- 2 ebowman northregion 96 Apr 8 12:53 ourBestReferenceFile
-r--r--r-- 2 tmiller northregion 96 Apr 8 12:53 ourBestReferenceFile
</syntaxhighlight>
</syntaxhighlight>
* Set the permissions for the ''{{strong|u}}ser'' and the Group ({{Mono|ug}}) to read and execute ({{Mono|rx}}) only (no write permission) on {{Mono|referenceLib}}, preventing anyone from adding files.

* Set the permissions for the ''{{strong|u}}ser'' and the Group ({{Mono|ug}}) to read and execute ({{Mono|rx}}) only (no write permission) on {{Mono|referenceLib}}, preventing anyone to add files.
<syntaxhighlight lang="console" highlight="3">
<syntaxhighlight lang="console" highlight="3">
$ ls -ld referenceLib
$ ls -ld referenceLib
Line 217: Line 217:
$ chmod ug=rx referenceLib
$ chmod ug=rx referenceLib
$ ls -ld referenceLib
$ ls -ld referenceLib
dr-xr-x--- 2 dhinkle northregion 96 Apr 8 12:53 referenceLib
dr-xr-x--- 2 ebowman northregion 96 Apr 8 12:53 referenceLib
</syntaxhighlight>
</syntaxhighlight>

<!-- Use meaning full unames and groups -DGerman -->
* Add the read and write permissions to the user and group classes of a file or directory named {{mono|sample}}:
* Add the read and write permissions to the user and group classes of a file or directory named {{mono|sample}}:
<syntaxhighlight lang="console" highlight="1">
<syntaxhighlight lang="console" highlight="1">
Line 227: Line 225:
drw-rw---- 2 rsanchez budget 96 Dec 8 12:53 sample
drw-rw---- 2 rsanchez budget 96 Dec 8 12:53 sample
</syntaxhighlight>
</syntaxhighlight>
* Remove all permissions, allowing no one to read, write, or execute the file named {{mono|sample}} to no useful end.

<!-- don't show unless configurations
* Remove all permissions, allowing no one to read, write, or execute the file named {{mono|sample}}.
<syntaxhighlight lang="console" highlight="1">
<syntaxhighlight lang="console" highlight="1">
$ chmod a-rwx sample
$ chmod a-rwx sample
Line 235: Line 231:
---------- 2 rswven planning 96 Dec 8 12:53 sample
---------- 2 rswven planning 96 Dec 8 12:53 sample
</syntaxhighlight>
</syntaxhighlight>
-->
* Change the permissions for the user and the group to read and execute only (no write permission) on {{mono|sample}}.
* Change the permissions for the user and the group to read and execute only (no write permission) on {{mono|sample}}.
<syntaxhighlight lang="console" highlight="4">
<syntaxhighlight lang="console" highlight="4">
Line 243: Line 238:
$ chmod ug=rx sample
$ chmod ug=rx sample
$ ls -ld sample
$ ls -ld sample
dr-xr-x--- 2 aolensky warehousing 96 Dec 8 12:53 NJ_DBs
dr-xr-x--- 2 oschultz warehousing 96 Dec 8 12:53 NJ_DBs
</syntaxhighlight>
* Set the item writable for the '''u'''ser while making it read-only for anyone else with only one command:
<syntaxhighlight lang="console">
$ chmod u=rw,go=r sample
$ ls -ld sample
drw-r--r-- 2 oschultz warehousing 96 Dec 8 12:53 sample
</syntaxhighlight>
</syntaxhighlight>


Line 249: Line 250:
{{See also|File-system permissions}}
{{See also|File-system permissions}}


The {{Mono|chmod}} command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use '{{Mono|s}}' to represent the ''[[setuid]]'' and ''[[setgid]]'' modes, and '{{Mono|t}}' to represent the ''[[Sticky bit|sticky]]'' mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.
The {{Mono|chmod}} command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use '{{Mono|s}}' to represent the ''[[setuid]]'' and ''[[setgid]]'' modes, and '{{Mono|t}}' to represent the ''[[sticky bit|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.
Most operating systems support the specification of special modes numerically, particularly in octal, but some do not. On these systems, only the symbolic modes can be used.


===Command line examples===
===Command line examples===
Line 258: Line 259:
! Command !! Explanation
! Command !! Explanation
|-
|-
| <code>chmod a+r ''publicComments.txt''</code> || adds read permission for all classes (i.e. ''{{strong|u}}ser'', ''Group'' and ''Others'')
|<code>chmod&nbsp;a+r&nbsp;publicComments.txt</code> || Adds read permission for all classes (i.e. ''{{strong|u}}ser'', ''Group'' and ''Others'')
|-
|-
| <code>chmod a-x ''publicComments.txt''</code> || removes execute permission for all classes
| <code>chmod a-x publicComments.txt</code> || Removes execute permission for all classes
|-
|-
| <code>chmod a+rx ''viewer.sh''</code> || adds read and execute permissions for all classes
| <code>chmod a+rx viewer.sh</code> || Adds read and execute permissions for all classes
|-
|-
| <code>chmod u=rw,g=r,o= ''internalPlan.txt''</code> || sets read and write permission for ''{{strong|u}}ser'', sets read for ''Group'', and denies access for ''Others''
| <code>chmod&nbsp;u=rw,g=r,o=&nbsp;internalPlan.txt''</code> || Sets read and write permission for ''{{strong|u}}ser'', sets read for ''Group'', and denies access for ''Others''
|-
|-
| <code>chmod -R u+w,go-w ''docs''</code> || adds write permission to the directory ''docs'' and all its contents (i.e. '''R'''ecursively) for owner, and removes write permission for group and others
| <code>chmod -R u+w,go-w docs</code> || Adds write permission to the directory docs and all its contents (i.e. '''R'''ecursively) for owner, and removes write permission for group and others
|-
|-
| <code>chmod ug=rw ''groupAgreements.txt''</code> || sets read and write permissions for ''{{strong|u}}ser'' and ''Group''
| <code>chmod ug=rw groupAgreements.txt</code> || Sets read and write permissions for ''{{strong|u}}ser'' and ''Group''
|-
|-
| <code>chmod 664 ''global.txt''</code> || sets read and write permissions for ''{{strong|u}}ser'' and ''Group'', and provides read to ''Others''.
| <code>chmod 664 global.txt</code> || Sets read and write permissions for ''{{strong|u}}ser'' and ''Group'', and provides read to ''Others''.
|-
|-
| <code>chmod 744 ''Show_myCV.sh''</code> || sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read permission for ''Group'' and ''Others''
| <code>chmod 744 Show_myCV.sh</code> || sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read permission for ''Group'' and ''Others''
|-
|-
| <code>chmod 1755 ''findReslts.sh''</code> || 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)
| <code>chmod 1755 findReslts.sh</code> || Sets sticky bit (this suggests that the script be retained in memory), sets read, write, and execute permissions for owner, and sets read and execute permissions for group and others
|-
|-
| <code>chmod 4755 ''setCtrls.sh''</code> || sets ''[[Setuid|UID]]'', sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read and execute permissions for ''Group'' and ''Others''
| <code>chmod 4755 setCtrls.sh</code> || Sets ''[[Setuid|UID]]'', sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read and execute permissions for ''Group'' and ''Others''
|-
|-
| <code>chmod 2755 ''setCtrls.sh''</code> || sets ''[[Setgid|GID]]'', sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read and execute permissions for ''Group'' and ''Others''
| <code>chmod 2755 setCtrls.sh</code> || sets ''[[Setgid|GID]]'', Sets read, write, and execute permissions for ''{{strong|u}}ser'', and sets read and execute permissions for ''Group'' and ''Others''
|-
|-
| <code>chmod -R u+rwX,g-rwx,o-rx ''personalStuff''</code> || '''R'''ecursively (i.e. on all files and directories in ''personalStuff'') adds read, write, and special execution permissions for ''{{strong|u}}ser'', removes read, write, and execution permissions for ''Group'', and removes read and execution permissions for ''Others''
| <code>chmod&nbsp;-R&nbsp;u+rw,g-,o-rx&nbsp;privateStuff</code> || '''R'''ecursively (i.e. on all files and directories in ''privateStuff'') adds read, write permissions for ''{{strong|u}}ser'', removes read, write, and execution permissions for ''Group'', and removes read and execution permissions for ''Others''
|-
|-
| <code>chmod -R a-x+X ''publicDocs''</code> || '''R'''ecursively (i.e. on all files and directories in ''publicDocs'') removes execute permission for all classes and adds special execution permission for all classes
| <code>chmod -R a-x+X publicDocs</code> || '''R'''ecursively (i.e. on all files and directories in ''publicDocs'') removes execute permission for all classes and adds special execution permission for all classes
|}
|}



Latest revision as of 01:27, 16 October 2024

chmod
Original author(s)AT&T Bell Laboratories
Developer(s)Various open-source and commercial developers
Initial release3 November 1971; 53 years ago (1971-11-03)
Written inPlan 9: C
Operating systemUnix, Unix-like, Plan 9, Inferno, IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils: GPLv3
Plan 9: MIT License

In Unix and Unix-like operating systems, chmod is the command and system call used to change the access permissions and the special mode flags (the setuid, setgid, and sticky flags) of file system objects (files and directories). Collectively these were originally called its modes,[1] and the name chmod was chosen as an abbreviation of change mode.[2]

History

[edit]

A chmod command first appeared in AT&T Unix version 1, along with the chmod system call.

As systems grew in number and types of users, access-control lists[3] were added to many file systems in addition to these most basic modes to increase flexibility.

The version of chmod bundled in GNU coreutils was written by David MacKenzie and Jim Meyering.[4] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[5] The chmod command has also been ported to the IBM i operating system.[6]

Command syntax

[edit]

Throughout this section, user refers to the owner of the file, as a reminder that the symbolic form of the command uses "u", to avoid confusion with "other".

Note that only the user or the superuser (root) is able to change file permissions.

chmod [options] mode[,mode] file1 [file2 ...][7]

Usually implemented options include:

  • -R Recursive, i.e. include objects in subdirectories.
  • -v verbose, 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.

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 r, w, and x specify the read, write, and execute access respectively. The first character of the ls -l display denotes the object type; a hyphen represents a plain file. The script findPhoneNumbers.sh 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:

For example: rwxr-x---

Each group of three characters 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, r-x, define permissions for the Group class (i.e. the group owning the file)
  • the rightmost 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.

Numerical permissions

[edit]

The chmod numerical format accepts up to four 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.

# Sum rwx Permission
7 4(r) + 2(w) + 1(x) rwx read, write and execute
6 4(r) + 2(w) rw- read and write
5 4(r)        + 1(x) r-x read and execute
4 4(r) r-- read only
3        2(w) + 1(x) -wx write and execute
2        2(w) -w- write only
1               1(x) --x execute only
0 0 --- none

For example, 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. 1, 3, 5, or 7). A numerical code permits "read" if and only if it is greater than or equal to 4 (i.e. 4, 5, 6, or 7). A numerical code permits "write" if and only if it is 2, 3, 6, or 7.

Numeric example

[edit]

Change permissions to permit the programmers update of a file:

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

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

$ chmod 0664 File

Symbolic modes

[edit]

The chmod command also accepts a finer-grained symbolic notation,[8] 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:

Reference Class 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 all three of the above, same as ugo
(empty) default same as "all", except that bits in the umask will be unchanged

The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:

Operator Description
+ 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:

Mode Name Description
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 + and usually in combination with the -R flag for giving Group or Others access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used chmod -R a+rx ., whereas with X you can do chmod -R a+rX . instead
s setuid/gid
t sticky

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.[9]

Symbolic examples

[edit]
  • 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 dir # show access modes before chmod
drwxr-xr-x   2 jsmitt  northregion 96 Apr 8 12:53 shared_dir
$ chmod  g+w dir
$ ls -ld dir  # show access modes after chmod
drwxrwxr-x   2 jsmitt  northregion 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 tmiller  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 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
  • 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
  • Remove all permissions, allowing no one to read, write, or execute the file named sample to no useful end.
$ chmod a-rwx sample
$ ls -l sample
----------   2 rswven  planning       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 oschultz  warehousing       96 Dec  8 12:53 NJ_DBs
  • Set the item writable for the user while making it read-only for anyone else with only one command:
$ chmod u=rw,go=r sample
$ ls -ld sample
drw-r--r--   2 oschultz  warehousing       96 Dec  8 12:53 sample

Special modes

[edit]

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 numerically, particularly in octal, but some do not. On these systems, only the symbolic modes can be used.

Command line examples

[edit]
Command Explanation
chmod a+r publicComments.txt Adds read permission for all classes (i.e. user, Group and Others)
chmod a-x publicComments.txt Removes execute permission for all classes
chmod a+rx viewer.sh Adds read and execute permissions for all classes
chmod u=rw,g=r,o= internalPlan.txt Sets read and write permission for user, sets read for Group, and denies access for Others
chmod -R u+w,go-w docs Adds write permission to the directory docs and all its contents (i.e. Recursively) for owner, and removes write permission for group and others
chmod ug=rw groupAgreements.txt Sets read and write permissions for user and Group
chmod 664 global.txt Sets read and write permissions for user and Group, and provides read to Others.
chmod 744 Show_myCV.sh sets read, write, and execute permissions for user, and sets read permission for Group and Others
chmod 1755 findReslts.sh Sets sticky bit (this suggests that the script be retained in memory), sets read, write, and execute permissions for owner, and sets read and execute permissions for group and others
chmod 4755 setCtrls.sh Sets UID, sets read, write, and execute permissions for user, and sets read and execute permissions for Group and Others
chmod 2755 setCtrls.sh sets GID, Sets read, write, and execute permissions for user, and sets read and execute permissions for Group and Others
chmod -R u+rw,g-,o-rx privateStuff Recursively (i.e. on all files and directories in privateStuff) adds read, write permissions for user, removes read, write, and execution permissions for Group, and removes read and execution permissions for Others
chmod -R a-x+X publicDocs Recursively (i.e. on all files and directories in publicDocs) removes execute permission for all classes and adds special execution permission for all classes

See also

[edit]

References

[edit]
  1. ^ The modes/permissions are shown when listing files in long format.
  2. ^ "Tutorial for chmod". catcode.com.
  3. ^ "AIX 5.3 System management". IBM knowledge Center. IBM. Retrieved 30 August 2015.
  4. ^ "chmod(1): change file mode bits - Linux man page". linux.die.net.
  5. ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  6. ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 5 September 2020.
  7. ^ "chmod Man Page with examples and calculator - Linux - SS64.com". ss64.com.
  8. ^ "AIX 5.5 Commands Reference". IBM Knowledge Center. IBM. Retrieved 30 August 2015.
  9. ^ "Permissions masking with umask, chmod, 777 octal permissions". teaching.idallen.com.
[edit]