Cyberithub

17 Useful Linux chown command examples to change owner and group

Advertisements

In this article, I will take you through 17 Linux chown command examples to change owner and group. As you might be aware that all the file and directories in Linux are having a owner and Group assigned so if you decide to change the owner and group of a file or directory in Linux then you need to use chown and chgrp command.

chown is an open source Linux command used for changing the ownership of Files and Directories. chgrp is also an open source Linux command to change the group of files and directories. Here we will only look at the chown command and will see chgrp command in later articles. I will go through the usage of chown command using various examples.

SYNOPSIS

chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

17 Useful Linux chown command examples to change owner and group

chown command examples to Change Owner and Group

Also Read: 31 Popular ps command in Linux/Unix with Examples (Monitor Linux Processes)

Example 1: How to Check chown command version

If you want to check chown command version then you need to use chown --version command as shown below. As you can see from below output current chown version is 8.22.

[root@localhost ~]# chown --version
chown (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to Change User and Group Ownership of a File

If you want to change the owner of a file then you need to use below chown command. In this example we are changing the ownership of hello.txt file to user centos and group centos by using chown centos:centos hello.txt command.

[root@localhost ~]# chown centos:centos hello.txt

Example 3: How to Enable Verbose mode

If you want to enable the verbose mode then you need to use -v option with chown command as shown below. Enabling the verbose mode will show the command output in detail.

[root@localhost ~]# chown -v centos:centos hello.txt
changed ownership of ‘hello.txt’ from root:root to centos:centos

-v : output a diagnostic for every file processed. More can be checked on chown Man Page.

Example 4: How to report only when the user or group owner are changed

If you want to check that the chown command is changing the user or group of the file or directory then you need to use -c option as shown below. Let's say if you give the same user and group name who is already the owner of file or directory then chown command won't show anything in the output as shown below. Here owner centos user and centos group are already associated with hello.txt file so assigning same user and group won't show anything in the output.

[root@localhost ~]# chown -c centos:centos hello.txt

-c : like verbose but report only when a change is made. More can be checked on chown command Man Page.

But if you change the owner to different user and group then it will show that change in the output as shown below. Here we are changing the owner of hello.txt file to user root and group root from previous owner centos and group centos using chown -c root:root hello.txt command so it is displaying that change in the output. This confirms that we are assigning different owner and group to hello.txt file.

[root@localhost ~]# chown -c root:root hello.txt
changed ownership of ‘hello.txt’ from centos:centos to root:root

Example 5: How to Change the ownership of a Directory

If you want to change the ownership of a directory then you need to use same chown command as you have used in above examples. There is not much difference in changing the ownership of a file and directory. In this example we are changing the ownership of example directory but first we need to check the current owner and group of this directory by using ls -lrtd example command. As you can see from below output owner is root and  associated group is also root.

[root@localhost ~]# ls -lrtd example
drwxrwxrwx 2 root root 35 Jun 28 14:02 example

So to change the ownership of example directory to user centos and group to centos we need to use chown  -v centos:centos example command as shown below.

[root@localhost ~]# chown -v centos:centos example
changed ownership of ‘example’ from root:root to centos:centos

Here you need to remember that we are only changing the ownership of example directory and not the files and directories contains within it as you can see below. The owners of all the files of example directory are still the same. It has not changed its owner to User centos and group centos.

[root@localhost ~]# ls -lrt example
total 2848
-rwxrwxrwx 1 root root 58 Jun 28 14:02 hello.rs
-rwxrwxrwx 1 root root 2912208 Jun 28 14:02 hello

Example 6: How to Only change the Owner of a File

If you are interested in only changing the owner of a file and not the group then you can use below chown command. In this example we are changing the owner of hello.txt from user centos to user root but first we are checking the current owner of hello.txt file using ls -lrt hello.txt command as shown below.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 centos centos 29 Jun 18 03:23 hello.txt

Now we can change the owner of hello.txt file to root using chown -v root hello.txt command as shown below.

[root@localhost ~]# chown -v root hello.txt
changed ownership of ‘hello.txt’ from centos to root

If you verify again using ls -lrt hello.txt command then you can see that owner is not changed to root.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 root centos 29 Jun 18 03:23 hello.txt

Example 7: How to Change Only the Group of a File

If you are only interested in changing the group of a File then you can do that by using below chown command. In this example hello.txt group is root which we will change it to group centos. Before changing the group we need to verify current group of hello.txt file by using ls -lrt hello.txt command as shown below.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 root root 29 Jun 18 03:23 hello.txt

Now we can change the group of hello.txt file using chown -v :centos hello.txt command as shown below.

[root@localhost ~]# chown -v :centos hello.txt
changed ownership of ‘hello.txt’ from root:root to :centos

If you verify again using ls -lrt hello.txt command then you can see that the group is now changed to centos.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 root centos 29 Jun 18 03:23 hello.txt

Example 8: How to Change the owner and group of a Symbolic Link 

If you want to change the owner and group of a Symbolic Link then you need use below chown command. In this example, we have a symbolic link hello pointing to hello.txt file as shown below. Also we can see root is the current owner and group of this symbolic link.

[root@localhost ~]# ls -lrt hello
lrwxrwxrwx 1 root root 9 Aug 19 17:58 hello -> hello.txt

Now to change the ownership to user centos and group centos we need to use chown -v centos:centos hello command as shown below.

[root@localhost ~]# chown -v centos:centos hello
changed ownership of ‘hello’ from root:root to centos:centos

If you want to verify the changes then you can run ls -lrt hello command and check the ownership.

[root@localhost ~]# ls -lrt hello
lrwxrwxrwx 1 centos centos 9 Aug 19 17:58 hello -> hello.txt

Example 9: How to replicate File Access to Another File

If you want to replicate access of a file to another file then you need to use --reference option with chown command as shown below. In this example we are replicating the access of hello.txt file to example.txt file. First check the access of hello.txt file using ls -lrt hello.txt command.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 centos centos 29 Jun 18 03:23 hello.txt

Then replicate the hello.txt file access to example.txt file using chown -v --reference=hello.txt example.txt command as shown below.

[root@localhost ~]# chown -v --reference=hello.txt example.txt
changed ownership of ‘example.txt’ from root:root to centos:centos

--reference=RFILE : use RFILE's owner and group rather than specifying OWNER:GROUP values. More can be checked on chown command Man Page.

Example 10: How to Recursively change the ownership of all the Files in a Directory

If you want to recursively change the ownership of all the files in a directory then you need to use -R option with chown command as shown below. In this example, we have a directory example which contains two files hello.rs and hello.

[root@localhost ~]# ls -lrt example
total 2848
-rwxrwxrwx 1 root root 58 Jun 28 14:02 hello.rs
-rwxrwxrwx 1 root root 2912208 Jun 28 14:02 hello

Now to recursively change the ownership of all files of example directory to user centos and group centos you need to use chown -R -v centos:centos example command as shown below.

[root@localhost ~]# chown -R -v centos:centos example
changed ownership of ‘example/hello.rs’ from root:root to centos:centos
changed ownership of ‘example/hello’ from root:root to centos:centos
ownership of ‘example’ retained as centos:centos

-R : operate on files and directories recursively. More can be checked on chown command Man Page.

If you again check the access of all the files of example directory then you can see it has now changed to user centos and group centos.

[root@localhost ~]# ls -lrt example
total 2848
-rwxrwxrwx 1 centos centos 58 Jun 28 14:02 hello.rs
-rwxrwxrwx 1 centos centos 2912208 Jun 28 14:02 hello

Example 11: Change owner only when if it is assigned to Specific Owner

If you want to change the owner of a file only when if it is assigned to a Specific Owner then you need to use --from option with chown command as shown below. In this example, we will change the owner of hello.txt file to root only when its current owner is centos. If it has some other owner then it won't change the ownership to root user. First we need to check the current owner of hello.txt file using ls -lrt hello.txt command.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 centos centos 29 Jun 18 03:23 hello.txt

Now if we try to change the owner of file hello.txt to test from admin user using command chown -v --from=admin test hello.txt then it won't change it as the current owner is centos and not the admin user.

[root@localhost ~]# chown -v --from=admin test hello.txt
ownership of ‘hello.txt’ retained as centos

--from : change the owner and/or group of each file only if its current owner and/or group match those specified here. More can be checked on chown command Man Page.

Again if you run the command chown -v --from=centos root hello.txt then it will now change the ownership to user root as the current owner is user centos.

[root@localhost ~]# chown -v --from=centos root hello.txt
changed ownership of ‘hello.txt’ from centos to root

Example 12: Change the group of a file Only when it is assigned to a Specific Group

If you want to change the owner of a file only when if it is assigned to a Specific Owner then you need to use --from option with chown command as shown below. In this example, we will change the owner of hello.txt file to root only when its current owner is centos. If it has some other owner then it won't change the ownership to root user. First we need to check the current owner of hello.txt file using ls -lrt hello.txt command.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 root centos 29 Jun 18 03:23 hello.txt

Now to change the group of hello.txt file to root from centos group you need to use chown -v --from=:centos :root hello.txt command as shown below.

[root@localhost ~]# chown -v --from=:centos :root hello.txt
changed ownership of ‘hello.txt’ from root:centos to :root

Example 13: Change the owner and group of a file Only when it is assigned to a Specific Owner and Group

If you want to change the owner and group of a file only when it is assigned to a specific user and group then you need to use below chown command. First we need to check the owner and group of hello.txt file using ls -lrt hello.txt command.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 root root 29 Jun 18 03:23 hello.txt

Now to change the owner and group of hello.txt file to centos you need to use chown -v --from=root:root centos:centos hello.txt command as shown below.

[root@localhost ~]# chown -v --from=root:root centos:centos hello.txt
changed ownership of ‘hello.txt’ from root:root to centos:centos

If you verify the ownership of hello.txt file again using ls -lrt hello.txt command then you can see it has now changed to user centos and group centos.

[root@localhost ~]# ls -lrt hello.txt
-r--rw-rw- 1 centos centos 29 Jun 18 03:23 hello.txt

Example 14: How to forcefully change the owner and group of a Symbolic Link File

If you want to forcefully change the owner and group of a Symbolic Link file then you need to use -h option with chown command as shown below. First we need to check the owner and group of symbolic link hello using ls -lrt hello command.

[root@localhost ~]# ls -lrt hello
lrwxrwxrwx 1 root root 9 Aug 19 17:58 hello -> hello.txt

Now to change the owner and group of hello symbolic link to centos you need to use chown -v -h centos:centos hello command as shown below.

[root@localhost ~]# chown -v -h centos:centos hello
changed ownership of ‘hello’ from root:root to centos:centos

-h : affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink). More can be checked on chown command Man Page.

If you again check the owner and group of symbolic Link hello using ls -lrt hello command then you can see it has now changed to centos as described below.

[root@localhost ~]# ls -lrt hello
lrwxrwxrwx 1 centos centos 9 Aug 19 17:58 hello -> hello.txt

Example 15: How to forcefully change the owner and group of a Symbolic link directory 

If you want to forcefully change the owner and group of a Symbolic link directory then you need to use -H option with chown command as shown below. In this example, first we are checking the owner and group of example directory symlink test using ls -lrt test command as shown below.

[root@localhost ~]# ls -lrt test
lrwxrwxrwx 1 root root 7 Aug 19 18:51 test -> example

Here we are changing the owner and group of symbolic link test using chown -v -R -H centos:centos test command as shown below.

[root@localhost ~]# chown -v -R -H centos:centos test
ownership of ‘test/hello.rs’ retained as centos:centos
ownership of ‘test/hello’ retained as centos:centos
ownership of ‘test’ retained as centos:centos

-H : if a command line argument is a symbolic link to a directory, traverse it. More can be checked on chown command Man Page.

Example 16: How to Check all the other options of chown command

If you want to check all the available options of chown command then you need to use chown --help command as shown below.

[root@localhost ~]# chown --help
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
or: chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.

-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)

Example 17: How to Check Man page of chown command

If you want to check the man page of chown command then you need to use man chown command as shown below.

[root@localhost ~]# man chown
CHOWN(1) User Commands CHOWN(1)

NAME
chown - change file owner and group

SYNOPSIS
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

DESCRIPTION
This manual page documents the GNU version of chown. chown changes the user and/or group ownership of each given file. If only an owner (a user name or
numeric user ID) is given, that user is made the owner of each given file, and the files' group is not changed. If the owner is followed by a colon and a
group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well. If a colon but no group name follows
the user name, that user is made the owner of the files and the group of the files is changed to that user's login group. If the colon and group are given,
but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as chgrp. If only a colon is given, or if
the entire operand is empty, neither the owner nor the group is changed.

 

 

 

 

 

Popular Recommendations:-

12 Popular Unix/Linux uname command examples(How to Get Kernel Version)

Learn HTML Tables(v5) with Best Examples

Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7

C# data types with Best Examples (.NET v4.7)

How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps

Learn HTML Image Maps(v5) with Best Examples

How to Install PHP on RedHat/CentOS 7 with Easy Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

Leave a Comment