Knowledge Base Navigation

Articles: 51 Categories: 8

KB Article: File Permissions and the CHMOD Command

Article:File Permissions and the CHMOD Command     Popular
Submitted By:Dauthus
Date Added:05-09-2005 7:20:42
Hits:9,575



chmod (abbreviated from change mode) is a shell command in Unix-like environments

From Wikipedia, the free encyclopedia.

The title given to this article is incorrect due to technical limitations. The correct title is chmod.

chmod (abbreviated from change mode) is a shell command in Unix-like environments.

When executed, the command can change file system modes of files and directories. The modes include permissions and special modes.


History

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


Usage

The chmod command options are specified like this:

chmod switches modes files

Switches

chmod has a number of command line options, or "switches", that can modify the output. Some of these options are

* -R: recursively modify modes of all files in subdirectories.
* -h: if the file specified is a soft link, change the mode of the link itself rather than the file that the link points to.
* -v: verbose. List all files as they are being processed



Modes

There are permissions and special modes. The permissions are:

r - read permission;
w - write permission;
x - execute permission;

For each file is specified which permissions apply to the owner of the file, users in the group of the file, or all other users.

The modes can be specified in two ways, with characters or octal numbers. For characters, there are modification operators such that + adds the mode, = sets the mode, and - removes the mode setting.


Special Modes

There are three so called special modes as well: the sticky bit, set user ID, and set group ID.

* 1000: If the sticky bit, set by adding octal number 1000 to the permissions, is applied to a directory, then only the file owner, the directory owner, or superuser can delete a file in that directory. For example, if a directory has permissions 0770, then the directory owner or anyone in the directory's group can add files or delete files any files (regardless of who the file's owner is). If the sticky bit is set, so the permissions are 1770, then anyone in the group can add files to the directory, but each user can only delete her own files.

* 4000: This permission sets the set user ID bit. When a directory has this permission, files created within the directory have the user ID of the directory, rather than that of the user who created the file. Note that some operating systems don't allow you to use this numerical value, instead forcing you to use the symbolic alternative.

* 2000: This octal permission code sets the set group ID bit. W a directory has this permission, files created within the directory have the group ID of the directory, rather than that of the default group setting for the user who created the file. Note that some operating systems don't allow you to use this numerical value, instead forcing you to use the symbolic alternative.


Examples

+r
Read is added for all;
u=rw,go=
Read and write is set for the owner, all permissions are cleared for the group and others;
-x
execute permission is removed for all;

In octal mode, four numbers are specified and used to set the mode of a file. For owner, group and other the value of each setting is added and the modes are concatenated. The modes are given the values r 4; w 2; x 1.

Specified as:
0755 - Equivalent to u=rwx,go=rx. The 0 specifies no special modes.


Sample use

Example:

chmod +rw file.txt

The chmod command will change the permissions of the file file.txt to read and write for all.

chmod -R u+w,go-w docs/

The chmod command will 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.



The most useful trick I have found is how to change the permissions of an entire website using two simple commands. Let's say you just installed a program with several hundred files of different types and folders. Now you want to change all the directories to 0755 and all the files (no matter what the extension) to 0644. SSH into your box and change to the proper directory. In this example html is the directory. Use the following command:

cd /var/www/html


Next you want to change the permissions of every folder and file at the same time. Type this command to change all the folders to the permission of 0755 and hit enter:

find . -type d -exec chmod 755 {} \;


Next you change all the files to 0644 by typing this and hitting enter:

find . -type f -exec chmod 644 {} \;


Now if you review all your files and folders, you will see they have all been changed to what you wanted them to be.


This article is licensed under the [ Register or login to view links on this board.] . It uses material from the [ Register or login to view links on this board.]

Current rating: 6.68 by 91 users
Please take one second and rate this article...

Not a Chance 12345678910 Absolutely

Please register or sign-in to post comments.


Jump to a selected article...