How to Change File/Directory User and Group Ownership in Linux

Linux is a multi-user system that can be accessed simultaneously by multiple users.

File permissions are a way to restrict users to access other user’s files on Linux.

It is controlled by file ownership and file permissions.

Each file in Linux has 3 types of owners, it’s user, group, and others.

A user is the owner of the file, and group is the one to which the user belongs to.

Each file and directory has three permissions for all three owners, such as read, write, and execute.

This can be done using the chown command and the chgrp command.

By default these commands do not display any output, so use the “-v” option to get information about what’s being done.

Use the ls command to find out who owns a file and which group the file belongs to.

$ ls -lh renu.txt
-rw-r--r-- 1 daygeek daygeek 18 Sep 29 10:36 renu.txt

What’s chown Command

The chown command is used to change the user and/or group ownership of each given file. This allows the user to perform various actions and the details are described below.

The common syntax is as follows:

chown [Options] [Owner_Name]:[Group_Name] [File/Folder_Name]
  • USER – If you provide only a username, the group of files will not be changed, becoming the owner of a given user file.
  • USER: – If you provide a username followed by a colon, the given user will become the owner of the files and the group of files will be converted into that user login group.
  • USER:GROUP – If you provide a username followed by a colon and group name, the given user will become the owner of the files and the group of files.
  • :GROUP – If you provide a group name after the colon, and owner is excluded, only the group of files will be transferred. It works the same way as chgrp.
  • : – If you only provide a colon, no action is performed.

Alternatively, you can use “-c” switch instead of “-v” switch, it works like verbose but report only when a change is made.

1) How to Change the Owner of a File/Directory on Linux Using the chown Command

In this example, we are going to change the ownership of the “passwd-up.sh” file from “daygeek” to “root”.
Before

Before:
-------
$ ls -lh passwd-up.sh
-rw-r--r-- 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh

Command:
--------
$ sudo chown -v root passwd-up.sh
changed ownership of 'passwd-up.sh' from daygeek to root


After:
-------
$ ls -lh passwd-up.sh
-rw-r--r-- 1 root daygeek 159 Aug 19 00:48 passwd-up.sh

2) How to Change the Group Ownership of a File/Directory on Linux Using the chown Command

In this example, we are going to change the group ownership of the file “mysql_backup.sh” from “daygeek” to “root”.

Before:
-------
$ s -lh mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh

Command:
--------
$ sudo chown -v :root mysql_backup.sh
changed ownership of 'mysql_backup.sh' from daygeek:daygeek to :root

Before:
-------
$ ls -lh mysql_backup.sh
-rw-r--r-- 1 daygeek root 761 Aug 19 00:48 mysql_backup.sh

3) How to Change the User and Group Ownership of a File/Directory on Linux Using the chown Command

Use the example below to change the file’s user and group ownership. In this example, the user and group ownership of the “test.txt” file will be changed from “daygeek” to “root”.

Before:
-------
$ ls -lh test.txt
-rw-r--r-- 1 daygeek daygeek 18 Aug 19 12:33 test.txt

Command:
--------
$ sudo chown -c root:root test.txt
changed ownership of 'test.txt' from daygeek:daygeek to root:root

After:
------
$ ls -lh test.txt 
-rw-r--r-- 1 root root 18 Aug 19 12:33 test.txt

4) How to Change the User and Group Ownership Recursively on Linux Using the chown Command

In this example, the ownership of all the files and its sub-directories is changed from “daygeek” to “root”, which includes the parent directory rights.

Before:
-------
$ ls -ld old
drwxr-xr-x 2 daygeek daygeek 4096 Aug 19 12:33 old/

$ ls -lh old
total 24K
-rw-r--r-- 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh
-rw-r--r-- 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh
-rw-r--r-- 1 daygeek root    761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek  98 Aug 19 00:48 passwd-up1.sh
-rw-r--r-- 1 root    daygeek 159 Aug 19 00:48 passwd-up.sh
-rw-r--r-- 1 daygeek daygeek  18 Aug 19 12:33 test.txt

Command:
--------
$ sudo chown -Rv root:root old
changed ownership of 'old/mysql_backup_1.sh' from daygeek:daygeek to root:root
changed ownership of 'old/test.txt' from daygeek:daygeek to root:root
changed ownership of 'old/passwd-up1.sh' from daygeek:daygeek to root:root
changed ownership of 'old/mysql_backup_2.sh' from daygeek:daygeek to root:root
changed ownership of 'old/mysql_backup.sh' from daygeek:root to root:root
changed ownership of 'old/passwd-up.sh' from root:daygeek to root:root
changed ownership of 'old' from daygeek:daygeek to root:root

After:
------
$ ls -ld old
drwxr-xr-x 2 root root 4096 Aug 19 12:33 old/

$ ls -lh old
total 24K
-rw-r--r-- 1 root root 237 Aug 19 00:48 mysql_backup_1.sh
-rw-r--r-- 1 root root 241 Aug 19 00:48 mysql_backup_2.sh
-rw-r--r-- 1 root root 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 root root  98 Aug 19 00:48 passwd-up1.sh
-rw-r--r-- 1 root root 159 Aug 19 00:48 passwd-up.sh
-rw-r--r-- 1 root root  18 Aug 19 12:33 test.txt

5) How to Change User and Group Ownership of Multiple Files on Linux Using the chown Command

To change the user and group ownership of multiple files, use the example below.

Before:
-------
$ ls -lh passwd-up.sh mysql_backup.sh 
-rw-r--r-- 1 root root 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 root root 159 Aug 19 00:48 passwd-up.sh

Command:
--------
$ sudo chown -c daygeek:daygeek passwd-up.sh mysql_backup.sh
changed ownership of 'passwd-up.sh' from root:root to daygeek:daygeek
changed ownership of 'mysql_backup.sh' from root:root to daygeek:daygeek

After:
------
$ ls -lh passwd-up.sh mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh

6) How to Change the User and Group Ownership of Link Files on Linux Using the chown Command

Use the following examples for changing ownership of link files.

When you use the chown command as usual in link files, it only changes the ownership of the source file, not the link file itself. In this example, it changes the ownership of the “output.txt” file from “daygeek” to “root”.

Before:
-------
$ ls -lh /home/daygeek/test/daygeek 
lrwxrwxrwx 1 daygeek daygeek 32 Sep 26 14:35 /home/daygeek/test/daygeek -> /home/daygeek/test/2g/output.txt

$ ls -lh /home/daygeek/test/2g/output.txt
-rw-r--r-- 1 daygeek daygeek 18 Jan 19  2019 /home/daygeek/test/2g/output.txt

Command:
--------
$ sudo chown -v root:root daygeek
changed ownership of 'daygeek' from daygeek:daygeek to root:root

After:
------
$ ls -lh /home/daygeek/test/daygeek
lrwxrwxrwx 1 daygeek daygeek 32 Sep 26 14:35 /home/daygeek/test/daygeek -> /home/daygeek/test/2g/output.txt

$ ls -lh /home/daygeek/test/2g/output.txt
-rw-r--r-- 1 root root 18 Jan 19  2019 /home/daygeek/test/2g/output.txt

Use the “-h” option with the chown command to change the ownership of the link file instead of the source file.

Before:
-------
$ ls -lh /home/daygeek/test/daygeek 
lrwxrwxrwx 1 daygeek daygeek 32 Sep 26 14:35 /home/daygeek/test/daygeek -> /home/daygeek/test/2g/output.txt

$ ls -lh /home/daygeek/test/2g/output.txt
-rw-r--r-- 1 daygeek daygeek 18 Jan 19  2019 /home/daygeek/test/2g/output.txt

Command:
--------
$ sudo chown -hv root:root daygeek
changed ownership of 'daygeek' from daygeek:daygeek to root:root

After:
------
$ ls -lh /home/daygeek/test/daygeek
lrwxrwxrwx 1 root root 32 Sep 26 14:35 /home/daygeek/test/daygeek -> /home/daygeek/test/2g/output.txt

$ ls -lh /home/daygeek/test/2g/output.txt
-rw-r--r-- 1 daygeek daygeek 18 Jan 19  2019 /home/daygeek/test/2g/output.txt

Note: “-L” and “-H” options can be used when recursively changing the symbolic link directory ownership.

7) How do i Change the User and Group Ownership of a File Using the “–from=” option Using the chown Command?

Alternatively, you can use the “–from=” option to change ownership of files.

Before:
-------
$ ls -lh 1.txt
-rw-r--r-- 1 daygeek daygeek 0 Jul 22 12:28 1.txt

Command:
--------
$ sudo chown -v --from=daygeek:daygeek root:root 1.txt
changed ownership of '1.txt' from daygeek:daygeek to root:root

After:
------
$ ls -lh 1.txt
-rw-r--r-- 1 root root 0 Jul 22 12:28 1.txt

8) How to Copy User and Group Ownership from One File to Another on Linux Using the chown Command

In some cases, if you want to copy ownership from one file to another, use the example below.
In this example, the owner permission is copied from the “2.txt” file to the “renu.txt” file.

Before:
-------
$ ls -lh renu.txt 
-rw-r--r-- 1 root root 18 Sep 29 10:36 renu.txt

$ ls -lh 2.txt
-rw-r--r-- 1 daygeek daygeek 0 Jul 22 12:28 2.txt

Command:
--------
$ sudo chown -v --reference=/home/daygeek/test/2.txt /home/daygeek/test/renu.txt 
changed ownership of '/home/daygeek/test/renu.txt' from root:root to daygeek:daygeek

After:
------
$ ls -lh renu.txt
-rw-r--r-- 1 daygeek daygeek 18 Sep 29 10:36 renu.txt

9) Bonus Tips: How To Find And Change Ownership of Files Based On Specific Extension

In this example, the ownership of the “.sh” extension files will be changed to “daygeek”.

Here are some helpful find command tutorials for you.

Before:
-------
$ ls -lh
total 24K
-rw-r--r-- 1 root    root    237 Aug 19 00:48 mysql_backup_1.sh
-rw-r--r-- 1 root    root    241 Aug 19 00:48 mysql_backup_2.sh
-rw-r--r-- 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 root    root     98 Aug 19 00:48 passwd-up1.sh
-rw-r--r-- 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh
-rw-r--r-- 1 root    root     18 Aug 19 12:33 test.txt

Command:
--------
$ sudo find /home/daygeek/shell-script/backup/old/  -type f -iname "*.sh" -exec chown -v daygeek:daygeek {} \;
changed ownership of '/home/daygeek/shell-script/backup/old/mysql_backup_1.sh' from root:root to daygeek:daygeek
changed ownership of '/home/daygeek/shell-script/backup/old/passwd-up1.sh' from root:root to daygeek:daygeek
changed ownership of '/home/daygeek/shell-script/backup/old/mysql_backup_2.sh' from root:root to daygeek:daygeek
ownership of '/home/daygeek/shell-script/backup/old/mysql_backup.sh' retained as daygeek:daygeek
ownership of '/home/daygeek/shell-script/backup/old/passwd-up.sh' retained as daygeek:daygeek

After:
------
$ ls -lh
total 24K
-rw-r--r-- 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh
-rw-r--r-- 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh
-rw-r--r-- 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek  98 Aug 19 00:48 passwd-up1.sh
-rw-r--r-- 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh
-rw-r--r-- 1 root    root     18 Aug 19 12:33 test.txt

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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