Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

Changing Ownership

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:

Tip

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.

What the setuid and setgid Properties Do

If an executable file such as a script has the setuid property turned on, and if the file is executable by users other than the owning user (group or other executable), then when the program is run by someone other than the owning user, the process has the same permission as the owning user. This means you can create a script that alters files on which only you normally have write permission, and you can allow other people to run that script. Obviously this can be both useful and dangerous, like giving out the keys to your house. A similar situation occurs with setgid permission: The process runs with the permission of the owning group.

When a user (other than root) changes the group that a file belongs to and setuid and setgid properties are removed, the properties must be reset as a security precaution. This helps avoid accidentally giving too much power to other users. If you truly want the file to have setuid or setgid permission, after changing its group you must reset the setuid and/or setgid permission.

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

Категории