Open In App

How to Change File Ownership in Linux | chown Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 the owner of files and directories. 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 delve into the usage of the chown command.

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.

Each user has some properties associated with them, such as a user ID and a home directory. We can add users to a group to make the process of managing users easier. A group can have zero or more users. A specified user can be associated with a “default group”. It can also be a member of other groups on the system as well.

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. Unless the execution permission is given to directories changes do affect them.
  • 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.

Note: 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. 

Syntax of chown Command in Linux

The chown command in Linux has the following syntax:

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.

Options available in `chown` command 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. The following example demonstrates its usage:

Example: 

chown -c master file1.txt




This command notifies you when the ownership of `file1.txt` is changed, providing valuable feedback for tracking modifications.

chown -c master file1.txt

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. The following example illustrates its application:

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.

chown -v master file1.txt

chown -v master file1.txt

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.

Examples to Change File Ownership in Linux

1) How to File Ownership in Linux

To Change the owner of a file in Linux, you can use the following basic syntax:

chown owner_name file_name





For example:

chown master file1.txt




In this instance, the command designates the user “master” as the new owner of the file `file1.txt`. This is particularly useful when transferring ownership of files between users.

chown master file1.txt

chown master file1.txt

2) How to Change the Group of the File in Linux

To change the group ownership of a file, utilize the following 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, we use the following 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, we use this 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.

chown :group1 file1.txt

chown :group1 file1.txt

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

chown master:group1 greek1




Here, greek1 is a file. 

chown master:group1 greek1

chown master:group1 greek1

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,” use the following 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.

chown --from=master root greek1

chown –from=master root greek1

7) How to Change Group from a Particular Group

To change the group of a file (e.g., “group1” to “root”), use the following 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.

chown --from=:group1 root greek1

chown –from=:group1 root greek1

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”), use the following syntax:

chown --reference=greek1 greek2





This command copies the ownership details from “greek1” to “greek2,” ensuring consistency in ownership between the two files.

chown --reference=greek1 greek2

chown –reference=greek1 greek2

9) How to Change Owner of Multiple Files

For simultaneous changes in the owner and group of multiple files, employ the following syntax:

chown master:group greek2 greek3 




In this instance, 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

In this article we have discussed Linux user types, ownership, file permissions and examples of chown commands with options available in it. One must have a good understanding of `chown` command in Linux so he/she can manage the change in the file owner or groups, basically simplification in user management.



Last Updated : 02 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads