Linux

Linux : how to change the owner of a file with the chown command?

I. Introduction

How do you change the owner of a file or folder under Linux? The chown command provides an answer to this question. As a system administrator, this is an operation you should be able to perform.

The chown command (short for "change owner") is an indispensable Linux command. It allows you to change the owner and group associated with one or more files or folders. In addition to the chown command, you should also be familiar with the chmod command, as it allows you to change the permissions on a file or folder. There's a real complementarity between these two commands. Together, they enable you to manage access permissions, and therefore control who has access to what.

II. The chown command syntax

Let's start with the basic syntax of the chown command.

chown [OPTIONS] owner[:group] file
  • [OPTIONS] one or more options for adapting the behavior of the chown command (enabling recursion, for example)
  • owner: the new user owner of the file specified in the
  • group: the new "owner" group associated with the file (optional)
  • file: the file or directory to be modified.

So, the presentation of the syntax leads me to tell you about the main options of the chown command:

  • -R (--recursive): apply changes recursively to all files and sub-folders under a root folder.
  • -v (--verbose): display changes made for each file (better visibility of actions performed).
  • --reference=: use a reference file to copy its properties (owner and group).
  • --help: display help and options available for the command.

The owner user and group associated with a file are visible when using the ls command. Here's an example.

Linux - The chown command

III. Examples of using chown

In the rest of this article, you'll learn how to use the chown command in a variety of situations.

A. How do I change the owner of a file?

To change the owner of a file called "it-connect.txt" and set it to the user "flo":

chown flo it-connect.txt

Note : adding"sudo" as a prefix may be necessary, depending on the user you're working with, the file you're working on and the desired operation. If you need to change the owner on a system file or a file that is not in your user's"/home/", it is necessary to use "sudo" (in most cases).

After executing this command, the user "flo" becomes the new owner of this file. You can check this with the following command:

ls -l it-connect.txt

The change is clearly visible. The chown command we've just run has modified the owner user, i.e. the value framed in orange in the image below.

To change the owner user and the group, the syntax is slightly different. The example below defines the user "flo" as owner, while associating the group "it-connect":

sudo chown flo:it-connect it-connect.txt

In the end, the ls command returns the following result:

-rw-rw-r-- 1 flo it-connect 0 janv. 24 09:29 it-connect.txt

B. How do I change the owner of a set of files?

If you wish to change the owner of all files in a directory, you need to use the "-R" option. This option allows you to recursively change the owner (and possibly the group). Here, the operation is performed recursively on the "/home/flo/data" directory.

sudo chown -R flo /home/flo/data

C. How do I change the group assigned to a file?

If you only want to change the group of a file, you can! But be careful, you need to use a very specific syntax. You need to specify a colon (:) followed by the group name, while leaving the first part corresponding to the owner user blank.

Here's an example:

sudo chown :it-connect demo.txt

This command only modifies the group associated with the "demo.txt" file, without affecting the current owner.

To recursively change the group on several files :

sudo chown -R :it-connect /home/flo/data

Following execution of this command, all files and subdirectories located under the "/home/flo/data" root will have "it-connect" as an associated group.

D. How do I use a reference file with chown?

The chown command lets you use an existing file as a reference to copy its owner and group properties. In a way, it's a way of duplicating this information.

sudo chown --reference=demo.txt fichier1.txt

In this example, the file named "fichier1.txt" will adopt the same properties as "demo.txt".

Linux - Command chown - Example with a reference file

E. The verbose mode of the chown command

The -v option allows you to view the changes made by the command, in what is known as verbose mode.

sudo chown -v flo it-connect.txt

In fact, we can mix the different options. This one, for example, uses a reference file. The result of the command contains a sentence to explain the change made.

sudo chown -v --reference=demo.txt fichier2.txt 
Ownership of 'fichier2.txt' changed from root:it-connect to flo:flo

This feature is particularly useful for tracking changes made by scripts.

F. Check owner and group before modification

The --from option is used to check that the file belongs to a specific user and group before applying a modification. If this is not the case, the operation is cancelled. The syntax is as follows:

sudo chown --from=[UtilisateurActuel]:[GroupeActuel] [NouvelUtilisateur]:[NouveauGroupe] fichier

For example, to check that the file named "fichier3.txt" belongs to "flo" and to the "it-connect" group, and then change the owner to "flo" and the group to "flo", we would use this:

sudo chown -v --from=flo:it-connect flo:flo fichier3.txt

Specifying the "-v" option activates verbose mode to obtain the test result. Here, the test fails, so the chown command makes no changes. In the end, the result returned is as follows:

The ownership of 'fichier3.txt' is retained as root:it-connect

IV. Conclusion

After reading this tutorial, you'll have all the information you need to get to grips with the chown command on Linux. It's simply indispensable for administering a Linux server, whether it's running Debian, Ubuntu, AlmaLinux or another distribution.

author avatar
Florian Burnel Co-founder of IT-Connect
Systems and network engineer, co-founder of IT-Connect and Microsoft MVP "Cloud and Datacenter Management". I'd like to share my experience and discoveries through my articles. I'm a generalist with a particular interest in Microsoft solutions and scripting. Enjoy your reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.