The Chmod Command

   

Introduction

The chmod utility is used to modify access permissions on files and directories on Unix / Linux systems. Using chmod, a user can set the Read, Write, and Execute permissions on any file or directory for that file's Owner, Group, and Others.

Format: There are multiple forms of the chmod command:
chmod <octal_permissions> <file/directory_name>
chmod <category><operation><permission> <file/directory_name>
NOTE! For many of the examples in this guide, we will change the permissions on a file and then display the file attributes. In order to have these same file attributes display on your own system, you will need to use the command ' ls -al ' in the same directory as the modified file.

Example

General usage (octal):
#chmod 644 process.txt 

-rw-r--r--    1 Aaron  Aaron  1281 Jul 20 23:00 process.txt
We use an octal code to represent the permissions we want. In this case 6 = rw and 4 = r. Owner can now read/write, group and others can only read process.txt

Example

General usage (Descriptive Notation): Assuming we start with: -rw-r--r-- 1 Aaron Aaron 1281 Jul 20 23:00 process.txt
#chmod ug+x process.txt 

-rwxr-xr--    1 Aaron  Aaron  1281 Jul 20 23:00 process.txt
In this case we are essentially telling chmod that for the user and group (ug) we want to add (+) the execute permission (x) to the process.txt file.

Common Uses

Set all files in a directory to a particular permission

Often a user will need to change the permissions on all of the files in a directory and its subdirectories.
chmod -R 744 

Change the permissions on the current directory.

When a user is in a directory that they want to modify permissions on, and they don't want to type in the whole directory path (who can blame them).
Octal:
chmod 644 .

drw-r--r--   2 Aaron  Aaron   68 Jul 25 23:10 .
Descriptive Notation:
chmod u=rw,g=r,o=r .

drw-r--r--   2 Aaron  Aaron   68 Jul 25 23:10 .
Both yield the same result, but if you can master the octal notation (through practice and understanding of ... OCTAL) you can save some time.
Take note! You wouldn't be able to immediately use the ls -al command because you need to be able to 'execute' the directory to do that.
You could, however, cd .. and then ls -al

Configuring Permissions for a WWW Server

One of the most underestimated and misunderstood responsibilities of an online publisher, whether you're a student or full-blown webmaster, is how to properly setup access permissions on a web directory. Often a publisher will create a web directory with files that are over-secure (not usable) or insecure.
This guide will provide a very standard set of permissions with which most web servers would properly function.

Web Directory Permissions

For the root web directory. i.e. The directory that is indexed when users visit http://www.yoursite.com.
Octal:
chmod 755 /www

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:35 www
Descriptive
chmod u=rwx,g=rx,o=rx /www

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:46 www
At the very least, this directory needs to be world executable. 755 Allows the user that owns the directory full access, and everyone else read/execute access.

Permissions for HTML files

Set permissions for all of the static documents in your root web directory.
Octal:
chmod -R 755 /www/           (assuming your www directory is at the root of your computer)

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:35 www
Descriptive
chmod -R u=rwx,g=rx,o=rx /www/

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:46 www
The -R option allows chmod to recursively traverse the www directory and apply the permission to all of the files within it AND its subdirectories.

Permissions for executable web applications (CGI / Scripts)

Set the permissions on executable web documents in order to allow users to properly utilize them.
Octal:
chmod 755 /www/cgi-bin/*.cgi

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:35 www
Descriptive
chmod u=rwx,g=rx,o=rx /www/cgi-bin/*.cgi

drwxr-xr-x  179 webuser webgroup     28160 Jul 27 13:46 www
This is assuming you're using .cgi files (slowly becoming an obsolete form of web application) and that they are located in /www/cgi-bin

Another example can be found in our http://www.dollarsintopounds.co.uk calculator.

CHMOD Calculator
Permission Owner Group Other
Read (4)
Write (2)
Execute (1)
Value