UNSW   Faculty of Engineering PRINT VERSION SITE MAP  
cse | School of Computer Science and Engineering (CRICOS Provider No. 00098G)
    #About CSE     #Undergraduate Study     #Postgraduate Study     #Timetables & Courses     #Research & Publications     #People & Work Units     #Help & Resources     #News & Events     #High School Portal

Last updated 19.02.08


Changing Permissions

The chmod command is used to change the access permissions on a file. Only the owner of a file can change its access permissions (with the exception of the super user). The general format of the command is:

    chmod mode filename

where mode has the format:

    [who] op permission
            or
    [0-7][0-7][0-7]


\begin{commandlist} \item[who] This is a combination of letters {\bf u} for {\i... ...o be indicated using a number to indicate the bits to be set. \end{commandlist}

Permissions may also be specified using the summation of octal (base 8) numbers:

Permissions Octal
Read permission 4
Write permission 2
Execute permission 1
Permission denied 0

These numbers are added to indicate the permission for each group. Some examples:

Permissions Granted Number To Use
read, write, execute 7
read and write 6
read only 4

Some examples of manipulating permissions follow:

A listing that shows the current permissions.

    % ls -al
    total 24
    drwx------   2 cathy        4096 Jan 21 17:00 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy        2989 Jan 21 16:58 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    -rw-------   1 cathy           0 Jan 21 17:00 typescript

Change the permissions on the file junk.txt to grant read and write permissions to group and other users. It is usually a bad idea to let other users write to your files or directories.

    % chmod 666 junk.txt 
    % ls -al
    total 24
    drwx------   2 cathy        4096 Jan 21 17:00 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-rw-rw-   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy        2989 Jan 21 16:58 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    -rw-------   1 cathy           0 Jan 21 17:00 typescript

Remove the others permissions from the file junk.txt.

    % chmod o-rw junk.txt
    % ls -al
    total 24
    drwx------   2 cathy        4096 Jan 21 17:00 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-rw----   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy        2989 Jan 21 16:58 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    -rw-------   1 cathy           0 Jan 21 17:00 typescript

Now remove the group permissions from the file junk.txt.

    % chmod g-rw junk.txt
    % ls -al
    total 24
    drwx------   2 cathy        4096 Jan 21 17:00 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-------   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy        2989 Jan 21 16:58 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    -rw-------   1 cathy           0 Jan 21 17:00 typescript

Grant group read and write permission to the file myfile.txt.

    % chmod 660 myfile.txt
    % ls -al
    total 24
    drwx------   2 cathy        4096 Jan 21 17:00 .
    drwx------  32 cathy        4096 Jan 21 16:39 ..
    -rw-------   1 cathy         120 Jan 21 16:43 junk.txt
    -rw-rw----   1 cathy         176 Jan 21 16:45 myfile.txt
    -rw-------   1 cathy        2989 Jan 21 16:58 session1.txt
    -rw-------   1 cathy          75 Jan 21 16:40 stuff.txt
    -rw-------   1 cathy           0 Jan 21 17:00 typescript

It is a matter of personal preference as to which of these two formats you decide to use. Whichever format you choose to use, make sure that you never make your home directory unreadable by you! This will result in you not being able to login again! If you do happen to have a problem, go to the Help Desk to have the problem resolved.

Loc Van Huynh 2007-03-15
Top Of Page

 ###
Site maintained by webmistress@cse.unsw.edu.au
Please read the UNSW Copyright & Disclaimer Statement