Files have two kinds of ownersthe user owner and the group owner. Only root can change a file's user ownership. You cannot "give away" a file.
In order to change a file's group ownership, you must be both the file's user owner and a member of the new group, or you must perform the change as root. Furthermore, when a file's group ownership is changed, the setuid and setgid properties are removed unless the change was made by root. This avoids some potential security problems (see the sidebar "What the setuid and setgid Properties Do" for more on this).
Group ownership can be changed using either the chgrp ( change group ) or chown ( change owner ) command.
To change the group ownership using chgrp:
localhost:~/Sites vanilla$ groups vanilla appserveradm appserverusr admin www localhost:~/Sites vanilla$ ls -l index.html -rw-r--r-- 1 vanilla vanilla 5754 Jan 17 14:18 index.html localhost:~/Sites vanilla$ chgrp www index.html localhost:~/Sites vanilla$ ls -l index.html -rw-r--r-- 1 vanilla www 6186 Apr 30 2001 index.html localhost:~/Sites vanilla$
O PTION | M EANING |
---|---|
-R | Makes changes recursively. Used when changing permissions on a directory and everything it contains. The next three options only work in combination with the -R option. Only one of the following may be used. If more than one is used, the last one on the command line takes precedence. |
-H | If the -R option is specified, symbolic links on the command line are followed. (Symbolic links encountered in the directory traversal are not followed.) See Chapter 5 for more on symbolic links. |
-L | If the -R option is specified, all symbolic links are followed. |
-P | If the -R option is specified, no symbolic links are followed. |
-f | Ignore errors (force silence). This option squelches error messages resulting from inadequate permissionsfor example, if you try to change the group ownership to a group to which you do not belong. |
Tip
It is often useful to use the -R option to change the group ownership of an entire directory. For example,
chgrp -R www webteam
changes the directory webteam and all it contains so that it is owned by group www.
To change the group ownership using chown:
As we've mentioned above, a file's user ownership can be changed only by the root user. On Mac OS X the standard way to perform a command as root is to use the sudo command. The following task shows how to change a file's user ownership using sudo and chown . (See Chapter 11 for more on sudo .)
To change the user ownership using chown:
1. | sudo chown newuser file For example, sudo chown whitney index.html changes the user ownership of the file index.html to whitney. The sudo command requires that you enter your password if you haven't used it within the last five minutes.
| |
2. | Enter your password if asked. The command is executed after you enter your password and press . Figure 8.21 shows before and after views of using this command line. Figure 8.21. Using chown to change a file's user ownership.localhost:~/Sites ls -l index.html -rw-r--r-- 1 vanilla vanilla 5754 Jan 17 14:18 index.html localhost:~/Sites sudo chown whitney index.html Password: localhost:~/Sites ls -l index.html -rw-r--r-- 1 whitney vanilla 5754 Jan 17 14:18 index.html localhost:~/Sites |
To change the user and group ownership simultaneously :
1. | sudo chown newuser : newgroup file For example, sudo chown vanilla:www index.html |
2. | Enter your password if asked. The command is executed when you enter your password and press . Figure 8.22 shows the before and after of this command line. Figure 8.22. Using chown to simultaneously change the user and group ownership.localhost:~/Sites ls -l index.html -rw-r--r-- 1 whitney vanilla 5754 Jan 17 14:18 index.html localhost:~/Sites sudo chown vanilla:www index.html Password: localhost:~/Sites ls -l index.html -rw-r--r-- 1 vanilla www 5754 Jan 17 14:18 index.html localhost:~/Sites |
Tip
The chown command uses the same options as the chgrp command. It is often useful to change an entire directory full of files at once:
sudo chown -R howard:www web_images
changes the directory web_images and all it contains to be owned by the user howard and the group www.