chown Command

chown is an abbreviation for “change owner”, which is pretty self-explanatory. It change the ownership of file. You may have notice in chmod command that it change the read, write and execute permission not the ownership of file. So, to change the ownership of file we will use the chown command. Later on, we will discuss about changing the ownership of file with example. Before that, lets see the basic structure of this command.

In its structure,
1. sudo allows you to access the file by entering a password.
2. chown command
3. The username of the new file owner, which is represented as user, user:, user:group, or :group. See chart below.
4. Path to the file.

It’s important to note that chown generally requires sudo/root permissions. Owning the file alone is not enough to be able to change the owner.

The basic format for [new owner] is user:group. There are 4 common ways to use this format:

Format for New OwnerExplanation
userChanges only the user that owns the file.
user:Changes the user that owns the file and changes the group to that user’s group.
user:groupChanges both the user and group that own the file.
:groupChanges only the group that owns the file.

Let’s use our “learningnotes.txt” file to walk through a few examples.

To change the user that owns “learningnotes.txt” to “someotheruser” but leave the group unchanged, we’ll use “sudo chown someotheruser learningnotes.txt”:

:~$ ls -l
-rw-rw-rw- 1 cooluser cooluser 0 Jun 7 20:56 learningnotes.txt
:~$ sudo chown someotheruser learningnotes.txt
:~$ ls-l
-rw-rw-r– 1 someotheruser cooluser 0 Jun 7 20:56 learningnotes.txt

We can see the permissions and group stayed the same, but the user that owns the file changed.

To change the group to “othergroup” we can use “sudo chown :othergroup learningnotes.txt”:

:~$ ls -l
-rw-rw-rw- 1 someotheruser cooluser 0 Jun 7 20:56 learningnotes.txt
:~$ sudo chown :othergroup learningnotes.txt
:~$ ls-l
-rw-rw-rw- 1 someotheruser othergroup 0 Jun 7 20:56 learningnotes.txt

Here, we can see group ownership changed.

If you are wondering how many groups are in there in your system. You can view inside /etc/group file. In this file, you will encounter line like this ‘scanner:x:128:saned’ . This line can be interpreted in given below format.

 group_name:password:GID:comma-separated-list-of-users

Hope, this blog helps you to insight the basic understanding of chown. This is a really powerful command which is used in our professional career. If you have any question, you are free to reach through me a comment.

Leave a Comment

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