7.5 Changing the Owner and Group of a File

   

7.5 Changing the Owner and Group of a File

When a user creates a new file, it belongs to the user who created it and to the current group of the user. Any user other than the owner of the file can't change ownership of a file, except the superuser. A user can change group membership of a file only if he or she is member of both the old and new groups. In normal circumstances, the system administrator needs to change file and group ownership when copying files to new user directories or when making files available to new users or groups.

In HP-UX, you use chown (CHange OWNer) for changing the owner of a file and chgrp (CHange GRouP) for changing group membership of a file. Let us see what happens when we change the owner of a file from user boota to jim .

 $  ll file1  -rwxrwxrwx   1 boota   users     0 Sep  8 18:06 file1 $  chown jim file1  $  ll file1  -rwxrwxrwx   1 jim     users     0 Sep  8 18:06 file1 $ 

Note that the user name is changed in the third field. We don't need to mention the old owner name when changing ownership of a file with the chown command.

A similar method is used for changing the group ownership of file1 from users to root with the chgrp command.

 $  ll file1  -rwxrwxrwx   1 jim     users     0 Sep  8 18:06 file1 $  chgrp root file1  $  ll file1  -rwxrwxrwx   1 jim     root      0 Sep  8 18:06 file1 $ 

Multiple file names and wildcard characters can also be used with chown and chgrp . Names of a new owner and group must be present in the /etc/passwd and /etc/group files, respectively. The chown command can also be used to change the owner and group in one step. See the following example, where the owner and group are used together with a colon symbol.

 $  ll file1  -rwxrwxrwx   1 boota   users     0 Sep  8 18:06 file1 $  chown jim:root file1  $  ll file1  -rwxrwxrwx   1 jim     root      0 Sep  8 18:06 file1 $ 

To change the ownership or group of a directory tree, you can use the -R option with both of these commands and with the directory name as argument. Whenever you change ownership of a link file, ownership of the file to which the link points is also changed. To avoid this, use the -h option with the chown or chgrp commands.

Study Break

Dealing with File Permissions

Up until this point you have studied basic concepts of file security structure used in UNIX. You have seen what the file permissions are and how can these be set and modified. Let's take a short break and use some of these concepts.

First, use the command umask 000 to ensure that the default file permissions are being used. Then, create a new file in your home directory with the name testfile . List the file using the ll testfile command and see what the default permissions are. Check the mask value using the umask command. Now change the value of umask to 222 and create another file with the name myfile . Did you find any difference between the file permissions of testfile and myfile ? Now try to change the contents of myfile using the vi editor. When you try to save the file, vi tells you that the file is read-only. This is because you had changed the mask value such that the owner didn't have write permission. Quit the vi editor without saving the file and use the chmod command to grant write access to the owner of the file.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net