How to Change Ownership of a file on Linux

File ownership is a way to manage files of different users and share files between users of same groups. Every file in linux is managed by a specific user and a group. The chmod command helps to define file permissions separately for user, group and others.

Using chown command you can change the ownership of a file and similarly using chgrp you can change the group owner for that file.

[chia]$ ls -l

-rwxrwxr-x 1 chia developers 1592 Jun 11 18:16 file1
-rwxrwxr-x 1 chia developers 2071 Jun 11 18:17 file2
-rwxrwxr-x 1 chia developers 1396 Jun 11 18:17 file3
-rwxrwxr-x 1 chia developers 9149 Jun 11 03:29 file4

In the case "chia" is the owner of the file and i have shared this directory with my fellow "developers". The developers have read write execute access.

Using Chown, change the ownership of a file

You can only transfer the ownership if you are the root user. Although it sounds fair for the owner of the file to be able to transfer the ownership but this doesn't happen because of two reasons:

  • Under shared systems, each user has a fixed quota (memory he can use). By changing the ownership of a file to another user, one is actually adding the file size to be counted under that user's quota, which might not be wanted by a user. As you can see, this may cause some serious trouble with large files.
  • The second reason, is the privilege escalation issue. Lets say, I have an executable file, I set the suid of the file and change its ownership to root. Now, I can run the executable as the super user, which is not right.

If you try to change the ownership of any arbitrary file owned by you, you will get the following error.

chown: changing ownership of `arbitrary_file': Operation not permitted

So, either be a root user or use sudo (for Debian/Ubuntu users).

[root]# chown shredder12 file1

In this case, I have transfered the ownership of file1 to user shredder12

[chia]$ ls -l

-rwxrwxr-x 1 shredder12 developers 1592 Jun 11 18:16 file1
-rwxrwxr-x 1 chia developers 2071 Jun 11 18:17 file2
-rwxrwxr-x 1 chia developers 1396 Jun 11 18:17 file3
-rwxrwxr-x 1 chia developers 9149 Jun 11 03:29 file4

Using Chown to change the Group ownership of a file

Now, here you don't have to be the root user to change the group. If you are the owner, you can change the group to another group of which you are a member. In the example below, I am changing the group to "somegroup" as user "chia". And it will only work if "chia" is a member of "somegroup" too.

[chia]$ chown :somegroup file1

[chia]$ ls -l

-rwxrwxr-x 1 chia somegroup 1592 Jun 11 18:16 file1

. . .

Syntax of Chown

If you have noticed, I have mentioned the group after a colon ":". This is how the chown syntax works.

[root]# chown [owner]:[group] filename

  • If only [owner] is mentioned as in the first example, then only the owner is changed, group remains as it is.
  • If both of are mentioned separated by a colon ":", [owner]:[group], without a space in between then both the user and group ownership are changed. Ofcourse, you will need superuser privileges to do so.
  • If only the username followed by a colon is mentioned, [owner]:, then the ownership of the file is changed to the new user and the group is changed to his login group.
  • If only the group is mentioned preceded by the colon, :[group], then only the group is changed. Here, chown works similar to chgrp(we will see it a while later).

Use -R flag to change the file ownership recursively

[root]# chown -R shredder12 directory_name

Using chgrp to change the group ownership of a file

Similar to chown, chgrp can be used to change the group ownership of the file. Once again you need to either be the root user or the owner of the file, only if you are the member of the new group too.

[chia]$ chgrp administrator file2

[chia]$ ls -l

. . . . .

-rwxrwxr-x 1 chia administrator 2071 Jun 11 18:17 file2

. . . . .

You can even mention the group by using another file as a reference. Say you want the file1 to have the group owner of file3, then use the following command

[chia]$ chgrp --reference=file3 file1

Just like chown, for recursively changing the group ownership, use the -R option.

[chia]$ chrgrp -R administrator some_directory

 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.