Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Changing Owners and Groups

Two commands are available to change the owner and the group of files:

  chown
  chgrp


The chown command stands for “change owner” and is used to change the owner of a file.

The chgrp command stands for “change group” and is used to change the group of a file.

On some older systems, the chgrp command might not be available, and the chown command must be used instead. You will learn how to use both chown and chgrp to change the group of a file. For maximum portability, you should stick to using chown to change both the owner and the group of a file.

Changing Ownership

The chown command changes the ownership of a file. The basic syntax is as follows:

chown options user:group files

Here, options can be one or more of the options listed in the man page for chown. Because considerable variation exists in the available options, please consult the man page on your system for a complete list.

The value of user can be either the name of a user on the system or the user id (uid) of a user on the system. The value of group can be the name of a group on the system or the group ID (GID) of a group on the system. To just change the owner, you can omit the group value.

As an example

chown ranga: /home/httpd/html/users/ranga

changes the owner of the given directory to the user ranga.

Restrictions

The super user, root, has the unrestricted capability to change the ownership of a file, but some restrictions occur for normal users.

Normal users can change only the owner of files they own. This means that if you give another user ownership of a file, you will not be able to regain ownership of that file. Only the new owner of the file or the super user can return the ownership to you.

On some systems, the chown command will be disabled for normal user use. This generally happens if the system is running disk quotas. Under a disk quota system, users might be allowed to store only 100MB of files, but if they change the ownership of some files, their free available disk space increases, and they still have access to their files.

The chown command will recursively change the ownership of all files when the -R option is included. For example, the command

chown -R ranga: /home/httpd/html/users/ranga

changes the owner of all the files and subdirectories located under the given directory to be the user ranga.

Changing Group Ownership

You can change group ownership of a file with the chgrp command. Its basic syntax is as follows:

chgrp options group files

Here, options is one or more of the options listed in the man page for chgrp. The value of group can be either the name of a group or the GID of a group on the system. As an example

chgrp authors /home/ranga/docs/ch5.doc

changes the group of the given file to be the group authors. Just like chown, all versions of chgrp understand the -R option also.

On systems without this command, you can use chown to change the group of a file. For example, the command

chown :authors /home/ranga/docs/ch5.doc

changes the group of the given file to the group authors.

Summary

In this chapter, I covered several important topics relating to files and file permissions. Specifically, I covered the following tasks:

  Determining a file’s type
  Changing file and directory permissions using symbolic and octal notation
  Enabling SUID and SGID permissions for files and directories
  Changing the owner of a file or directory
  Changing the group of a file or directory

As you will see in subsequent chapters, each of these tasks is important in shell scripts.

Questions

For the three questions, refer to the following ls -l output:

crw-r-----   1 bin     sys      188 0x001000 Oct 13 00:31 /dev/
⇒rdsk/c0t1d0
-r--r--r--   1 root    sys          418 Oct 13 16:25 /etc/passwd
drwxrwxrwx  10 bin     bin         1024 Oct 15 20:27 /usr/local/
-r-sr-xr-x   1 root    bin        28672 Nov  6  1997 /usr/sbin/ping
1.  Identify the file type of each of the files given above.
2.  Identify the owner and group of each of the files given above.
3.  Describe the permissions for the owner, group, and all “other” users for each of the files given above.


Previous Table of Contents Next