Signup/Sign In
PUBLISHED ON: AUGUST 17, 2021

How to change File Ownership (chown)

Linux is a multi-user operating system, and sometimes we have files in common folders that all users can access. To maintain security, as well as to handle the data access, Linux uses ownership. In the same manner, an object owned by you can not be accessed by anyone else, unless someone higher up gives permission, Linux ensures your file is accessed only by the specified users, or groups, using the chown (change owners) command. With some experience chown can be used to set even the group of the file.

chown Syntax

chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

Common Options used with chown

Option

Description

-c, --changes

like a verbose but report only when a change is made

-f, --silent, --quiet

suppress most error messages

-v, --verbose

output a diagnostic for every file processed

--reference=RFILE

Refers to RFILE ownerships, and applies them

-R, --recursive

Recursively apply the permissions to files and directories

To make any edits to our files and observe the changes, let's see the current owner of the file.

File owner details:

ls -l fileName

List File Ownership

Here 'delta' is the owner, and group respectively to which '.bashrc' file belongs to.

Changing ownership to another user

  1. Make a new user, using the command adduser userName, using sudo privileges either by prefixing the command with sudo, or by starting an elevated shell, using the command sudo -s, to log in, and start an interactive shell (an interactive shell has # at the end of the prompt, instead of a $ usually).

  2. List the file information using the command ls -l fileName.

  3. To change file ownership, run chown <userName|userID> fileName.

  4. To see if the change has been made, we can again run ls -l fileName, or we could use the -c, or -v flag with our chown command depending on the verbosity desired, and use case.chown example

Changing Group ownership, and using chown options

  1. We have seen how to edit the owner user of our file. A few special cases for group ownership, using the chown command, are as follows
    1. We can change our group using the command in the format as follows

      chown [OPTION]... :[GROUP] FILE...

      And this will change only the group of your specified file.

    2. To change both User, and Group ownership simultaneously

      chown [OPTION]... [USER]: FILE... # When Group is to be made login group of User
      chown [OPTION]... [USER]:[GROUP] FILE... # When User, and Group are to be explicitly changed

      Changing User and Group both

  2. chown with -c flag ( report any changes if made )chown -c example

  3. chown with -v flag ( report all changes, and errors ), and --reference flag ( use referred file ownerships for all files passed as arguments )chown -v, and --ref example

Conclusion

We have seen how chown can be used in the terminal, for manipulating ownerships and ensuring files' security, on *nix-based operating systems. We have also covered suitable daily need options, with examples aiding in the use of the command.

chown can also be used to manipulate the group ownerships, and as specified in the previous section in 1.1, when used like that, chown behaves like chgrp.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.