Atlantic.Net Blog

How to Use chown (Change Ownership) Command in Linux

The chown command, change owner, is a Linux command-line tool used to change the file and directory ownership. Only root and admin users can access all files and directories in the file system in Linux. Each file and directory is associated with an owner and group owner. You can’t access any files that are owned by other users. In such a case, you will need to change the owner of the file to grant file access. The chown command allows you to grant read, write, and execute level permission on files and directories.

In this post, we will show you how to use the chown command in Linux.

In This Article

Basic Syntax of chown Command

The basic syntax of using the chown command is shown below:

chown [option] [owner][group] [file]/[directory]

A brief explanation of each option is shown below:

  • -c Display verbose output only after the change is made.
  • -f Suppress most error messages
  • -v Show diagnostic for every processed file.
  • -R Apply ownership on files and directories recursively.
  • –help Used to display help information.

Display User and Group Ownership Of a File

Before changing the ownership of any file, it is important to know the existing owner of that file. You can use the ls -l command to display the user and group owner of a file.

For example, to display the user and group ownership of a file file.txt, run the following command:

ls -l file.txt

You will get the following output:

-rw-rw-r-- 1 vyom vyom 0 Mar 15 08:21 file.txt

As you can see, vyom is a user and group owner of a file.

Also Read

How to Use chgrp (Change Group Ownership) Command in Linux

Change the User Owner Of a File

To change the user owner of the file, use the below syntax:

chown username filename

For example, to change the user owner of the file named file.txt to www-data, run the following command:

chown www-data file.txt

You can now verify the ownership of file.txt using the following command:

ls -l file.txt

You should see that the owner of file.txt has changed.

-rw-rw-r-- 1 www-data vyom 0 Mar 15 08:21 file.txt

Also Read

How to Download File Using Wget in Linux

Change the Group Owner Of a File

To change the group owner of a file, use the chown command and specify the group name. The basic syntax to change the group owner is shown below:

chown :groupname filename

For example, to change the group owner of the file named file.txt to sudo, run the following command:

chown :sudo file.txt

You can now verify the changes using the following command:

ls -l file.txt

You should see that the group owner is changed.

-rw-rw-r-- 1 www-data sudo 0 Mar 15 08:21 file.txt

Change the User and Group Owner Of a File

If you want to change the user and group owner together, use the following syntax:

chown username:groupname file

For example, to change the user owner to root and group owner to root, run the following command:

chown root:root file.txt

If you want to change the user and group owner of a specified directory and all of their sub-directories recursively, use the following command:

chown -R www-data:www-data /var/www/html

This command will change the user and group owner of the /var/www/html directory and all of its sub-directories recursively.

Copy Ownership Settings From One File to Another

The chown command also allows you to copy the ownership settings from one file to another file. The basic syntax to copy the ownership settings is shown below:

chown --reference=filename new-filename

For example, to copy the ownership settings from file1.txt to file2.txt, run the following command:

chown --reference=file1.txt file2.txt

Conclusion

In this post, we explained how to change the ownership of files using the chown command. Try it on dedicated hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year