10 ways to use the Chown command with examples

One of the most common tasks of chown is to change the ownership of a file or directory. Below are ten ways to use the Chown command with examples. Below are ten ways to use the Chown command with examples.

Any file in a Linux system belongs to a particular owner(s) and a group. It is one of the many features that maintain security in Linux systems. For example; If a file belongs to say UserA, then UserB cannot manipulate the data depending on any additional permissions set unless the user has permission to do that.

At times we need to change file ownership or the group to which the file belongs. Several Linux commands can help us achieve this; they include chown, chmod, usermod, and more. Today we shall focus on the chown command and how we can use it to change owners and groups of a file.

Chown command in Linux (with practical examples)

We need to first understand one useful Linux command that we shall use throughout our article – the ls -l command. It displays all the necessary information about a file. Including the owner, group, and permissions given to the file. Let’s look at the example below, showing the command and its output:

tuts@ubuntu:~/Desktop/FOSSLINUX$ ls -l sample-file

ls -l command output
ls -l command output

From the above output, we have seven columns. Each column represents a specific set of data.

• Column one = Indicates the type of the file and permissions assigned to it.
• Column two = Indicates the memory blocks taken by the file.
• Column three = Indicates the ownership of a file or directory – the one who created it.
• Column four = Indicates the groups to which a file is assigned.
• Column five = It indicates the size of the directory or file. It is calculated in bytes.
• Column six = It indicates the exact date and time when the file or directory was created.
• Column seven = It indicates the name of the file or directory.

In this article, we shall focus on columns three and four. They indicate the owner and group to which a file belongs. In the above example, we see the file sample-file belongs to tuts(owner) and tuts(group). Below are ten ways to use the Chown command with examples.

1. Change ownership of a file

One of the most common tasks of chown is to change the ownership of a file or directory. It’s illustrated in the image below:

ls -l command output
ls -l command output

From the above output, we can see that our sample-file belongs to the owner(tuts) and group(tuts). Let’s change the ownership to fosslinux_admin. The syntax is:

sudo chown [new_owner] [file]

Changea ownership to fosslinux_admin
Change ownership to fosslinux_admin

To confirm the changes, we s run the ls -l command again.

ls -l command
ls -l command

The changes were applied successfully.

 2. Change the group of a file

Changing the grouping of a file is essential, especially when you are dealing with multiple users. Below is the basic chown syntax to change groups.

sudo chown [:new_group] [file]

Note, for the group; we include a full colon [:] at the beginning. The current file belongs to the group, tuts; we want to change it to fosslinux_group0. It is illustrated below.

change group of a file
change group of a file

The changes are applied successfully.

3. Change both the owner and the group

Instead of changing the owner and the group separately, you can achieve this with one single chown command. Syntax:

sudo chown [new_owner][:new_group] [file]

Current oner and group
Current owner and group

The current owners and groups are fosslinux_admin and fosslinux_group0. We shall change this to the owner as tuts and group as tuts.

change both owner and group
change both owner and group

From the above output, we see that the changes are applied. We have changed both the owner and group from tuts, fosslinux_group0 to tuts, tuts, respectively.

4. Change the owner of a file by checking the existing owner first

There are cases where you don’t need to run the ls -l command to check the owner of a file before changing. It is possible using the chown –from the argument. It first confirms the existing owner before applying any changes. Syntax:

chown --from=[current_own] [new_owner] [filename]
chown --from=fosslinux_admin fosslinux_tuts sample-file

Now, by executing the ls -l command, we can see that the changes have been applied.

change from particular owner
change from a particular owner

5. Change the group of a file by checking the existing owner first

We can also check the existing group to which a file belongs before applying any changes. We shall use the chown –from command. The command will first check whether the current group is the one specified in the command argument before applying any changes.

chown –from=[:current_group] [:new_group] [filename]

Change from a particular group
Change from a particular group

By running the ls -l command, we can see that the changes are applied successfully. We have changed the group from tuts to fosslinux_group0

6. Change the owner of a directory and its contents recursively

Assuming you have a directory with a directory containing many files, executing the chown command on every single file will be quite a time-consuming process. Luckily, Linux makes use of the -R (Recursive) command to traverse through all the files in a directory recursively. Syntax:

chown -R [new_owner] [/Directory]

chown with recursive command
chown with recursive command

The above command changes the owner of the FOSSLINUX directory and all its contents to tuts.

7. Copy the settings of a file and apply to another file

The chown command is useful in situations where you need to set the ownership and grouping similar to the settings of another file. Take a look at these two files, sample-file, and testOne.txt. By running the ls -l command, we see they have belonged to one owner but different groups.

ls -l command
ls -l command

Let’s use the chown command to apply the settings of example.txt to sample-file. Syntax:

copy settings of another file
copy settings of another file

By running the ls -l command, we see both files now belong to the same owner and group.

8. Suppress errors resulting from chown command

We use the -f argument to suppress any errors. For example, in the image, we execute a chown command which prints an error.

chown command output an error
Output an error

However, we can suppress this error using the -f parameter. It is illustrated in the image below.

using chown with -f parameter
using chown with -f parameter

9. Changing ownership and group of a symbolic link

Generally, when you change the ownership and group of a symbolic link, no changes should be applied. Instead, they are applied to the linked file. As an example, we first create a symbolic link to the file ‘sample-file.’

create symbolic link
create a symbolic link

By executing the ls -l command, we see the symbolic link belongs to the owner (tuts) and group (tuts).

ls -l command on the symbolic link

Let’s try changing the owner and group to root.

using chown on symbolic link
On the symbolic link

From the above output, we see the changes were applied to the sample file. It is the file linked to by link1.
To overcome this, we include the -h argument in our syntax.

using chown on symbolic link
Using chown on the symbolic link

Now, by running the ls -l, we can see the changes are applied to the symbolic link.

10. Display the details of the operation in output

When a chown command completes successfully, there should be no output about the operation carried out. However, in situations where a report of any operation carried out is necessary, we will need to use the -v (verbose) argument in our syntax. It’s illustrated below:

using chown and verbose command
using chown and verbose command

From the illustration above, we see that the output of the operation carried out was printed.

Conclusion

From the above article, it’s evident that the chown command is quite easy to work with. You can execute chown –help, which should give you a list of arguments that to use with this command. Also, the man page comes with all the necessary information about this useful command. Do you have a special usage scenario that you want to share with our readers? Please feel free to do so in the comment section below.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,437FansLike
374FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to change Hostname on Ubuntu

Generally, a hostname is just an assigned name to a computer, laptop, or any other connected device to a network. You can say a hostname is a computer's nickname, and it is used to identify the machine over the network uniquely.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

9 Useful Tips Working with Operating System using Python

There are several situations when we want to work with the operating system using python. We may want to see the user details or want to do some tasks with the files and directories. If you are a system administrator, you will find it useful to work with Operating System as one can easily automate some repeating tasks of the Operating System using python.

System76 Lemur Pro Review

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.

Top 10 New Features in Linux Kernel 5.8

Linus Torvalds recently announced the release of Linux Kernel 5.8, and he seems delighted with it. He has pointed it out as the most significant release of all time. To developers, this new kernel comes with an addition of 800,000 new code lines and more than 14,000 changed files. To the average user, you might not see many eye-candy changes, as seen in the earlier releases.

How to install Flutter on Linux

Do you want to install Flutter and start creating awesome Android and iOS applications? Then you have come to the right place. Here is a comprehensive tutorial on how to install Flutter on Linux. If you are new to Flutter, don't worry, we got that covered too as we shall go in-depth understanding of Flutter and what it brings to the table.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

Ubuntu 19.10 (Eoan Ermine) Beta Installation and Overview

It may be early, but I've been looking forward to the release of Ubuntu 19.10 for some time now. As an impatient person, and promised readers in the FOSSLinux article announcing the release of Eoan Ermine that we would provide you a review of Ubuntu 19.10 Eoan Ermine - Beta.  So, here it goes.

5 Best Ways to Free up Hard Disk Space in Ubuntu

Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

Beaker Browser: A P2P web browser you must try

I think we can all agree on the fact that the web browser is an integral part of our Linux systems, or any computer system, for that matter. We have had several fulfilling options for browsing the web, like Firefox, Brave, Vivaldi, Tor, etc. and they have worked pretty well, but also pretty much in the same way, except for Tor, which is much better for anonymity.