chmod

From Wikipedia, the free encyclopedia - View original article

 
Jump to: navigation, search

In Unix-like operating systems, chmod is the name of a Unix shell command and a system call, which both change the access permissions to file system objects (including files and directories), as well as specifying special flags.[1] The name is an abbreviation of change mode.

History[edit]

A chmod command first appeared in AT&T Unix version 1.

Command syntax[edit]

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

[2]

Usual implemented options include:

If a symbolic link is specified, the target object is affected. File modes directly associated with symbolic links themselves are typically never 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. This script can be read, written to, and executed by the user, read and executed by other members of the staff group and can also be read by others.

Octal modes[edit]

The chmod numeric format accepts up to four octal digits. The rightmost three refer to permissions for the file owner, the group, and other users. The next digit (fourth from the right) specifies special setuid, setgid, and sticky flags.

Numerical permissions

#Permissionrwx
7full111
6read and write110
5read and execute101
4read only100
3write and execute011
2write only010
1execute only001
0none000

Numeric example[edit]

In order to permit all users who are 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 

Symbolic modes[edit]

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 ... 

The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all” but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters:

ReferenceClassDescription
uuserthe owner of the file
ggroupusers who are members of the file's group
oothersusers who are neither the owner of the file nor members of the file's group
aallall three of the above, same as ugo

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

OperatorDescription
+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:

ModeNameDescription
rreadread a file or list a directory's contents
wwritewrite to a file or directory
xexecuteexecute a file or recurse a directory tree
Xspecial executewhich 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 other). It is only really useful when used with '+' and usually in combination with the -R option for giving group or other 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
ssetuid/giddetails in Special modes section
tstickydetails in Special modes section

Multiple changes can be specified by separating multiple symbolic modes with commas (without spaces).

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 shared_dir # show access modes before chmod drwxr-xr-x   2 teamleader  usguys 96 Apr 8 12:53 shared_dir $ chmod  g+w shared_dir $ ls -ld shared_dir  # show access modes after chmod drwxrwxr-x   2 teamleader  usguys 96 Apr 8 12:53 shared_dir 

Remove write permissions (w) for all classes (a),
preventing anyone from writing to the file:

 $ ls -l ourBlestReferenceFile -rw-rw-r--   2 teamleader  usguys 96 Apr 8 12:53 ourBlestReferenceFile $ chmod a-w ourBestReferenceFile $ ls -l ourBlestReferenceFile -r--r--r--   2 teamleader  usguys 96 Apr 8 12:53 ourBlestReferenceFile 

Set the permissions for the user and the group (ug) to read and execute (rx) only (no write permission) on referenceLib,
preventing anyone other than the owner to add files.

 $ ls -ld referenceLib drwxr-----   2 teamleader  usguys 96 Apr 8 12:53 referenceLib $ chmod ug=rx referenceLib $ ls -ld referenceLib dr-xr-x---   2 teamleader  usguys 96 Apr 8 12:53 referenceLib 

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

Command line examples[edit]

commandexplanation
chmod a+r fileread is added for all
chmod a-x fileexecute permission is removed for all
chmod a+rw filechange the permissions of the file file to read and write for all.
chmod +rwx fileOn some UNIX platforms such as BSD, this will restore the permission of the file file to default: -rwxr-xr-x.
chmod u=rw,go= fileread and write is set for the owner, all permissions are cleared for the group and others
chmod -R u+w,go-w docschange the permissions of the directory docs and all its contents to add write access for the user, and deny write access for everybody else.
chmod = fileremoves all privileges for all.
chmod 777 filechange the permissions of the file file to read, write, and execute for all.
chmod 664 filesets read and write and no execution access for the owner and group, and read, no write, no execute for all others.
chmod 0755 fileequivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 specifies no special modes.
chmod 4755 filethe 4 specifies set user ID and the rest is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1).
chmod 2755 filethe 2 specifies set group ID and the rest is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1).
chmod -R u+rwX,g-rwx,o-rwx directoryset a directory tree to rwx for owner directories, rw for owner files, --- for group and others.
chmod -R a-x+X directoryremove the execute permission on all files in a directory tree, while allowing for directory browsing.

System call[edit]

The POSIX standard defines the following function prototype:

 int chmod(const char *path, mode_t mode); 

The mode parameter is a bitfield composed of various flags:

FlagOctal valuePurpose
S_ISUID04000Set user ID on execution
S_ISGID02000Set group ID on execution
S_ISVTX01000Sticky bit
S_IRUSR, S_IREAD00400Read by owner
S_IWUSR, S_IWRITE00200Write by owner
S_IXUSR, S_IEXEC00100Execute/search by owner
S_IRGRP00040Read by group
S_IWGRP00020Write by group
S_IXGRP00010Execute/search by group
S_IROTH00004Read by others
S_IWOTH00002Write by others
S_IXOTH00001Execute/search by others

Where alternate flag names are given, one of the pair of names might not be supported on some OSs. The octal values of the flags are summed or combined in a bitwise OR operation to give the desired permission mode.

The function returns an error code.

Notable Example[edit]

In 2007, C-H-M-O-D was used as the letters for the Bonus Round of Wheel Of Fortune.

See also[edit]

References[edit]

External links[edit]