3.4.2.4. Changing user and group ownership
When a file is owned by the wrong user or group, the error can
be repaired with the chown (change owner)
and chgrp (change group) commands. Changing
file ownership is a frequent system administrative task in
environments where files need to be shared in a group. Both
commands are very flexible, as you can find out by using the
--help option.
The chown command can be applied to
change both user and group ownership of a file, while chgrp only changes group ownership. Of course the
system will check if the user issuing one of these commands has
sufficient permissions on the file(s) she wants to change.
In order to only change the user ownership of a file, use this
syntax:
chown newuser file
If you use a colon after the user name (see the Info pages),
group ownership will be changed as well, to the primary group of
the user issuing the command. On a Linux system, each user has his
own group, so this form can be used to make files private:
jacky:~> id
uid=1304(jacky) gid=(1304) groups=1304(jacky),2034(pproject)
jacky:~> ls -l my_report
-rw-rw-r-- 1 jacky project 29387 Jan 15 09:34 my_report
jacky:~> chown jacky: my_report
jacky:~> chmod o-r my_report
jacky:~> ls -l my_report
-rw-rw---- 1 jacky jacky 29387 Jan 15 09:34 my_report
|
If jacky would like to share this file, without having
to give everybody permission to write it, he can use the chgrp command:
jacky:~> ls -l report-20020115.xls
-rw-rw---- 1 jacky jacky 45635 Jan 15 09:35 report-20020115.xls
jacky:~> chgrp project report-20020115.xls
jacky:~> chmod o= report-20020115.xls
jacky:~> ls -l report-20020115.xls
-rw-rw---- 1 jacky project 45635 Jan 15 09:35 report-20020115.xls
|
This way, users in the group project will be able to
work on this file. Users not in this group have no business with it
at all.
Both chown and chgrp can be used to change ownership recursively,
using the -R option. In that case, all
underlying files and subdirectories of a given directory will
belong to the given user and/or group.
|
Restrictions |
|
On most systems, the use of the chown and
chgrp commands is restricted for
non-privileged users. If you are not the administrator of the
system, you can not change user nor group ownerships for security
reasons. If the usage of these commands would not be restricted,
malicious users could assign ownership of files to other users
and/or groups and change behavior of those users' environments and
even cause damage to other users' files.
|