Inadequate planning on your part does not necessarily constitute a crisis for us
Home » General Help » The chmod Command 

Printer Friendly Version  PDF Version 

The chmod Command

You may have noticed that when you look at a listing of the files in your UNIX account (using the command ls -la), several strange characters appear on the left side of the screen. For example:

% ls -la
total 424
drwx--x--x  24 bm901 student         1024 May 15 14:03 ./
dr-xr-xr-x   3 root  root               3 May 14 14:19 ../
-rw-------   1 bm901 student         2504 Apr 02 15:52 .Mailbox
-rw-------   1 bm901 student         2140 Feb 18 20:07 .Xauthority
-rw-------   1 bm901 student          785 May 07 12:22 .cshrc
drwx------   1 bm901 student          512 May 14 16:28 .mozilla/
drwx------   2 bm901 student          512 May 15 13:50 Mail/
drwxr-xr-x  13 bm901 student         1024 May 15 14:37 public_html/

These series of dashes and letters indicate what the file access permissions of each file are. Or, in other words, who can do what to these files.

Each file or directory is allotted 10 characters to indicate file access permissions. The first character indicates whether it is a file or a directory. If the first character is a "d", then it is a directory. A "-" indicates that it is a file.

The next 3 characters (2-4) indicate which permissions the owner of the file has. The owner is the user who created the file. Note that bm901 is the owner of the files in the example above. The next set of characters (5-7) indicates permissions for the group. The group is a set of users categorized together according to what permissions they have. In the example above, the user bm901 is in the group "student", The last set of characters (8-10) indicates permissions for all others, meaning everyone else in the world.

Access permissions are categorized mainly into 3 functions: Read, Write, and eXecute. An "r" indicates read permissions for a file or directory. A "w" indicates write permissions. An "x" indicates execution permissions if it is a file, and search permissions if it is a directory. The position of the letter in the 10-block string indicates whether the permissions are for the owner, the group, or others.

The following chart might help you to better visualize what all this means:

                          read permissions to user (owner)
                          |write permissions to user (owner)
                          ||execution permissions to user (owner)
                          |||   read permissions to all others
                          |||   |write permissions to all others
                          |||   ||execution permissions to all others
                          |||   |||
                         -rw-r--r--
                         |   |||
                         |   ||execution permissions to group
            file/directory   |write permissions to group
                             read permissions to group

For the file in the above example, the owner has read and write permissions, but no execution permissions. The group and all others have read only permissions. Anyone else on the system may read this file, but they may not write to it or execute it.

When you execute a chmod command, you have to specify four different fields:
2) whether you are adding or subtracting permissions
3) what permissions you are adding or subtracting
4) the name of the file or directory you are changing.

There are certain abbreviations that can be used to specify what you want to do when you want to change file access permissions. In the first field, the who is abbreviated with a u for user, g for group, or o for others.

In the second field, you may indicate whether you are adding or subtracting permissions with a simple + or a -.

In the third field, r means read, w means write, and x means execution.
Example:

% chmod g+r myFile

This command adds read permissions to the group for the file myFile.

You can also use multiple abbreviations within the chmod syntax to add or subtract multiple permissions within one single command line. For example:

% chmod ugo+rwx myFile

This command gives read, write, and execute permissions to the user (owner), the group, and all others, thus leaving the file access permissions looking like this:

% ls -l myFile
-rwxrwxrwx   1 bm901    student        0 Nov  4 10:42 myFile*

Using Octal Notation With chmod

Using the chmod command as explained above, (using ugo+/-rwx) you may set permissions relative only to what the permissions have been set to previously. You may also set permissions directly by using a variety of 3-digit octal numbers (ranging from 0 to 7). This will allow you to change permissions absolutely, meaning you specify what you want the permissions to be regardless of what they were before. This method can be somewhat easier for some people. Below is an example of the octal notation method:

                                    user
                                    | others
                                    | |
                              chmod 711 .plan
                                     |
                                     group

The first digit indicates what permissions are given to the user. The second digit is for the group, and the third digit is for all others.

This table indicates what the digits mean:


Octal Digit

Permissions

3-Character Display

7

read, write, execute
rwx

6

read, write
rw-

5

read, execute
r-x

4

read
r--

3

write, execute
-wx

2

write
-w-

1

execute
--x

0

(none)
---

So the command:

% chmod 644 myFile

sets the permissions for the file myFile as
-rw-r--r--
meaning that the user has permission to read and write and everyone else has read-only permission.


Home » General Help » The chmod Command
University of Greenwich, a charity and company limited by guarantee,
registered in England (reg no. 986729). Registered Office: Old Royal
Naval College, Park Row, Greenwich SE10 9LS.