These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Setting Ownership And Permissions

Classnotes | UNIX01 | RecentChanges | Preferences

Commands for setting file ownerships and permissions.

chown

chown changes the ownership of a file or files. It's usage is as follows:
   chown [OPTION]... OWNER[:[GROUP]] FILE...
   chown [OPTION]... :GROUP FILE...
   chown [OPTION]... --reference=RFILE FILE...

Where OWNER is the username of the owner of the file, GROUP is the group-name of the group owner, FILE is the file, files, or directory to be changed, and OPTION is any of the following:

  • -c, --changes
like verbose but report only when a change is made
  • --dereference
affect the referent of each symbolic link, rather than the symbolic link itself
  • -h, --no-dereference
affect symbolic links instead of any referenced file (available only on systems that can change the ownership of a symlink)
  • --from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute.
  • -f, --silent, --quiet
suppress most error messages
  • --reference=RFILE
use RFILE's owner and group rather than the specified OWNER:GROUP values
  • -R, --recursive
operate on files and directories recursively
  • -v, --verbose
output a diagnostic for every file processed
  • --help
display this list of options and exit
  • --version
output version information and exit

If I was changing a file "foobar" to be owned by the user "frank" and the group "business" then the command would be:

 # chown frank:business foobar

If I wanted to change the directory "somedir" to be owned by user "clarke" and the group "mainoffice", and I wanted all files underneath somedir to gain this ownership as well, I would issue:

 # chown -R clarke:mainoffice somedir/

Note that on some UNIXes, it is equally valid to use a period (".") character in place of the colon separator (":"). For example, the following is generally equivalent to the previos command

 # chown -R clarke.mainoffice somedir/

chgrp

chgrp changes the group ownership of a given file or directory. Because it's functionality is duplicated in chown, chgrp is not used as often anymore. chgrp usage is very similar to chown:
   chgrp [OPTION]... GROUP FILE...
   chgrp [OPTION]... --reference=RFILE FILE...

and it accepts the same options as chown. Thus, we will not go into much depth with them.

If I wanted to change the group owner of the file "airplane.txt" to group "users", I would issue the following command

 # chgrp users airplane.txt

chmod

chmod changes the permissions of a file. chmod's general usages is as follows:
   chmod [OPTION]... MODE[,MODE]... FILE...
   chmod [OPTION]... OCTAL-MODE FILE...
   chmod [OPTION]... --reference=RFILE FILE...

Again, the OPTIONS are the same as chown and chgrp, so we will not delve into them. Here, the thing we must learn is what all these "MODE"s mean.

There are two basic ways to assign permissions to a file or directory, using OCTALS or SYMBOLS. OCTALS are generall considered more difficult, so we will examine them first to get them out of the way.

OCTAL-MODE

OCTAL is a numeric mode with one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Any omitted digits are assumed to be leading zeros. The first digit selects the set user ID (4) and set group ID (2) and sticky (1) attributes. The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.

For example, if I wanted to obtain permissions of "-rw-r--r-x", the octal code would be:

   rw- = 4+2 = 6
   r-- = 4
   r-x = 4+1 = 5
       = 645
 $ chmod 645 some_file

As another example, if I wanted to obtain permissions of "-rwxrwx---", the octal code would be

   rwx = 4+2+1 = 7
   rwx = 4+2+1 =7
   --- = 0
       = 770
 $ chmod 770 some_file

SYMBOL MODE

The format of a symbolic mode is `[ugoa...][[+-=][rwxXstugo...]...][,...]'. Multiple symbolic operations can be given, separated by commas.

A combination of the letters `ugoa' controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if `a' were given, but bits that are set in the umask are not affected.

The operator `+' causes the permissions selected to be added to the existing permissions of each file; `-' causes them to be removed; and `=' causes them to be the only permissions that the file has.

The letters `rwxXstugo' select the new permissions for the affected users: read (r), write (w), execute (or access for directories) (x), execute only if the file is a directory or already has execute permission for some user (X), set user or group ID on execution (s), sticky (t), the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file's group (g), and the permissions granted to users that are in neither of the two preceding categories (o).

In other words, if I wanted a permissions of OCTAL 645 (-rw-r--r-x), I would use:

 $ chmod u=rw,g=r,o=rx some_file

More often than not, you will simply be using the symbolic modes to tweak an existing permission setting. For example, if we had the following file:

 -rw-r--r-x    1 sam      sam       1264 Jul 11 20:42 readme

and we wanted to remove all world access, we could execute:

 $ chmod o-rwx readme 
 -rw-r-----    1 sam      sam       1264 Jul 11 20:42 readme


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 18, 2003 9:56 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.