7.1 File Owner and Belonging Group


The file creator's user ID and group ID become its file owner and its belonging group. The file owner and belonging group are important attributes for the files.

The owner can be changed by the chown command and the belonging group can be changed by the chgrp command.

7.1.1 Change Owner

We can change file owner by chown command.

Format

chown user[.group] directory
chown user[.group] file


The root user can change the file owner and group. We can operate both files and directories similarly. We can use either ‘.’ or ‘:’ as user-group separator.

Options


-R
This option is for directories. The directories and files in a specified directory are changed recursively.


Practice: Changing file owner


# ls -l /usr|grep bin ↵      Display current owner and group
drwxr-xr-x 2 root root 69632 Mar 27 20:07 bin root user/root group
drwxr-xr-x 2 root root 16384 Mar 27 20:07 sbin root user/root group
# chown mail /usr/bin ↵      Change owner to mail user
# chown mail.news /usr/sbin ↵      Change owner and group to mail user/news
group # ls -l /usr|grep bin ↵      Display current owner and group again
drwxr-xr-x 2 mail root 69632 Mar 27 20:07 bin mail user/root group
drwxr-xr-x 2 mail news 16384 Mar 27 20:07 sbin mail user/newsgroup
# chown root /usr/bin ↵      Restore the original value
# chown root:root /usr/sbin ↵      Restore the original value


Caution: Although the above change causes no problem, please restore original value(root user, root group).


Practice: Change file owner by both root user and general user


Please create file as general user, and try to change its owner. Next you become root user and try it again. Please check whether its owner changed.


$ touch user ↵       Create a file
$ ls -l user ↵       Display file owner and group
-rw-r--r-- 1 penguin lpic 0 Feb 9 12:39 user Current owner is penguin and group is lpic
$ chown nobody user ↵       Change owner as general user
chown: changing ownership of ‘users' : Operation not permitted General user can’t change ownership
$ su ↵             Become root user
password: xxxxxxxx↵       Enter root user password
# chown nobody user ↵       Try change owner again
# ls -l user ↵       Check the owner
-rw-r--r-- 1 nobody lpic 0 Feb 9 12:39 user


The user(penguin) and group(lpic) in the 3rd line turn into your user and group which have inputted the present command.

7.1.2 Change Belonging Group

The belonging group of a file can be changed by the chgrp command.


Format

chgrp group directory
chgrp group file


This command allows us to change files/directories group. We can operate both files and directories similarly.

Options

-R
This option is for directories. The directories and files in a specified directory are changed recursively.



Practice: Changing belonging group


# ls -l /bin/ls ↵      Display current owner and group
-rwxr-xr-x 1 root root 93560 Mar 22 2007 /bin/ls
# chgrp mail /bin/ls ↵      Change belonging group to mail
# ls -l /bin/ls ↵      Display current owner and group
-rwxr-xr-x 1 root mail 93560 Mar 22 2007 /bin/ls
# chgrp root /bin/ls ↵      Restore the original value


Caution: Although the above change causes no problem, please restore original value(root group).


Practice: Change group by both root user and general user


Please create file as general user, and try to change its group.
Next you become root user and try it again.
Please check whether its group changed.


$ touch groups ↵          Create file
$ ls -l groups ↵          Display file owner and group
-rw-r--r-- 1 penguin lpic 0 Feb 9 12:39 groups Current belonging group
$ chgrp nobody groups ↵      Change group as general user
chgrp: changing group of `groups' : Operation not permitted     General user can’t change ownership
$ su ↵           Become root user
Password : xxxxxxxx↵      Enter root user password
# chgrp nobody groups ↵      Try change group again
# ls -l groups ↵               Check the group
-rw-r--r-- 1 penguin nobody 0 Feb 9 12:39 groups



Previous Next