The Wayback Machine - https://web.archive.org/web/20241122173903/https://www.geeksforgeeks.org/chown-command-in-linux-with-examples/
Open In App

How to Change File Ownership in Linux | chown Command

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

How to Change File Ownership in Linux | chown Command – FAQs

How to Change Ownership of a File in Linux?

To change the ownership of a file or directory in Linux, you use the chown command. The basic syntax is:

chown [new_owner]:[new_group] file_or_directory
  • new_owner: The username of the new owner you want to set.
  • new_group: The group name you want to assign to the file or directory. This is optional; if you only provide an owner, the group will not be changed unless you follow the owner’s name with a colon.

For example, to change the owner of file.txt to the user john, you would use:

chown john file.txt

To change both the owner and group of file.txt to john and admin respectively:

chown john:admin file.txt

How Do I Change My File Ownership?

To change your file ownership to another user, assuming you have the necessary permissions (usually as the root user or via sudo privileges), use the chown command as described above.

Which Command is Used to Change File Ownership?

The chown command is used to change file ownership. It can change the ownership of both files and directories.

What is chown in Linux Command?

chown stands for “change owner” and is used in Unix-like operating systems to change the ownership of files and directories. It can modify the owner and the group associated with files or directories.

What is the Meaning of 777 in Unix Permissions?

In Unix and Unix-like operating systems, 777 is a permission setting that grants all rights to the user, group, and others. Permissions in Unix are represented by three numbers:

  • The first digit represents the user’s permissions.
  • The second digit represents the permissions for the group.
  • The third digit represents the permissions for others (anyone else).

Each digit is a sum of values:

  • 4 stands for “read”,
  • 2 stands for “write”,
  • 1 stands for “execute”.

So, 777 means:

  • Read, write, and execute permissions are granted to the user, group, and others.
  • This setting allows anyone to do anything with the file, which can be useful but poses a security risk.

Here’s how you might typically use chmod (change mode) to set this permission:

chmod 777 file.txt

This command gives everyone full access to file.txt.



Previous Article
Next Article

Similar Reads

chown - Permission Denied
In Linux, file permissions and ownership are critical to security and performance. The chown command means "change ownership" and is important for managing this. It allows users to change ownership of files and directories. However, encountering a "permission denied" error when using chown is a common problem, especially for Linux beginners. What i
7 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
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 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
8 min read
How to Change the Directory in Linux | cd Command
Mastering the cd command in Linux is a fundamental skill for anyone working in the Linux environment. The cd command allows you to change directories in Linux, making it easier to navigate through the file system and manage your files efficiently. Whether you're a beginner or an experienced user, understanding how to use the cd command in Linux is
6 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 Mount File System in Linux | mount Command
All files in a Linux filesystem are arranged in form of a big tree rooted at '/'.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at '/', others can be mounted manually using GUI interface(if available) or using mount command. mount command is used
6 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different places, `mv` is the go-to friend for the job. It's
7 min read
Unmount File System in Linux Using the umount Command
Unmounting a file system is a vital procedure for securely deleting storage devices or rendering system resources accessible. A vital instrument in this procedure is the "umount" command, which enables users to softly unmount mounted devices or partitions. Before beginning the unmounting process, this step ensures data integrity and protects any po
7 min read
How to Display Path of an Executable File in Linux | Which Command
In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In this article, we are going to discuss on what is whi
6 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This article explores the prowess of the find command, sheddi
9 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 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
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Line or Terminal. Table of Content Using the 'touch'
6 min read
file command in Linux with examples
The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable format (e.g., ASCII text) or as a MIME type (e.g.,
3 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool; it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's delve into the details of some frequently used cat commands, understanding each example along the way. Table of Content Basic Syntax o
7 min read
How to Save Output of Command in a File in Linux?
Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpful. When we run a command on a terminal on Linux i
4 min read
Extract Emails From a Text File Using Grep Command in Linux
When dealing with large text files containing various information, it's often necessary to extract specific data such as email addresses. While manual extraction is possible, it can be time-consuming and error-prone. This is where the powerful grep command in Linux comes to our rescue. In this article, we'll explore how to use grep to efficiently e
4 min read
Check If File Exist Inside Tar File Using Tar And Grep Command
Linux users are familiar with the popular command-line tool Tar (tape archive), which is used to archive directories and files into a single file called a "tarball." Occasionally, you might need to verify whether a certain file is present in a tar archive without extracting the full archive. This detailed article will show you how to effectively se
4 min read
How to Change the Number of Open File Limit in Linux?
If you are an active Linux user, who has to work with many files on Linux at a time then you might have definitely faced a problem regarding “Too many open files” on a Linux system. When you have reached the maximum open file limit you will get an error message displaying "Too many open files (24)” error on your screen. Why do we face such a thing?
4 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
3 min read
How to change time zone from command line on RHEL 9
Correctly setting the time zone on a Red Hat Enterprise Linux (RHEL) 9 system is crucial for ensuring accurate time synchronization and proper functioning of various applications and services. While graphical tools are available for configuring the time zone, administrators often prefer command-line methods for efficiency and automation. Here, we w
4 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
three90RightbarBannerImg