Chmod
Encyclopedia
The chmod command is a Unix
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

 command that lets a user tell the system how much (or little) access it should permit to a file. It changes the file system modes of files
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

 and directories
Directory (file systems)
In computing, a folder, directory, catalog, or drawer, is a virtual container originally derived from an earlier Object-oriented programming concept by the same name within a digital file system, in which groups of computer files and other folders can be kept and organized.A typical file system may...

. The modes include permissions
File system permissions
Most current file systems have methods of administering permissions or access rights to specific users and groups of users. These systems control the ability of the users to view or make changes to the contents of the filesystem....

 and special modes. It is also a C language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 function in Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 and Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 environments.

History

A chmod command first appeared in AT&T
AT&T
AT&T Inc. is an American multinational telecommunications corporation headquartered in Whitacre Tower, Dallas, Texas, United States. It is the largest provider of mobile telephony and fixed telephony in the United States, and is also a provider of broadband and subscription television services...

 Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 version 1, and is still used today on Unix like machines.

Usage

The chmod command options are specified like this:


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


This is used to control the file mode.

To view the current file mode:


$ ls -l file

or use the stat command to view the octal numerical values ("*" lists all files in current directory)

$ stat -c '%A %a %n' *

Octal numbers

See also: Octal notation of file system permissions


The chmod command accepts up to four digits to represent an octal number. The octets refer to bits applied to the file owner, group and other users, respectively. Use of three digits is discouraged because it leaves the fourth as the default and this value is not fixed. The least significant digit sets/resets an additional mode for each of these three sets of bits. Experienced Unix and Linux users tend to recommend that the user of this command check the man page (man chmod) on the system of interest.

Particular care should be taken when a directory is the target because the effect is not intuitive. In addition, it will not work on all file types. For example, it has no effect on a symbolic link.
myfile :


$ chmod 664 myfile
$ ls -l myfile
-rw-rw-r-- 1 57 Jul 3 10:13 myfile


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


$ chmod 0664 myfile


Symbolic modes

See also: Symbolic notation of file system permissions


chmod also accepts finer-grained symbolic notation, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:


$ chmod [references][operator][modes] file1 ...


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
Umask
umask is a command and a function in POSIX environments that sets the file mode creation mask of the current process which limits the permission modes for files and directories created by the process...

. The references are represented by one or more of the following letters:
Reference Class Description
u user the owner of the file
g group users who are members of the file's group
o others users who are not the owner of the file or members of the group
a all all three of the above, is the 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:
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 taken away 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 1 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
s setuid/gid details in Special modes section
t sticky details in Special modes section


The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas.

Numerical Permissions
# Permission
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none

Symbolic examples

Add the read and write permissions to the user and group classes of a directory:


$ chmod ug+rw mydir
$ ls -ld mydir
drw-rw---- 2 unixguy uguys 96 Dec 8 12:53 mydir


For a file, remove write permissions for all classes:


$ chmod a-w myfile
$ ls -l myfile
-r-xr-xr-x 2 unixguy uguys 96 Dec 8 12:53 myfile


Set the permissions for the user and the group to read and execute only (no write permission) on mydir.


$ chmod ug=rx mydir
$ ls -ld mydir
dr-xr-x--- 2 unixguy uguys 96 Dec 8 12:53 mydir

Special modes

See also: File system permissions
File system permissions
Most current file systems have methods of administering permissions or access rights to specific users and groups of users. These systems control the ability of the users to view or make changes to the contents of the filesystem....



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
Setuid
setuid and setgid are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group...

and setgid modes, and t to represent the sticky
Sticky bit
In computing, the sticky bit is an access-right flag that can be assigned to files and directories on Unix systems.-History:The sticky bit was introduced in the Fifth Edition of Unix in 1974 for use with pure executable files. When set, it instructed the operating system to retain the text segment...

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

command explanation
chmod a+r file read is added for all
chmod a-x file execute permission is removed for all
chmod a+rw file change the permissions of the file file to read and write for all.
chmod +rwx file On some UNIX platforms such as BSD, this will restore the permission of the file file to default: -rwxr-xr-x.
chmod u=rw,go= file read and write is set for the owner, all permissions are cleared for the group and others
chmod -R u+w,go-w docs change 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 file removes all privileges for all
chmod 777 file change the permissions of the file file to read, write, and execute for all.
chmod 664 file sets read and write and no execution access for the owner and group, and read, no write, no execute for all others.
chmod 0755 file equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 specifies no special modes.
chmod 4755 file the 4 specifies set user ID
Setuid
setuid and setgid are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group...

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 directory set a directory tree to rwx for owner directories, rw for owner files, --- for group and others.
chmod -R a-x+X directory remove the execute permission on all files in a directory tree, while allowing for directory browsing.

Function details

For C programming language, the function prototype is defined:

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

The function takes a parameter of type mode_t, which is a bitfield composed of various flags:
flag octal value purpose
S_ISUID 04000 set user ID on execution
S_ISGID 02000 set group ID on execution
S_ISVTX 01000 sticky bit
S_IRUSR, S_IREAD 00400 read by owner
S_IWUSR, S_IWRITE 00200 write by owner
S_IXUSR, S_IEXEC 00100 execute/search by owner
S_IRGRP 00040 read by group
S_IWGRP 00020 write by group
S_IXGRP 00010 execute/search by group
S_IROTH 00004 read by others
S_IWOTH 00002 write by others
S_IXOTH 00001 execute/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.

Various error codes can be returned, and these are detailed in the associated man page for the function.

See also

  • File system permissions
    File system permissions
    Most current file systems have methods of administering permissions or access rights to specific users and groups of users. These systems control the ability of the users to view or make changes to the contents of the filesystem....

  • chown
    Chown
    The chown command is used on Unix-like systems to change the owner of a file. In most implementations, it can only be executed by the superuser. Unprivileged users who wish to change the group of a file that they own may use chgrp.-Usage examples:These examples illustrate typical syntax and use...

    , the command used to change the owner of a file or directory on Unix-like systems
  • chgrp
    Chgrp
    The chgrp command is used by unprivileged users on Unix-like systems to change the group associated with a computer file...

    , the command used to change the group of a file or directory on Unix-like systems
  • cacls
    Cacls
    cacls and icacls are Microsoft Windows native command line utilities capable of displaying and modifying the access control lists on folders and files...

    , a command used on Windows NT
    Windows NT
    Windows NT is a family of operating systems produced by Microsoft, the first version of which was released in July 1993. It was a powerful high-level-language-based, processor-independent, multiprocessing, multiuser operating system with features comparable to Unix. It was intended to complement...

     and its derivatives to modify the access control lists associated with a file or directory
  • attrib
    Attrib
    attrib is a command, in DOS, OS/2 and Microsoft Windows. The function of attrib is to set and remove file attributes...

  • User ID
    User identifier (Unix)
    Unix-like operating systems identify users within the kernel by an unsigned integer value called a user identifier, often abbreviated to UID or User ID...

  • Group ID
    Group identifier (Unix)
    In Unix-like systems, multiple users can be categorized into groups. POSIX and conventional Unix file system permissions are organized into three classes, user, group, and others. The use of groups allows additional abilities to be delegated in an organized fashion, such as access to disks,...

  • List of Unix programs

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK