Previous | Table of Contents | Next |
Two commands are available to change the owner and the group of files:
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.
In this chapter, I covered several important topics relating to files and file permissions. Specifically, I covered the following tasks:
As you will see in subsequent chapters, each of these tasks is important in shell scripts.
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
Previous | Table of Contents | Next |