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
Here 'delta
' is the owner, and group respectively to which '.bashrc
' file belongs to.
Changing ownership to another user
-
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).
-
List the file information using the command ls -l fileName
.
-
To change file ownership, run chown <userName|userID> fileName
.
-
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.
Changing Group ownership, and using chown
options
- 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
-
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.
-
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
-
chown
with -c
flag ( report any changes if made )
-
chown
with -v
flag ( report all changes, and errors ), and --reference
flag ( use referred file ownerships for all files passed as arguments )
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
.