Open In App

How to Change File Ownership in Linux | chown Command

Last Updated : 02 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user pa
7 min read
How to Change the Directory in Linux | cd Command
The Linux Command Line or Linux Terminal is a super useful tool that lets people talk to the computer. One big part of using this Linux Terminal Tool is moving around the computer's folders and files, which we call the file system. CD Command in Linux is the most important aspect of doing this. It helps you go from one folder to another. All of you
6 min read
file command in Linux with examples
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it. It has three sets of tests as follows: filesystem test: This test is based on the result which returns from a stat system cal
3 min read
How to Save Output of Command in a File in Linux?
When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command. Using Redirections in Linux: We can use the redirections operators to save the output of commands into files. Redirection operators redirect the
3 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
7 min read
Extract Emails From a Text File Using Grep Command in Linux
Let's consider we have a text file that contains lots of text and in that text file there are some email IDs present, and we have to find the all email IDs present in that text file. So what we can do? How can we find all email IDs present in that text file?. One way to find all email IDs manually, but this is a very time-consuming and boring proce
3 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting out or you're already good at working with files, t
8 min read