DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Protecting files and directories

Changing the permissions on a file

To change the permissions on a file, you use the command chmod. (chmod stands for ``change mode;'' a file's permissions are also known as its mode.) As with chown, and chgrp, only the owner of a file or the superuser (root) can change the permissions of a file.

To change the permissions on the file, type chmod, how you want to change the permissions, the name of the file, then press <Enter>.

To specify how you want to change permissions, you type a letter representing which set of permissions you want to change, a symbol that tells whether you want to add to, remove from, or overwrite the existing permissions, and a letter representing which permission you want to work with.

For example, to change the permissions on the file report so that members of the group techpubs can modify the file, you could type:

   $ l report
   -rw-r-----   1 susannah techpubs      25 Jun 27 11:58 report
   $ chmod g+w report
   $ l report
   -rw-rw----   1 susannah techpubs      25 Jun 27 11:58 report
The chmod command in the preceding example says ``group plus write''; in other words, add write permission to the existing permissions for group. If you wanted to remove the group write permission, you could type:
   $ l report
   -rw-rw----   1 susannah techpubs      25 Jun 27 11:58 report
   $ chmod g-w report
   $ l report
   -rw-r-----   1 susannah techpubs      25 Jun 27 11:58 report
If you wanted to remove all permissions for group, you could type:
   $ l report
   -rw-rw----   1 susannah techpubs      25 Jun 27 11:58 report
   $ chmod g= report
   $ l report
   -rw-------   1 susannah techpubs      25 Jun 27 11:58 report
The equals sign in the second example says ``overwrite all group permissions with nothing''; in other words, remove all group permissions.

You can think of how you specify permissions as an expression of the form:

chmod who [+|-|=] permission filename

Here, who tells which set of permissions you want to change; + , -, or = tells whether you want to add, remove, or overwrite; permission is the permission itself, and filename is the name of the file.

Here are all the options for who:

Option Meaning
a All users; change all three sets of permissions at once
u User; change the user, or owner, permissions
g Group; change the group permissions
o Others; change the other permissions
If you do not specify a who (for example, if you just said chmod +w), the write permissions are changed for all three sets.

Try creating a report file and then changing the permissions, the ownership, and the group it is in:

  1. If you are not already in you home directory, type cd and press <Enter>. (The reason you type cd and press <Enter> at the beginning of each exercise is to guarantee you are working in a directory where you have write permission.)

  2. Create a file called test using cat. Type cat > test and press <Enter>; then type This is a test file (or whatever text you like); then press <Enter> to go to a new line and press <Ctrl>D to close the file.
    $ cd
    $ cat > test
    This is a test file
    <Ctrl>D
    

  3. List the file by typing l test and pressing <Enter>.

  4. Change the permissions on the file so that everyone can modify the file by typing chmod +rw test and pressing <Enter>.

  5. Check what happened by typing l test and pressing <Enter>.

  6. Now, change the permissions back so only the owner of the file can change it, by typing chmod o-w test and pressing <Enter>, chmod g-w test and pressing <Enter>.

  7. List the file with l test and press <Enter>.

  8. Change the ownership of the file to root by typing chown root test and pressing <Enter>; then list the file with l test and press <Enter>.
    $ l test
    -rw-rw----   1 susannah techpubs      20 Jun 27 15:40 test
    $ chmod +rw test
    $ l test
    -rw-rw-rw-   1 susannah techpubs      20 Jun 27 15:40 test
    $ chmod o-w test
    $ chmod g-w test
    $ l test
    -rw-r--r--   1 susannah techpubs      20 Jun 27 15:40 test
    $ chown root test
    $ l test
    -rw-r--r--   1 root     techpubs      20 Jun 27 15:40 test
    

  9. Use the vi editor to open the file (vi test and <Enter>). You should see the words "test" [Read only] at the bottom of your screen. This is because you are no longer the owner of the file, so you only have read permission on it. (If you were to make changes to the file, when you tried to save it, you would see the error message File is read only; you would not be able to save your changes.) Type :q to quit vi.


Next topic: Summary
Previous topic: Changing the owner of a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003