Skip to main content

File Properties Commands

How to Use chown Command to Change Ownership in Linux

The chown command in Linux enables you to change the user and group ownership of a file or directory. Learn to use chown with some practical examples.

The chown command in Linux allows you to change the ownership of files and directories. You can rightly guess that ‘chown’ is short for ‘change owner’.

If you are not aware of these terms, I highly recommend reading my explainer article about file permissions and ownership in Linux.

Here’s a picture to quickly recall it:

Linux file permission

With the chown command, you can change both user and group ownership of a file or a directory.

Examples of chown command in Linux

Here’s what the syntax for chown command looks like:

chown [options] user_spec files

Do keep in mind that chown is an administrative command and so you need to be root or have sudo rights in order to make changes with chown command. I hope you know how to add sudo user.

Otherwise, you’ll see an error like this:

chown: changing ownership of 'agatha.txt': Operation not permitted

Now let’s see how to use the chown command with examples.

Here’s the example set I am going to use:

-rw-r--r-- 1 abhishek abhishek  456 Jan 24 09:30 agatha.txt
drwxr-xr-x 2 abhishek abhishek 4096 Jan 24 09:31 mydir
drwxr-xr-x 3 abhishek abhishek 4096 Jan 24 09:30 new
-rw-r--r-- 1 abhishek abhishek  356 Jan 24 09:30 sherlock.txt

1. Change or set the user ownership of a file

To change the ownership of a file, use the command in this fashion:

sudo chown user_name file_name

You may also use UID (user ID) instead of the user name. Here’s an example of the changes it makes:

abhishek@nuc:~/tutorial$ sudo chown prakash agatha.txt 
abhishek@nuc:~/tutorial$ ls -l agatha.txt 
-rw-r--r-- 1 prakash abhishek 456 Jan 24 09:30 agatha.txt

As you can see the owner of the file has changed to ‘prakash’ from ‘abhishek’ but the group remains ‘abhishek’. Let me show you how to change both user ownership and group ownership.

2. Change the user and group ownership

To change the user and group ownership of a file, you can specify the group with the user name separated by a column like this:

sudo chown user_name:group_name file_name

Of course, you can use UID and GID instead of user name and group name respectively.

abhishek@nuc:~/tutorial$ sudo chown prakash:adm sherlock.txt 
abhishek@nuc:~/tutorial$ ls -l sherlock.txt 
-rw-r--r-- 1 prakash adm 356 Jan 24 09:30 sherlock.txt

As you can see in the example above, I changed the user of the file sherlock.txt to ‘prakash’ and the group to ‘adm’.

3. Change the group to default group of a user

Every user has a default or primary group. If the user creates a new file or directory, this primary group automatically becomes the group owner of the file. You can list the default group of a user with the id command.

Now if you want to change the group ownership of a file to the default group of a user, you should just leave the group name after colon.

sudo chown user_name: file_name

As you can see in the example below, sherlock.text file user owner ‘prakash‘ and group owner ‘adm‘. I changed the owner to ‘abhishek’ but didn’t provide the group name. And yet it changed the group from ‘adm’ to default ‘abhishek’ group.

abhishek@nuc:~/tutorial$ ls -l sherlock.txt 
-rw-r--r-- 1 prakash adm 356 Jan 24 09:30 sherlock.txt
abhishek@nuc:~/tutorial$ sudo chown abhishek: sherlock.txt 
abhishek@nuc:~/tutorial$ id abhishek
uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),999(docker)
abhishek@nuc:~/tutorial$ ls -l sherlock.txt 
-rw-r--r-- 1 abhishek abhishek 356 Jan 24 09:30 sherlock.txt

4. Change the group only

If you just want to change the group without being bothered by the user owner, you can use the chown command like this:

sudo chown :group_name file_name

In the example below, I set the group ownership to sudo without touching the user ownership:

abhishek@nuc:~/tutorial$ ls -l agatha.txt 
-rw-r--r-- 1 prakash abhishek 456 Jan 24 09:30 agatha.txt
abhishek@nuc:~/tutorial$ sudo chown :sudo agatha.txt 
abhishek@nuc:~/tutorial$ ls -l agatha.txt 
-rw-r--r-- 1 prakash sudo 456 Jan 24 09:30 agatha.txt

5. Change user and group ownership of a directory recursively

In all the above commands, you can replace file with directory and it will work the same for changing ownership of the directory.

The one problem here is that by default the ownership of the content inside the directory is not changed.

To change the ownership of the directory along with all the content inside the directory, you can use the recursive option -R.

sudo chown -R user_name:group_name directory_name

6. Set the same user and group ownership as a reference file

You can use a file as reference and change the user and group ownership of a file based on the reference file in this manner:

sudo chown --reference=file1.txt file2.txt

In the example below, file agatha.txt has been used as a reference. And as you can see, the ownership of sherlock.txt has been changed on the basis of agatha.txt.

abhishek@nuc:~/tutorial$ ls -l agatha.txt sherlock.txt 
-rw-r--r-- 1 prakash  sudo     456 Jan 24 09:30 agatha.txt
-rw-r--r-- 1 abhishek abhishek 356 Jan 24 09:30 sherlock.txt
abhishek@nuc:~/tutorial$ sudo chown --reference=agatha.txt sherlock.txt
abhishek@nuc:~/tutorial$ ls -l agatha.txt sherlock.txt 
-rw-r--r-- 1 prakash sudo 456 Jan 24 09:30 agatha.txt
-rw-r--r-- 1 prakash sudo 356 Jan 24 09:30 sherlock.txt

I think you have enough examples of the chown command to understand it. You can always refer to chown man page for more details.

If you have questions or suggestions, do let me know.