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.

Tag : Chown
FAQ
Q
How to set the ownership of the file or directory in recursively using the "chown" command?
A
You can use the following syntax to set the ownership of the file or directory in recursively using the "chown" command. Syntax: "chown -R root:friends /file/path"
Q
How to check the version of "chown" command in Linux?
A
You can use following command to get the version of the "chown" command in Linux. For Ex: "chown --version"
Q
What is file "ownership" in Linux?
A
Linux is designed to support a large number of users. Because of this, it needs to keep careful track of who is allowed to access a file, and how they can access it.
Q
How to get the ownership detail of the particular file or directory?
A
You can use the following command to get the ownership detail of the particular file or directory. For Ex: "ls -l filename".
Q
How to copy the owner/group settings from one file to another?
A
You can use the following syntax to copy the owner/group settings from one file to another. For Ex: "chown --reference=file1 file2"