In Linux and Unix-like operating systems, 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.
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
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
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
Change ownership for directories recursively
To apply ownership recursively on all files and directories under a given directory use
In the following example, the ownership of 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 option shows all the ownership changing on the terminal. It outputs the diagnostics for each file processed.
# 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
--changes option, the output is reported only when changes are made.
Change ownership for symbolic links
chown command won't change ownership of symbolic links rather it changes ownership on the target file. Chmod has an option
-h that can be used to change ownership for symbolic links.
Check the following example:
# chown -h tom vmlinuz # ls -l total 0 lrwxrwxrwx 1 tom root 31 Apr 5 00:50 vmlinuz -> /boot/vmlinuz-4.15.0-88-generic
Change ownership same as reference file
You can reference a file from which
chown copy the same user and group ownership to a new file.
In following command
--reference=myfile1.txt option to provide reference file and ownership assigned to file named 'myfile2.txt':
# chown --reference=myfile1.txt myfile2.txt
A normal user cannot change the ownership of files owned by others. So an error is displayed when a normal user tries to change the ownership.
[tom@node051 ~]$ chown tom /etc/ chown: changing ownership of `/etc/': Operation not permitted
But if we use
--quiet option, the error is not displayed.
[tom@node051 ~]$ chown -f tom /etc/ [tom@node051 ~]$
How to preserve the root
Chmod command has an option
--preserve-root to prevent chmod from acting recursively on
/. This option should be used with
-R option to take effect.
[root@node051 ~]# chown -c --preserve-root tom / changed ownership of '/' from root to tom
Only the permissions of the
/ will be changed and ownership of files and directories inside
/ will remain the same.
[root@node051 ~]# ls -ld / drwxr-xr-x 23 tom root 4096 Feb 19 14:36 / [root@node051 ~]# ls -l total 88 drwxr-xr-x 2 root root 4096 Feb 19 14:33 bin drwxr-xr-x 3 root root 4096 Feb 19 14:36 boot drwxr-xr-x 16 root root 3660 Mar 28 11:09 dev drwxr-xr-x 90 root root 4096 Apr 4 02:28 etc drwxr-xr-x 2 root root 4096 Feb 19 14:35 home lrwxrwxrwx 1 root root 33 Feb 19 14:35 initrd.img -> boot/initrd.img-4.15.0-88-generic lrwxrwxrwx 1 root root 33 Feb 19 14:29 initrd.img.old -> boot/initrd.img-4.15.0-76-generic drwxr-xr-x 22 root root 4096 Feb 19 14:37 lib drwxr-xr-x 2 root root 4096 Feb 19 14:28 lib64 drwx------ 2 root root 16384 Feb 19 14:27 lost+found
In this tutorial, we learned how to change ownership of files and directories in Linux. Thanks for reading and let us know your comments.