How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The chown
command, short for "change owner," is a powerful tool that allows users to change owner of file in Linux. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific resources. In this article, we will explore the fundamentals of file ownership in Linux and also go into the usage of the Linux chown command.
Syntax of chown Command in Linux
The chown command is used to modify ownerships and permissions. The basic chown syntax is:
chown [options] new_owner[:new_group] file(s)
Here's a breakdown of the components:
`chown`
: The base command.`options`
: Optional flags that modify the behavior of the `chown`
command.`new_owner[:new_group]`
: The new owner and optionally the new group. If `new_group`
is omitted, only the owner is changed.`file(s)`
: The file or files for which ownership is to be changed.
Understanding User Ownership and Permissions in Linux
Different users in the operating system have ownership and permission to ensure that the files are secure and put restrictions on who can modify the contents of the files. In Linux, different users use the system:
- Root User: It is a superuser who has access to all the directories and files in our system, and it can perform any operation. An important thing to note is that only the root user can perform changing of permissions or ownerships of the files that are not owned by them.
- Regular User: These users have limited access to files and directories and can only modify the files that they own.
Ownership and Permissions
To protect and secure files and directories in Linux we use permissions to control what a user can do with a file or directory. Linux uses three types of permissions:
- Read: This permission allows the user to read files in directories, it lets the user read directories and subdirectories stored in it.
- Write: This permission allows a user to modify and delete a file. Also, it allows a user to modify its contents (create, delete, and rename files in it) for the directories.
- Execute This permission on a file allows it to get executed. For example, if we have a file named php.sh unless we don't give it execute permission it won't run.
Types of file Permissions in Chown Command in Linux:
There are three types of file permission in Chown Command in Linux discussed below.
- User: This type of file permission affects the owner of the file.
- Group: This type of file permission affects the group that owns the file. Instead of the group permissions, the user permissions will apply if the owner user is in this group.
- Other: These types of file permissions affect all other users on the system.
To view the permissions we use:
ls -l
chown command is used to change the file Owner or group. Whenever you want to change ownership, you can use the chown command.
Options available in `chown` command in Linux
Here are common options for change owner of file in Linux:
1) Using `-c` Option in `chown` to Change File Ownership
The `-c`
option in the `chown`
command is utilized to report when a file change is made. This option is beneficial when you want to receive notifications about ownership alterations.
Example:
chown -c master file1.txt
This command notifies you when the ownership of `file1.txt`
is changed, providing valuable feedback for tracking modifications.

2) `Using `-v` Option in `chown` to Change File Ownership
The `-v`
option enhances the verbosity of the `chown`
command by showing detailed information for every processed file. This is particularly useful when you want a comprehensive log of ownership changes.
Example:
chown -v master file1.txt
By using this command, you get a verbose output, displaying information about each file processed during the ownership change.

3) `-f` Option in `chown` to File Ownership in Linux.
The `-f`
option in the chown
command serves to suppress most error messages and forcefully or silently change ownership, even when not permitted. This option is handy when you want to override restrictions without being interrupted by error notifications. Here's an example:
chown -f master file1.txt
In this case, the command attempts to change ownership, and any error messages are suppressed, allowing for a more seamless execution.
Additional Useful Options in chown
Here are more options
when the user managing file ownership across different files, directories, and symbolic links.
-h
: Modifies ownership of a symbolic link instead of its referenced file.--reference=FILE
: Uses another file as a reference to copy ownership.--from=OWNER:GROUP
: Changes ownership only if file’s current ownership matches given values.-R
: Recursively change ownership of all files and subdirectories.-H
,-L
,-P
: Control how symbolic links are handled during recursive changes.
Examples to Change File Ownership in Linux
Here are some examples of how we change the file ownership:
1) How to File Ownership in Linux
To Change the owner of a file in Linux.
Syntax
chown owner_name file_name
For example:
chown master file1.txt
This designates the user "master" as the new owner of file1.txt
.
.webp)
2) How to Change the Group of the File in Linux
To change the group ownership of a file.
Syntax
chown :group1 file1.txt
In this scenario, the group "group1" is assigned as the new group for the file `file1.txt`
. This operation is handy for managing access permissions within specific groups.
3) How to Change Owner and Group of the File in Linux
For simultaneous change in both of the owner and group of a file.
Syntax
chown master:group1 file1.txt
In this use case, the user "master" assumes ownership, and the group "group1" is assigned as the new group for the file file1.txt
. This can be beneficial when restructuring file access hierarchies.
4) How To Change Group Ownership
When the goal is to change only the group ownership of a file.
Syntax
chown :group1 file1.txt
This command exclusively alters the group ownership of file1.txt
to "group1" from its previous state. It proves useful in scenarios where group permissions need to be modified independently.
.webp)
You can see that the group permissions changed to group1 from root, if you use -v option it will report that. We just need to add a ":" to change group.
5) How to Change Owner as well as Group
Again, taking master as user and group1 as a group in the system.
Syntax
chown master:group1 greek1
Here, greek1 is a file.
.webp)
6) How to Change Owner from a Particular Ownership Only
To change ownership from a specific user (e.g., "master") to another (e.g., "root"), where the current owner must be "master".
Syntax
chown --from=master root greek1
This command ensures that ownership is changed from "master" to "root" only when the current owner is "master." It adds an additional layer of control to ownership modifications.
.webp)
7) How to Change Group from a Particular Group
To change the group of a file (e.g., "group1" to "root").
Syntax
chown --from=:group1 root greek1
This command specifically changes the group of greek1
from "group1" to "root." It is useful when refining group associations.
.webp)
Here, the group of greek1 is changed to root.
8) How to Copy Ownership of One File to Another
To duplicate the ownership of one file (e.g., "greek1") onto another file (e.g., "greek2").
Syntax
chown --reference=greek1 greek2
This command copies the ownership details from "greek1" to "greek2," ensuring consistency in ownership between the two files.
.webp)
9) How to Change Owner of Multiple Files
For simultaneous changes in the owner and group of multiple files.
Syntax
chown master:group greek2 greek3
In this example, both "greek2" and "greek3" will have their owner set to "master" and their group set to "group." This is useful for batch ownership modifications, streamlining the process for multiple files at once.
Conclusion
Whether you need to change ownership of file in Linux for single files or entire directories, the linux chown command provides flexible control over file ownership in unix-like systems. Understanding and managing file ownership in Linux is keeping your system clean, secure, and properly permissioned. The linux chown command is useful for system administrators and users with administrative access to establish, modify, or correct file and directory ownership.