How to Use Chown Command to Change File Ownership [11 Examples]

[ad_1]

Brief: In this beginner’s guide, we will discuss some practical examples of the chown command. After following this guide, users will be able to manage file ownership effectively in Linux.

In Linux, everything is a file, which means, all input/output resources, such as files, directories, disk drives, printers, etc are exposed as files through the file system namespace. In addition to this, there is ownership associated with each and every file in Linux.

The ownership is represented by two entities – user and group. The combination of access permissions and ownership allows Linux to implement an access control mechanism in an effective way.

In this guide, we will learn about the chown command. As the name suggests, the chown command is used to change the ownership of the files. After following this guide, beginners will be able to use the chown command effectively while working with Linux systems.

chown Command Syntax

The syntax of the chown command is as follows:

$ chown [OPTION]... [OWNER][:[GROUP]] [FILE-1] [FILE-2]...[FILE-N]

Now let’s understand the usage of the chown command with some practical examples in Linux.

1. How to Find the Ownership of the File

The easiest way to find the owner of the file is using the ls command, which will list the user and group of the file.

$ touch file-1.txt
$ ls -l file-1.txt 
Check File Ownership in Linux
Check File Ownership in Linux

In the above output, we can see that the file is owned by the tecmint user and group. This information is represented by the third and fourth columns respectively.

2. How to Change Ownership of File

The chown command allows us to change the ownership of the file. Let’s see its usage by setting user narendra as the owner of the file:

$ sudo chown narendra file-1.txt

Now, let’s verify that ownership of the file has been changed:

$ ls -l file-1.txt 
Change File Ownership in Linux
Change File Ownership in Linux

3. How to Change Group Ownership of File

Similar to the user, we can also change the group ownership of the file using the chown command. So, let’s set group ownership of the file to the group – narendra:

$ sudo chown :narendra file-1.txt

It is important to note that, we have to use a colon (:) with the group name while changing the group ownership.

Now, let’s verify the group ownership of the file:

$ ls -l file-1.txt
Change File Group Ownership in Linux
Change File Group Ownership in Linux

4. How to Change Ownership and Group of File

In the previous examples, we used the chown command twice. First, to change the user ownership and then to change the group ownership. However, we can change both user and group using a single command.

Let’s reset the file ownership to the user and group tecmint, using the following command:

$ sudo chown tecmint:tecmint file-1.txt

In this example, we have used the colon (:) character to separate the user and group. The value before the colon represents the user whereas the value after the colon represents the group.

Now, let’s check the updated file ownership:

$ ls -l file-1.txt 
Change File Ownership and-Group
Change File Ownership and-Group

5. How to Change Ownership of the Symbolic Link

By default, the chown command dereferences the symbolic link, which means, if the input file is a symbolic link then it changes the ownership of the reference file instead of the symbolic link itself.

However, we can override the default behavior using the -h option as shown in the following example.

First, create a symbolic link and verify that it’s pointing to the correct reference file:

$ ln -s file-1.txt symlink
$ ls -l symlink 

Next, change the ownership of the symbolic link using the -h option:

$ sudo chown -h narendra:narendra symlink

Finally, verify the ownership of the symbolic link and its reference file:

$ ls -l symlink file-1.txt
Change Ownership of Symbolic Link
Change Ownership of Symbolic Link

6. How to Transfer File Ownership to the User

Sometimes, we need to update the ownership of the file only after validating its current ownership. In such cases, we can use the --from option of the chown command as shown.

$ sudo chown -h --from narendra:narendra tecmint:tecmint symlink

In this example, the --from option represents the current owner of the file whereas the next argument represents the new ownership. So the above command updates the ownership of the file – symlink, only if the file is owned by the user and group – narendra.

Now, let’s check the updated ownership of the file:

$ ls -l symlink
Transfer File Ownership to User in Linux
Transfer File Ownership to the User in Linux

In this example, we have specified both user and group using the colon (:) character. However, we can specify either of them as discussed in the previous examples.

7. How to Copy Ownership From Another File

Sometimes, it’s convenient to copy the ownership from the existing file instead of providing the same from the command line. In such scenarios, we can use the --reference option with the chown command, which represents the file from which ownership is to be copied.

First, create a new file and change its ownership:

$ touch file-2.txt
$ sudo chown narendra:narendra file-2.txt

Now, let’s check the current ownership of both files:

$ ls -l file-1.txt file-2.txt 

Next, set the ownership of the file-2.txt file the same as the file-1.txt using the following command:

$ sudo chown --reference=file-1.txt file-2.txt

Finally, verify that the ownership has been updated successfully:

$ ls -l file-1.txt file-2.txt 
Copy Ownership From Another File in Linux
Copy Ownership From Another File in Linux

In the above output, we can see that now both files have the same ownership.

8. How to Change Ownership of the Directory Recursively

We can use the chown command to change the ownership of the directory as well. However, the default behavior of the command is non-recursive.

It means that the chown command will change the ownership of the input directory only. However, we can override this default behavior using the -R option as shown in the following example.

First, create a directory and two files into it:

$ mkdir dir-1
$ touch dir-1/demo-1.txt dir-1/demo-2.txt

Next, check the ownership of the directory and its files:

$ ls -ld dir-1
$ ls -l dir-1

Then, change the ownership of the directory and its files in a recursive way:

$ sudo chown -R narendra:narendra dir-1

Finally, verify the ownership of the directory and its files:

$ ls -ld dir-1
$ ls -l dir-1
Change Directory Ownership Recursively in Linux
Change Directory Ownership Recursively in Linux

9. How to Print Chown Command Process Details

By default, the chown command doesn’t print anything on the terminal after changing the ownership. Hence, so far we have been using the -l option of the ls command to verify the updated ownership.

To overcome this limitation, we can enable the verbose mode of the command that prints the diagnostics for each processed file. This option gives meaningful information when we use it with the -R option:

So, let’s use the -v option of the command to enable the verbose mode:

$ sudo chown -Rv tecmint:tecmint dir-1

Now, let’s check the output of the command:

Enable Chown Verbose Mode in Linux
Enable Chown Verbose Mode in Linux

10. How to Suppress Chown Command Errors

Like other Linux commands, chown also provides meaningful information in case of error scenarios. The error can happen due to various reasons, such as non-existing files, groups, or users, insufficient permission to perform certain operations, and so on.

However, sometimes we don’t want to show these error messages. In such cases, we can use the -f option of the command to suppress the error messages.

To understand this in a better way, let’s try to change the ownership of the non-existing file:

$ sudo chown -f narendra:narendra non-existing-file.txt
$ echo $?
1

Now, let’s see the output of the command:

Suppress Chown Command Errors in Linux
Suppress Chown Command Errors in Linux

As we can see, the above command doesn’t show any error. However, the command reports failure using the non-zero return value.

11. How to Change File User and Group ID

So far, we used the user and group names to change the ownership of the file. However, we can also use the user and group IDs to achieve the same result.

First, use the id command to find the user and group ID of the user – narendra:

$ id narendra

Now, let’s use the user and group ID 1001 with the chown command:

$ ls -l file-1.txt
$ sudo chown 1001:1001 file-1.txt

Finally, verify that the ownership has been updated successfully:

$ ls -l file-1.txt
Change File Ownership UID and GID in Linux
Change File Ownership UID and GID in Linux

In this article, we discussed some practical examples of the chown command. One can use these examples in day-to-day life to boost productivity while working with Linux systems.

Do you know of any other best example of the chown command in Linux? Let us know your views in the comments below.

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00