Chown Command in Linux (Change File Ownership)

In Linux, all files, directories and processes (which are again files) are owned by users. The group is a set of users that share the same access permissions (i.e read, write and execute) for that objects (files/directories).

The chown command is used in Linux to change the user and group ownership of files, directories and symbolic links.

In this tutorial, I will show you how to use the chown command with some practical examples.

Chown syntax

Let check the basic syntax of chown command as follows:

chown [OPTIONS] USER[:GROUP] FILE/DIRECTORY

You can run ls -l command to print file ownership details.

In the following output, you can see the file 'myfile.txt' is owned by user 'tom' and the group is owned by 'developers':

$ ls -l
-rw-r--r-- 1 tom  developers   0 Apr  4 01:35 myfile.txt

1) Change Ownership of a file

To change the ownership of a file, use chown with new owner name and the file name for which the owner has to be changed.

The following command change ownership of the file named 'myfile.txt' to a new user 'tom':

# chown tom myfile.txt

If the command successfully executed it won't display any output on the terminal. Let verify using ls -l command as follows:

# ls -l myfile.txt
-rw-r--r-- 1 tom root 623 Dec 10 2012 myfile.txt

2) Changing the owner and group of a file

If the owner is followed by a colon (:) and a group name (without spaces), the group name is changed as well.

The following example shows how to change owner and group for the file named 'myfile.txt':

# chown tom:developers myfile.txt

Now the new owner of the file is 'tom' and the new group owner is 'developers' group.

# ls -l myfile.txt
-rw-r--r-- 1 tom developers 424 Dec 10 2012 myfile.txt

If you remove the group name after the colon the group of the file is changed to the specified user’s login group:

# chown tom: myfile.txt
# ls -l myfile.txt
-rw-r--r-- 1 tom tom 453 Dec 10 2012 myfile.txt

If the colon (:) and group are mentioned, only the group of the file is changed. In this case, the command works like chgrp command.

# chown :developers myfile.txt
# ls -l myfile.txt
-rw-r--r-- 1 tom developers 1579 Dec 10 2012 myfile.txt

3) Change ownership for directories recursively

To apply owernship recursively on all files and directories under a given directory use -R option

In the following example, the ownership all the files and directories under '/var/www/html' will be recursively changed to 'linoxide' and group ownership to 'www-data':

# chown linoxide:www-data -R /var/www/html

Verbose output

The --verbose option shows all the ownership changing on the terminal. It outputs the diagnostics for each file processed.

For example:

# chown -R --verbose jones /home/jones/
changed ownership of `/home/jones/hello' to jones
changed ownership of `/home/jones/.emacs' to jones
changed ownership of `/home/jones/.bash_history' to jones
changed ownership of `/home/jones/.bash_logout' to jones
changed ownership of `/home/jones/.bashrc' to jones
changed ownership of `/home/jones/file1' to jones

The verbose option outputs the processing of each file even when the changes are not made. But with -c or --changes option, the output is reported only when changes are made.

5) Silent operation

A normal users cannot change the ownership of files owned by others. So an error is displayed when a normal user tries to change the ownership.

[tom@redhat-server ~]$ chown tom /etc/
chown: changing ownership of `/etc/': Operation not permitted

But if we use -f or --silent or --quiet option, the error is not displayed.

[tom@redhat-server ~]$ chown -f tom /etc/
[tom@redhat-server ~]$

6) Preserve root

To prevent changing the ownership of / directory recursively, --preserve-root is used.

[root@redhat-server ~]# chown -R --preserve-root raghu /
chown: changing ownership of `/proc/1/task/1/fd/10': Permission denied
chown: changing ownership of `/proc/1/task/1/fd': Permission denied
chown: changing ownership of `/proc/1/task/1/fdinfo/10': Permission denied
chown: changing ownership of `/proc/1/task/1/fdinfo': Permission denied
chown: changing ownership of `/proc/1/task/1/environ': Permission denied
chown: changing ownership of `/proc/1/task/1/auxv': Permission denied
chown: changing ownership of `/proc/1/task/1/status': Permission denied
<--output-truncated-->

Conclusion

In this tutorial we learned how to change ownership of files and directories in linux. Thanks for reading and let us know your comments.

Raghu 9:16 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.