chown Command in Linux with Examples
chown Command
CHOWN command is used to change the ownership of the user or group of a particular file or directory in Linux. All the files and documents belong to specific ownership. It can be changed with the help of chown and chgrp commands.
This command needs root privilege.
Syntax
chown < Options> < Owner> .< Group> < File/Directories>
To change the user ownership of a file
chown command helps to change only the user ownership of a file using chown command.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Jan 23 02:38 example [root@linuxhelp test]# chown user1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 user1 root 0 Jan 23 02:38 example
The above output shows that the user ownership of the file ' example' is changed from root to user1
To change the group ownership of a file
chown command helps to change the group ownership of a file using chmod command.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Jan 23 02:38 example [root@linuxhelp test]# chown .group1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root group1 0 Jan 23 02:38 example
In the output the group ownership of the file ' example' is changed from root to group1
Another Method
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Jan 23 02:38 example [root@linuxhelp test]# chgrp group1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root group1 0 Jan 23 02:38 example
To change both owner and the group in single command
chown command helps to change both the user and group ownership in a single command.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Jan 23 02:38 example [root@linuxhelp test]# chown user1.group1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 user1 group1 0 Jan 23 02:38 example
Thus the user owner and group owner can be changed in a single command line.
To change the ownership if a file is owned by a particular user
The option --from is used to change the directory user from one to other.
[root@linuxhelp test]# ls -l
total 0
-rw-r--r-- 1 root group1 0 Jan 23 02:38 example
Now change the file' s owner to user1, only if it already owned by the user user2
[root@linuxhelp test]# chown --from=user2 user1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root group1 0 Jan 23 02:38 example [root@linuxhelp test]# chown --from=root user1 example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 user1 group1 0 Jan 23 02:38 example
Now the ownership has been changed because first it searches for the user mentioned in ' --from=user' and replaces by the new user given finally. In the above command it searches for the user root and replaces by the user user1.
To change group only if a file already belongs to a particular group
Like the previous command, we use --from option to change, but here we use group name instead of user.
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root group1 0 Jan 23 02:38 example [root@linuxhelp test]# chown --from=.group1 .office example [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root office 0 Jan 23 02:38 example
Now the group owner is changed from group1 to office.
To copy the owner/group settings from one file to another
To copy the setting from a file we have to enter the --reference option preceding the reference filename.
Run the below command to copy the owner/group settings of the ' new' to the file ' example'
[root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 root root 0 Jan 23 02:38 example drwxr-xr-x 2 user1 office 6 Jan 23 05:44 new [root@linuxhelp test]# chown --reference=new/ example [root@linuxhelp test]# ll total 0 -rw-r--r-- 1 user1 office 0 Jan 23 02:38 example drwxr-xr-x 2 user1 office 6 Jan 23 05:44 new
Now the owner/group setting of the reference file ' new' is also assigned to the ' example'
To change the owner/group of the files by traveling the directories recursively
chown command helps to change the owner or group of files recursively by traveling through the directories.
[root@linuxhelp test]# ls -l
total 0
-rw-r--r-- 1 user1 office 0 Jan 23 02:38 example
drwxr-xr-x 2 user1 office 6 Jan 23 05:44 new
drwxr-xr-x 2 root root 6 Jan 23 05:57 sample
In the below command we have used two new options ' -R & -v' .' -R' which is for recursively applying ownership for the below mentioned directories and their sub directories and the other one ' -v' is to list the process running on the given command.
[root@linuxhelp Desktop]# chown -Rv user1.group1 test/ changed ownership of ‘ test/example’ from user1:office to user1:group1 changed ownership of ‘ test/new’ from user1:office to user1:group1 changed ownership of ‘ test/sample’ from root:root to user1:group1 changed ownership of ‘ test/’ from user1:user1 to user1:group1 [root@linuxhelp test]# ls -l total 0 -rw-r--r-- 1 user1 group1 0 Jan 23 02:38 example drwxr-xr-x 2 user1 group1 6 Jan 23 05:44 new drwxr-xr-x 2 user1 group1 6 Jan 23 05:57 sample
Now the owner/group setting of the directory test is applied to it and also to its sub directories and files.
To change the ownership of all the files present inside a directory
Using chown command we can change the user or group ownership of the entire file or a directory. This action will not affect the parent directory it only takes effects on the files present inside the directory.
[root@linuxhelp example3]# ls -l total 12 drwxr-xr-x. 2 user2 group1 4096 Feb 3 10:59 1 drwxr-xr-x. 2 user2 group1 4096 Feb 3 10:59 2 drwxr-xr-x. 2 user2 group1 4096 Feb 3 10:59 3 [root@linuxhelp example3]# chown root.root * [root@linuxhelp example3]# ls -l total 12 drwxr-xr-x. 2 root root 4096 Feb 3 10:59 1 drwxr-xr-x. 2 root root 4096 Feb 3 10:59 2 drwxr-xr-x. 2 root root 4096 Feb 3 10:59 3
In the output, you can see all the file user and group ownership is changed to root.