previous contents up next

Unix for Advanced Users

6. Manipulating Files

6.4. File Ownership and Permissions

Changing File Ownership and Permissions

For security reasons, most Unix flavors only allow the super user can change ownership of a file. The owner of a file can change the group ownership of the file, but only if they belong to the group they are changing ownership to. The superuser is allowed to change the ownership and/or permissions bits of any file.

Ownership or group membership can be changed using the chown or chgrp command. The format is

A file's owner can change any or all of the permissions with the chmod (change mode) command. The chmod command allows you to dictate the type of access permission that you want each file to have. In the previous example the current permissions for myfile are read for everybody, write for the owner, and execute by no one.

The arguments supplied to chmod are a symbolic specification of the changes required, followed by one or more filenames. The specification consists of whose permissions are to be changed: u for user (owner), g for group, o for others, or some combination thereof (a (all) has the same effect as ugo), how they are to be changed (+ adds a permission, - removes a permission, and = sets the specified permissions, removing the other ones) and which permission to add or remove (r for read, w for write, and x for execute). For example, to remove all the permissions from myfile:


To allow read and write permissions for all users:


To remove write permission for your groups and other users:


Finally, to allow only read permission to all users:


Now the file is protected by allowing only read access; it cannot be written to or executed by anyone, including you. Protecting a file against writing by its owner is a safeguard against accidental overwriting, although not against accidental deletion.

Symbolic to Octal Conversion

chmod will also accept a permission setting expressed as a 3-digit octal number. To determine this octal number, you first write a 1 if the permission is to be set and a 0 otherwise. This produces a binary number which can be converted into octal by grouping the digits in threes and replacing each group by the corresponding octal digit according to the table below.

TABLE 2. Symbolic to Octal Conversions

SYMBOLBINARYOCTAL
---0000
--x0011
-w-0102
-wx0113
r--1004
r-x1015
rw-1106
rwx1117

Thus, if the setting you want is rw-r--r--, determine the octal number with the following method:

This shows that the octal equivalent of rw-r--r-- is 644. The following example illustrates that the permissions for myfile have been reset to the values with which we began.


To change the permissions back to read only, you can execute chmod as follows:


Directories

As with files, directories may also have permissions assigned. When listing directories, you may use the -d option to keep from descending into the directories you list. Otherwise, the contents of the directories will be displayed as well as their names. Below is an example of permissions assigned to a directory:


The directory home and the files and directories under it may be read and executed by anyone, but written to only by the owner and users in the masc223 group. Assuming you are the owner of this directory, you may decide to change the permission to allow only yourself and the masc223 group to read and execute files in the home directory. You would set the permissions accordingly:


You may decide that only you should be able to alter the contents of the directory. You must remove the write permission for the group.


previous contents up next