Ownership and groups of files and directories are governed by the following rules:
- When a file is created, the user who created it owns it.
- When a file is created, it belongs to the group of the user who created it.
The command
chown
is used to change a file's owner.
The command
chgrp
is used to change a file's group. These commands have the following syntax:
chown new_owner filename
chgrp new_group filename
In general, only the superuser may change file ownership. If a user owns a file, he or she may change its group to another group if he or she belongs to the new group. In practice, these commands are almost always used by the superuser. Both
chgrp
and
chown
support the
–R
option, which allows changing ownership of an entire directory tree. For example, to change everything in directory1 and its subdirectories to have owner
owner2
, use
chown –R owner2 directory1
Unix file ownership is a bit more complex than it is under some other operating systems. The basic concept is that each file has an owner.
Typically, the user who created it and has control over it. On Unix systems, files have two owners:
- a user owner and
- a group owner.
What is unusual about Unix file ownership is that these two owners are decoupled. A file's group ownership is independent of the user who owns it. In other words, although a file’s group owner is often, perhaps even usually, the same as the group its user owner belongs to, this is not required. In fact, the user owner of a file does need not even need to be a member of the group that owns it. There is no necessary connection between them at all. In such a case, when file access is specified for a file’s group owner, it applies to members of that group and not to other members of its user owner’s group, who are treated simply as part of "other": the rest of the world.
The motivation behind this group ownership of files is to allow file protections and permissions to be organized according to your needs.
The key point here is flexibility, since Unix lets users be in more than one group, you are free to create groups as you need them.
Files can be made accessible to almost completely arbitrary collections of the system's users. Group file ownership means that giving someone access to an entire set of files and commands is as simple as adding her to the group that owns them; similarly, taking access away from someone else involves removing her from the relevant group.
The following section discusses how to change a file’s ownership and group.