Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

chmod

From Wikipedia, the free encyclopedia
Shell command for changing access permissions of a file
chmod
Original authorAT&T Bell Laboratories
DevelopersVariousopen-source andcommercial developers
Initial release3 November 1971; 54 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

chmod is ashellcommand for changingaccess permissions and special mode flags offiles (includingspecial files such asdirectories). The name is short forchangemode wheremode refers to the permissions and flags collectively.[1][2]

The command originated inAT&T Unix version 1 and was exclusive toUnix andUnix-likeoperating systems until it was ported to other operating systems such asWindows (inUnxUtils)[3] andIBM i.[4]

InUnix andUnix-like operating systems, asystem 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 enhancedfile-system permissions grew,access-control lists[5] were added to many file systems to augment the modes controlled viachmod.

The implementation ofchmod bundled inGNU 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 (which specifiesall the mode bits on each file), or a comma-delimited list of symbolic specifiers (which describes how to change the existing mode bits of each file). The remaining arguments are a 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 asymbolic 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

[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 a numeric permissions argument, thechmod command treats it as anoctal number,[a] and replacesall the mode bits for each file. (Although four digits are specified, leading0 digits can be elided.)[8]

There are twelve standard mode bits, comprising three special bits (setuid,setgid, andsticky), and three permission groups (controlling access byuser,group, andother) of 3 bits each (read,write, andexec/scan); each permission bit grants access if set (1) or denies access if clear (0).

As an octal digit represents a 3-bit value, the twelve mode bits 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, 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.

Octal digit permission
#bitsrwxgranted operations
74 + 2 + 1rwxread, write and execute
64 + 2rw-read and write
54     + 1r-xread and execute
44r--read only
3    2 + 1-wxwrite and execute
2    2-w-write only
1        1--xexecute only
0---none

The commandstat can report a file's permissions as octal. For example:

$stat-c%afindPhoneNumbers.sh754

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 isodd (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]

Thechmod command accepts symbolic notation that specifies how to modify the existing permissions.[9] 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 all classes by not specifying a symbol; when using the last method, the bits of theumask mask will remain unchanged.[10] Class specifiers include:

Class specifiers
symboldescription
uuser: file owner
ggroup: members of the file's group
oothers: users who are neither the file's owner nor members of the file's group
aall three classes; same asugo

As ownership is key to access control, and since the symbolic specification uses the abbreviationo, some incorrectly think that it meansowner, when, in fact, it is short forothers.

The change operators include:

Operators
symboldescription
+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:

Operation specifiers
symboldescription
rread a regular file or list a directory's contents
wwrite to a file
xexecute a regular file or recurse a directory tree
Xspecial 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 withchmod -R a+rx .; instead usechmod -R a+rX .
ssetuid mode orsetgid mode
tsticky mode

Mostchmod implementations support the specification of the special modes in octal, but some do not which requires using the symbolic notation.

Thels command can report file permissions in a symbolic notation that is similar to the notation used withchmod.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-lfindPhoneNumbers.sh-rwxr-xr--  1 dgerman  staff  823 Dec 16 15:03 findPhoneNumbers.sh

The permission specifier-rwxr-xr-- starts with a dash, which indicates thatfindPhoneNumbers.sh is a regular file, not a directory. The next three lettersrwx indicate that the file can be read, written, and executed by the owning userdgerman. The next three lettersr-x indicate that the file can be read and executed by members of thestaff group. And the last three lettersr-- indicate that the file is read-only for other users.

Examples

[edit]

Addwrite permission to thegroup class of a directory, allowing users in the same group to add files:

$ls-lddir# beforedrwxr-xr-x   2 jsmitt  northregion 96 Apr 8 12:53 shared_dir$chmodg+wdir$ls-lddir# afterdrwxrwxr-x   2 jsmitt  northregion 96 Apr 8 12:53 shared_dir

Removewrite permission forall classes, preventing anyone from writing to the file:

$ls-lourBestReferenceFile-rw-rw-r--   2 tmiller  northregion 96 Apr 8 12:53 ourBestReferenceFile$chmoda-wourBestReferenceFile$ls-lourBestReferenceFile-r--r--r--   2 tmiller  northregion 96 Apr 8 12:53 ourBestReferenceFile

Set the permissions for theuser andgroup classes toread and execute only, with nowrite permission, preventing anyone from adding files:

$ls-ldreferenceLibdrwxr-----   2 ebowman  northregion 96 Apr 8 12:53 referenceLib$chmodug=rxreferenceLib$ls-ldreferenceLibdr-xr-x---   2 ebowman  northregion 96 Apr 8 12:53 referenceLib

Enablewrite for theuser class while making itread-only forgroup and others:

$chmodu=rw,go=rsample$ls-ldsampledrw-r--r--   2 oschultz  warehousing       96 Dec  8 12:53 sample

To recursively set access for the directorydocs/ 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]

Notes

[edit]
  1. ^Why octal rather than decimal? Although rarely used today, during the early development of UNIX, octal was very useful because repeating groups of 3 bits were common in the physical structure of computers at the time, and these bits were easier to read and understand when encoded as octal digits, just as groups of 4 bits are easier when grouped into hexadecimal digits. The numeric expression of filesystem permissions in octal is one of the few of the few remnants of this time.[original research?]

References

[edit]
  1. ^The modes/permissions are shown whenlisting files in long format.
  2. ^"Tutorial for chmod".catcode.com.Archived from the original on 20 May 2000. Retrieved26 August 2008.
  3. ^"Native Win32 ports of some GNU utilities".unxutils.sourceforge.net.Archived from the original on 9 February 2006. Retrieved9 August 2025.
  4. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Archived(PDF) from the original on 18 September 2020. Retrieved5 September 2020.
  5. ^"AIX 5.3 System management".IBM knowledge Center. IBM.Archived from the original on 4 March 2016. Retrieved30 August 2015.
  6. ^"chmod(1): change file mode bits - Linux man page".linux.die.net.Archived from the original on 9 January 2019. Retrieved6 January 2019.
  7. ^"chmod Man Page with examples and calculator - Linux - SS64.com".ss64.com.Archived from the original on 12 February 2003. Retrieved12 September 2020. (note that "space delimited" is a feature of theshell, not of chmod itself.)
  8. ^This differs from the “C” language, where the0 prefix for octal numbers is a remnant of its early period.
  9. ^"AIX 5.5 Commands Reference".IBM Knowledge Center. IBM.Archived from the original on 4 March 2016. Retrieved30 August 2015.
  10. ^"Permissions masking with umask, chmod, 777 octal permissions".teaching.idallen.com.Archived from the original on 22 February 2020. Retrieved9 March 2020.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Processes
User environment
Text processing
Shell builtins
Networking
Searching
Software development
Miscellaneous
File system
Text utilities
Shell utilities

Retrieved from "https://en.wikipedia.org/w/index.php?title=Chmod&oldid=1336327771"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp