Chmod

Last updated
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 system Unix, Unix-like, Plan 9, Inferno, IBM i
Platform Cross-platform
Type Command
License coreutils: 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]

Contents

History

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

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:

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-lfindPhoneNumbers.sh -rwxr-xr--  1 dgerman  staff  823 Dec 16 15:03 findPhoneNumbers.sh$ stat-c%afindPhoneNumbers.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 :

Numerical permissions

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.

#SumrwxPermission
74(r) + 2(w) + 1(x)rwxread, write and execute
64(r) + 2(w) rw-read and write
54(r)        + 1(x)r-xread and execute
44(r) r--read only
3       2(w) + 1(x)-wxwrite and execute
2       2(w) -w-write only
1              1(x)--xexecute only
00 ---none

For example, 754 would allow:

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

Change permissions to permit the programmers update of a file:

$ ls-lFile -rw-r--r--  1 jsmith programmers 57 Jul  3 10:13  File$ chmod664File $ ls-lFile -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:

$ chmod0664File 

Symbolic modes

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:

ReferenceClassDescription
uuserfile owner
ggroupmembers of the file's group
oothersusers who are neither the file's owner nor members of the file's group
aallall three of the above, same as ugo
(empty)defaultsame 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:

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 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
ssetuid/gid
tsticky

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 (w) to the Group's (g) access modes of a directory, allowing users in the same group to add files:
$ ls-lddir# show access modes before chmoddrwxr-xr-x   2 jsmitt  northregion 96 Apr 8 12:53 shared_dir$ chmodg+wdir $ ls-lddir# show access modes after chmoddrwxrwxr-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-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 the user and the Group (ug) to read and execute (rx) only (no write permission) on referenceLib, preventing anyone from adding files.
$ ls-ldreferenceLib drwxr-----   2 ebowman  northregion 96 Apr 8 12:53 referenceLib$ chmodug=rxreferenceLib $ ls-ldreferenceLib 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:
$ chmodug+rwsample $ ls-ldsample 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.
$ chmoda-rwxsample $ ls-lsample ----------   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-ldsample drw-rw----   2 oschultz  warehousing       96 Dec  8 12:53 NY_DBs$ chmodug=rxsample $ ls-ldsample 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:
$ chmodu=rw,go=rsample $ ls-ldsample drw-r--r--   2 oschultz  warehousing       96 Dec  8 12:53 sample

Special modes

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

CommandExplanation
chmod a+r publicComments.txtAdds read permission for all classes (i.e. user, Group and Others)
chmod a-x publicComments.txtRemoves execute permission for all classes
chmod a+rx viewer.shAdds read and execute permissions for all classes
chmod u=rw,g=r,o= internalPlan.txtSets read and write permission for user, sets read for Group, and denies access for Others
chmod -R u+w,go-w docsAdds 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.txtSets read and write permissions for user and Group
chmod 664 global.txtSets read and write permissions for user and Group, and provides read to Others.
chmod 744 Show_myCV.shsets read, write, and execute permissions for user, and sets read permission for Group and Others
chmod 1755 findReslts.shSets 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.shSets UID , sets read, write, and execute permissions for user, and sets read and execute permissions for Group and Others
chmod 2755 setCtrls.shsets 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 privateStuffRecursively (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 publicDocsRecursively (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

Related Research Articles

ls Command to list files and directories in Unix and Unix-like operating systems

In computing, ls is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.

dir (command) Directory information command on various operating systems

In computing, dir (directory) is a command in various computer operating systems used for computer file and directory listing. It is one of the basic commands to help navigate the file system. The command is usually implemented as an internal command in the command-line interpreter (shell). On some systems, a more graphical representation of the directory structure can be displayed using the tree command.

In computing, a symbolic link is a file whose purpose is to point to a file or directory by specifying a path thereto.

fstab is a system file commonly found in the directory /etc on Unix and Unix-like computer systems. In Linux, it is part of the util-linux package. The fstab file typically lists all available disk partitions and other types of file systems and data sources that may not necessarily be disk-based, and indicates how they are to be initialized or otherwise integrated into the larger file system structure.

split is a utility on Unix, Plan 9, and Unix-like operating systems most commonly used to split a computer file into two or more smaller files.

In computing, umask is a command that determines the settings of a mask that controls how file permissions are set for newly created files. It may also affect how the file permissions are changed explicitly. umask is also a function that sets the mask, or it may refer to the mask itself, which is formally known as the file mode creation mask. The mask is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files. The bits in the mask may be changed by invoking the umask command.

The Unix and Linux access rights flags setuid and setgid allow users to run an executable with the file system permissions of the executable's owner or group respectively and to change behaviour in directories. They are often used to allow users on a computer system to run programs with temporarily elevated privileges to perform a specific task. While the assumed user id or group id privileges provided are not always elevated, at a minimum they are specific.

In Unix-like systems, multiple users can be put 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, printers, and other peripherals. This method, among others, also enables the superuser to delegate some administrative tasks to normal users, similar to the Administrators group on Microsoft Windows NT and its derivatives.

Most file systems include attributes of files and directories that control the ability of users to read, change, navigate, and execute the contents of the file system. In some cases, menu options or functions may be made visible or hidden depending on a user's permission level; this kind of user interface is referred to as permission-driven.

The seven standard Unix file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket as defined by POSIX. Different OS-specific implementations allow more types than what POSIX requires. A file's type can be identified by the ls -l command, which displays the type in the first character of the file-system permissions field.

In Unix-like operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

chgrp Computer command

The chgrp command may be used by unprivileged users on various operating systems to change the group associated with a file system object to one of which they are a member. A file system object has 3 sets of access permissions, one set for the owner, one set for the group and one set for others. Changing the group of an object could be used to change which users can write to a file.

File attributes are a type of metadata that describe and may modify how files and/or directories in a filesystem behave. Typical file attributes may, for example, indicate or specify whether a file is visible, modifiable, compressed, or encrypted. The availability of most file attributes depends on support by the underlying filesystem where attribute data must be stored along with other control structures. Each attribute can have one of two states: set and cleared. Attributes are considered distinct from other metadata, such as dates and times, filename extensions or file system permissions. In addition to files, folders, volumes and other file system objects may have attributes.

In computing, the sticky bit is a user ownership access right flag that can be assigned to files and directories on Unix-like systems.

alias (command) Command in various command line interpreters

In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. alias is available in Unix shells, AmigaDOS, 4DOS/4NT, FreeDOS, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.

chattr is the command in Linux that allows a user to set certain attributes of a file. lsattr is the command that displays the attributes of a file.

Qshell is an optional command-line interpreter (shell) for the IBM i operating system. Qshell is based on POSIX and X/Open standards. It is a Bourne-like shell that also includes features of KornShell. The utilities are external programs that provide additional functions. The development team of Qshell had to deal with platform-specific issues such as translating between ASCII and EBCDIC. The shell supports interactive mode as well as batch processing and can run shell scripts from Unix-like operating systems with few or no modifications.

bs is a programming language and a compiler/interpreter for modest-sized programs on UNIX systems. The bs command can be invoked either for interactive programming or with a file containing a program, optionally taking arguments, via a Unix shell, e.g., using a Shebang (Unix) #!/usr/bin/bs.

References

  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.