Chmod
From Luke Jackson
Revision as of 02:43, 26 November 2007 (edit) Ljackson (Talk | contribs) (→Octal numbers) ← Previous diff |
Current revision (02:54, 26 November 2007) (edit) Ljackson (Talk | contribs) (→Octal numbers) |
||
Line 100: | Line 100: | ||
The <tt>chmod</tt> command also accepts three- and four-digit octal numbers representing modes. See the article mentioned above for more. Using a four-digit octal number to set the modes of a file or directory named <tt>sample</tt> would look something like this: | The <tt>chmod</tt> command also accepts three- and four-digit octal numbers representing modes. See the article mentioned above for more. Using a four-digit octal number to set the modes of a file or directory named <tt>sample</tt> would look something like this: | ||
- | $ chmod 0664 sample | + | chmod 0664 sample |
Assuming that the <tt>setuid</tt>, <tt>setgid</tt> and <tt>sticky</tt> bits are not set, this is equivalent to: | Assuming that the <tt>setuid</tt>, <tt>setgid</tt> and <tt>sticky</tt> bits are not set, this is equivalent to: | ||
- | $ chmod 664 sample | + | chmod 664 sample |
or | or | ||
- | $ chmod +r,-x,ug+w sample | + | chmod +r,-x,ug+w sample |
==Special modes== | ==Special modes== | ||
- | :''See also: File system permissions#Additional permissions|Additional file system permissions'' | ||
The <tt>chmod</tt> command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use <tt>s</tt> to represent the ''setuid'' and ''setgid'' modes, and <tt>t</tt> to represent the ''Sticky bit|sticky'' mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified. | The <tt>chmod</tt> command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use <tt>s</tt> to represent the ''setuid'' and ''setgid'' modes, and <tt>t</tt> to represent the ''Sticky bit|sticky'' mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified. | ||
Line 119: | Line 118: | ||
== Examples == | == Examples == | ||
- | *<tt>chmod +r file</tt> – read is added for all | + | Read is added for all |
- | *<tt>chmod -x file</tt> – execute permission is removed for all | + | |
- | *<tt>chmod u=rw,go= file</tt> – read and write is set for the owner, all permissions are cleared for the group and others | + | chmod +r <file> |
- | *<tt>chmod +rw file</tt> – change the permissions of the file ''file'' to read and write for all. | + | |
- | *<tt>chmod -R u+w,go-w docs/</tt> – 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. | + | Execute permission is removed for all |
- | *<tt>chmod 666 file</tt> – ADDS! read and write access for the owner, the group, and all others. | + | |
- | *<tt>chmod 0755 file</tt> – equivalent to <tt>u=rwx (4+2+1),go=rx (4+1 & 4+1)</tt>. The <tt>0</tt> specifies ''no special modes''. | + | chmod -x <file> |
- | *<tt>chmod 4755 file</tt> – the <tt>4</tt> specifies ''Setuid|set user ID''. | + | |
- | *<tt>find path/ -type d -exec chmod a-x {} \;</tt> – sets a-x for all directories in tree starting from path/ (use '-type f' to match files only). | + | Read and write is set for the owner, all permissions are cleared for the group and others |
- | *<tt>find path/ -type d -exec chmod 777 {} \; </tt> allows directory browsing ls for example if you've reset permissions for Samba write access. | + | |
- | *<tt>chmod -R u+rwX,g-rwx,o-rwx <directory></tt> – set a directory tree to rwx for owner directories, rw for owner files, --- for group and all. | + | chmod u=rw,go= <file> |
+ | |||
+ | Change the permissions of the file ''file'' to read and write for all. | ||
+ | |||
+ | chmod +rw <file> | ||
+ | |||
+ | 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 -R u+w,go-w docs/ | ||
+ | |||
+ | ADDS! read and write access for the owner, the group, and all others. | ||
+ | |||
+ | chmod 666 <file> | ||
+ | |||
+ | equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 specifies ''no special modes''. | ||
+ | |||
+ | chmod 0755 <file> | ||
+ | |||
+ | The 4 specifies ''Setuid (set user ID)''. | ||
+ | |||
+ | chmod 4755 <file> | ||
+ | |||
+ | Sets a-x for all directories in tree starting from path/ (use '-type f' to match files only). | ||
+ | |||
+ | find path/ -type d -exec chmod a-x {} \; | ||
+ | |||
+ | Allows directory browsing ls for example if you've reset permissions for Samba write access. | ||
+ | |||
+ | find path/ -type d -exec chmod 777 {} \; | ||
+ | |||
+ | set a directory tree to rwx for owner directories, rw for owner files, --- for group and all. | ||
+ | |||
+ | chmod -R u+rwX,g-rwx,o-rwx <directory> | ||
= Sources = | = Sources = | ||
* http://en.wikipedia.org/wiki/Chmod | * http://en.wikipedia.org/wiki/Chmod | ||
+ | |||
+ | [[Category:Linux]] | ||
+ | [[Category:Mac OS X]] |
Current revision
The chmod command (abbreviated from change mode) is a UNIX shell command in Unix and Unix-like environments.
When executed, the command can change file system modes of Computer file|files and directory (file systems)|directories. The modes include permissions and special modes.
Contents |
History
A chmod command first appeared in AT&T Unix version 1, and is still used today on Unix-like machines.
Usage
The chmod command options are specified like this:
$ chmod [options] mode file1 ...
To view what the permissions currently are, type:
$ ls -l
Options
The chmod command has a number of command line options that affect its behavior. The most common options are:
- -R: Changes the modes of directories and files recursion|recursively
- -v: Verbose mode; lists all files as they are being processed
String modes
- See also: File system permissions#Symbolic notation|Symbolic notation of file system permissions
To the chmod utility, 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". They 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 | Special modes section |
t | sticky | 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.
String mode examples
For example, the following command would be used to add the read and write permissions to the user and group classes of a file or directory named sample:
$ chmod ug+rw sample $ ls -ld sample drw-rw---- 2 unixguy unixguy 96 Dec 8 12:53 sample
This command removes all permissions, and allows no one to read, write, or execute the file named sample.
$ chmod a-rwx sample $ ls -l sample ---------- 2 unixguy unixguy 96 Dec 8 12:53 sample
The following command changes the permissions for the user and the group to read and execute only (no write permission) on sample .
Sample file permissions before command $ ls -ld sample drw-rw---- 2 unixguy unixguy 96 Dec 8 12:53 sample $ chmod ug=rx sample $ ls -ld sample dr-xr-x--- 2 unixguy unixguy 96 Dec 8 12:53 sample
Octal numbers
The chmod command also accepts three- and four-digit octal numbers representing modes. See the article mentioned above for more. Using a four-digit octal number to set the modes of a file or directory named sample would look something like this:
chmod 0664 sample
Assuming that the setuid, setgid and sticky bits are not set, this is equivalent to:
chmod 664 sample
or
chmod +r,-x,ug+w 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 bit|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.
Examples
Read is added for all
chmod +r <file>
Execute permission is removed for all
chmod -x <file>
Read and write is set for the owner, all permissions are cleared for the group and others
chmod u=rw,go= <file>
Change the permissions of the file file to read and write for all.
chmod +rw <file>
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 -R u+w,go-w docs/
ADDS! read and write access for the owner, the group, and all others.
chmod 666 <file>
equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 specifies no special modes.
chmod 0755 <file>
The 4 specifies Setuid (set user ID).
chmod 4755 <file>
Sets a-x for all directories in tree starting from path/ (use '-type f' to match files only).
find path/ -type d -exec chmod a-x {} \;
Allows directory browsing ls for example if you've reset permissions for Samba write access.
find path/ -type d -exec chmod 777 {} \;
set a directory tree to rwx for owner directories, rw for owner files, --- for group and all.
chmod -R u+rwX,g-rwx,o-rwx <directory>