Open In App
Related Articles

chown command in Linux with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

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, there are different users who 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 file 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 so unless we don’t give it execute permission it won’t run.

Types of file Permissions:  

  • User: This type of file permission affects the owner of the file.
  • Group: This type of file permission affects the group which 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 permission 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 chown command. 

Syntax:  

chown [OPTION]… [OWNER][:GROUP] FILE…
chown [OPTION]… –reference=RFILE FILE…

Some examples of `chown`commands in Linux

1) To change the Owner of the file:

Syntax: 

chown owner_name file_name

Now if I use file1.txt in my case, to change ownership I will use the following syntax: 

chown master file1.txt
chown master file1.txt

chown master file1.txt

In this example, the user master is assigned as the owner of file.1.txt

2) To change the group of the file:

In this example, the group1 group is assigned as the group of file1.txt

Syntax:

chown :group1 file1.txt

3) To change both owner and group of the file:

In this example, the user master is assigned as the owner and the group1 group is assigned as the group of file1.txt.

Syntax:

chown master:group1 file1.txt

Options available in `chown` command in Linux

1) `-c` Option in `chown` command in Linux.

Reports when a file change is made. 
Example: 

chown -c master file1.txt

chown -c master file1.txt

2) `-v` Option in `chown` command in Linux.

It is used to show verbose information for every file processed. 

Example:

chown -v master file1.txt
chown -v master file1.txt

chown -v master file1.txt

3) `-f` Option in `chown` command in Linux. 

It suppresses most of the error messages. When you are not permitted to change group permissions and shows error, this option forcefully/silently changes the ownership.

Examples:  

  • To Change group ownership in our case I am using group1 as a group in the system. To change ownership, we will use.
chown :group1 file1.txt
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.

  • To change the 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

  • To change the owner from particular ownership only: Suppose we want to change ownership from master to root where current owner must be master only.
chown --from=master root greek1
chown --from=master root greek1

chown –from=master root greek1

  • To change group from a particular group:
chown --from=:group1 root greek1
chown --from=:group1 root greek1

chown –from=:group1 root greek1

Here, the group of greek1 is changed to root.

  • To copy ownership of one file to another:
chown --reference=greek1 greek2
chown --reference=greek1 greek2

chown –reference=greek1 greek2

  • To change ownership of multiple files:
chown master:group greek2 greek3 

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials