Английская Википедия:Chmod

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:Lowercase title Шаблон:Infobox software In Unix and Unix-like operating systems, Шаблон:Mono 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 Шаблон:Mono was chosen as an abbreviation of change mode.[2]

History

A Шаблон:Mono command first appeared in AT&T Unix version 1, along with the Шаблон:Mono 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 Шаблон:Mono 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 Шаблон:Mono command has also been ported to the IBM i operating system.[6]

Шаблон:See also

Command syntax

Throughout this section, Шаблон:Strongser 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 ...][7]

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|Шаблон:Code]] or [[stat (Unix)|Шаблон:Code]] 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 Шаблон:Code, Шаблон:Code, and Шаблон:Code specify the read, write, and execute access (the first character of the Шаблон:Mono display denotes the object type; a hyphen represents a plain file). The script Шаблон:Mono can be read, written to, and executed by the user Шаблон:Mono; read and executed by members of the Шаблон:Mono group; and only read by any other users.

The main parts of the Шаблон:Mono permissions:

For example: Шаблон:Code

Each group of three characters define permissions for each class:

  • the three leftmost characters, Шаблон:Code, define permissions for the User class (i.e. the file owner).
  • the middle three characters, Шаблон:Code, define permissions for the Group class (i.e. the group owning the file)
  • the rightmost three characters, Шаблон:Code, 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

The Шаблон:Mono 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 Шаблон:Mono, Шаблон:Mono, and Шаблон:Mono 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
Шаблон:Code Шаблон:Mono Шаблон:Code read, write and execute
Шаблон:Code Шаблон:Mono Шаблон:Code read and write
Шаблон:Code Шаблон:Mono Шаблон:Code read and execute
Шаблон:Code Шаблон:Mono Шаблон:Code read only
Шаблон:Code Шаблон:Mono Шаблон:Code write and execute
Шаблон:Code Шаблон:Mono Шаблон:Code write only
Шаблон:Code Шаблон:Mono Шаблон:Code execute only
Шаблон:Code Шаблон:Mono Шаблон:Code none

For example, Шаблон:Code 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. Шаблон:Code, Шаблон:Code, Шаблон:Code, or Шаблон:Code). A numerical code permits "read" if and only if it is greater than or equal to Шаблон:Code (i.e. Шаблон:Code, Шаблон:Code, Шаблон:Code, or Шаблон:Code). A numerical code permits "write" if and only if it is Шаблон:Code, Шаблон:Code, Шаблон:Code, or Шаблон:Code.

Numeric example

Change permissions to permit members of the Шаблон:Mono 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 Шаблон:Mono, Шаблон:Mono and Шаблон:Mono bits are not specified, this is equivalent to:

$ chmod 0664 sharedFile

Шаблон:See also

Symbolic modes

Шаблон:See also

The Шаблон:Mono 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
Шаблон:Mono user file owner
Шаблон:Mono group members of the file's group
Шаблон:Mono others users who are neither the file's owner nor members of the file's group
Шаблон:Mono all all three of the above, same as Шаблон:Code
(empty) default same as "all", except that bits in the umask will be unchanged

The Шаблон:Mono program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:

Operator Description
Шаблон:Mono adds the specified modes to the specified classes
Шаблон:Mono removes the specified modes from the specified classes
Шаблон:Mono 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
Шаблон:Mono read read a file or list a directory's contents
Шаблон:Mono write write to a file or directory
Шаблон:Mono execute execute a file or recurse a directory tree
Шаблон:Mono special execute which is not a permission in itself but rather can be used instead of Шаблон:Mono. 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 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, whereas with Шаблон:Mono you can do Шаблон:Code instead
Шаблон:Mono setuid/gid Шаблон:See
Шаблон:Mono sticky Шаблон:See

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

  • Add write permission (Шаблон:Mono) to the Group's (Шаблон:Mono) 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 jsmitt  northregion 96 Apr 8 12:53 shared_dir
    
  • Remove write permissions (Шаблон:Mono) for all classes (Шаблон:Mono), 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 Шаблон:Strongser and the Group (Шаблон:Mono) to read and execute (Шаблон:Mono) only (no write permission) on Шаблон:Mono, 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 Шаблон:Mono:
    $ 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 Шаблон:Mono 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 Шаблон:Mono.
    $ # 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
    

Special modes

Шаблон:See also

The Шаблон:Mono command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use 'Шаблон:Mono' to represent the setuid and setgid modes, and 'Шаблон:Mono' 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

Command Explanation
chmod a+r publicComments.txt Adds read permission for all classes (i.e. Шаблон:Strongser, 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 Шаблон:Strongser, 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 Шаблон:Strongser and Group
chmod 664 global.txt Sets read and write permissions for Шаблон:Strongser and Group, and provides read to Others.
chmod 744 Show_myCV.sh sets read, write, and execute permissions for Шаблон:Strongser, 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 Шаблон:Strongser, and sets read and execute permissions for Group and Others
chmod 2755 setCtrls.sh sets GID, Sets read, write, and execute permissions for Шаблон:Strongser, 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 Шаблон:Strongser, 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

References

Шаблон:Reflist

External links

Шаблон:Wikibooks

Шаблон:Unix commands Шаблон:Plan 9 commands Шаблон:Core Utilities commands Шаблон:Use dmy dates