In Linux, there may be times when you might want to change the owner and group-related information for a file or directory. If you are a command-line newbie and want to know how you can make such changes through the command line, you’ll be glad to know that there exists a command – dubbed chown – that lets you do this.

Before we start with the chown command tutorial, it’s worth mentioning that all examples and instructions mentioned here have been tested on Ubuntu 22.04 LTS and Debian 11.

Linux chown command explained

As already mentioned in the beginning, the chown command lets you change the file owner and group through the command line. Following is the command’s generic syntax:

chown [OPTION]... [OWNER][:[GROUP]] FILE...

Here’s what the tool’s man page says about it:

If only  an  owner  (a user  name or numeric user ID) is given, that user is made the owner of each

given file, and the files' group is not changed.  If the owner  is followed  by  a  colon  and a

group name (or numeric group ID), with no spaces between them, the group ownership of the  files  is

changed  as well.  If a colon but no group name follows the user name, that user is made the owner

of the files and the group of the files  is  changed  to that  user's  login  group. If the colon

and group are given, but the owner is omitted, only the group of the files is changed; in this case,

chown  performs  the same function as chgrp.  If only a colon is given, or if the entire operand is

empty, neither the owner nor the  group  is changed.

The following Q&A-type examples will give you a good idea about how the chown command works:

Q1. How to change the owner of a file?

Consider the following example:

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-fileowner-group.png63779bf33b288.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="35" loading="lazy" src="data:image/svg xml,” width=”471″>

Here, the owner of the file is ‘himanshu’ and the group it belongs to is also ‘himanshu’. Now, to change the owner to, say ‘root,’ use the following command:

chown root file1

The following screenshot confirms that the owner has now been changed to ‘root’.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-user-changed.png63779bf3777ac.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="36" loading="lazy" src="data:image/svg xml,” width=”434″>

Q2. How to change the group of a file?

Changing a group is similar to changing the owner. The only difference is in the syntax of the command, which is as follows:

chown :[group-name] [file-name]

So suppose the requirement is to change the group of ‘file1’ to ‘root’. Then the command would be:

chown :root file1

The following screenshot shows the group was successfully changed from ‘himanshu’ to ‘root’.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-group-changed.png63779bf3b82b8.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="87" loading="lazy" src="data:image/svg xml,” width=”443″>

Note: In case you’re wondering why the ‘sudo’ command was used, or in case you’re new to ‘sudo’, you should first go through our tutorial on this tool.

Q3. How to change both the owner and group of a file?

To change both the owner and group of file, use the following syntax:

chown [new-owner]:[new-group] [file-name]

So in our case, to change the existing owner and group from ‘root’ to ‘himanshu’, we’ll use the following command:

chown himanshu:himanshu file1

The following screenshot shows the above command in action:

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-change-owner-group.png63779bf40c60f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="79" loading="lazy" src="data:image/svg xml,” width=”500″>

Q4. How to change the owner or group (or both) after checking existing owner/group?

There might be situations where-in you may want to first cross-check existing owner/group of a file before making any change. So for those cases, you can use the –from command line option. This option requires you to mention the owner/group name that you want to verify.

chown --from=[curr-own]:[curr-group] [new-owner]:[new-group] [filename]

For example:

chown --from=root:himanshu himanshu:root file1

The above command will check whether the existing owner is ‘root’ and group is ‘himanshu’. If yes, then owner will be changed to ‘himanshu’ and group will become ‘root’.

Q5. How to pick owner/group information from a reference file?

There might also be situations where-in you may want chown to pick up owner and group-related information from a file, instead of manually entering it on the command line. For those cases, you can use the –reference command-line option. This option requires you to enter the name of the reference file.

chown --reference=[ref-file-name] [filename]

For example:

chown --reference=file2 file1

So the above command will copy the owner and group information from file2 to file1.

Q6. How to make chown operate on files and directories recursively?

To make the chown command recursively operate on files and directories, use the -R command-line option.

chown -R [new-owner]:[new-group] [directory-name-or-path]

For those who aren’t aware, recursive means the operation will be performed for all files in the given directory, as well as for files and directories within all sub-directories.

Q7. How to make chown suppress error messages?

There may be times when the chown command you run gives an error. For example, the following command when executed on my system:

chown --from=himanshu:himanshu himanshu:root file4

gave the following error:

chown: cannot access 'file4': No such file or directory

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-error-message.png63779bf44c69c.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="26" loading="lazy" src="data:image/svg xml,” width=”500″>

Now suppose the requirement is that the tool shouldn’t display such errors. Then this can be made possible using the -f command line option.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-supress-error.png63779bf48c8ec.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="37" loading="lazy" src="data:image/svg xml,” width=”500″>

The aforementioned screenshot confirms that the -f command-line option suppresses errors/warnings.

Q8. How to change group ownership to login group of the specified user?

This can be done using the [user/owner]: syntax. For example, the following command:

chown himanshu: file1

Will make sure the ownership is given to ‘himanshu’ and the group gets changed to the login group of ‘himanshu’. Note that this is different from the case when colon (:) isn’t used, as in that case (explained in Q1 above), the group remains unchanged.

Another thing worth mentioning here is that if you just use a colon (:) without specifying an owner or group, then no change will take place. For example:

chown : file1

This command will have no effect on either user or group ownership of the file.

By default, if you try changing the user and group ownership of a symbolic link, there will be no change. Instead, the file it links to will get these changes.

For example, the screenshot below shows I created a symbolic link ‘link1’ whose user and group ownerships are set to ‘himanshu’. Then I executed the chown command to change user and group to ‘root’. But the command had no effect on the symbolic link file – instead, it’s ‘file1’ (to which symbolic link points) whose user and group ownerships that got changed.

<img alt="Chown command symlinks" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/chown-sym-links.png63779bf4cb342.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="164" loading="lazy" src="data:image/svg xml,” width=”500″>

However, if you want, you can override this default behavior using the -h option.

Q10. How to change the owner and group for a directory?

Just like the way you do it for files. Following is an example:

chown root:root ./test-dir/

Note that you can cross verify the owner and group change for a directory using the stat command – the UID and GID fields in the output display user and group names.

For example in my case, the output clearly showed the changed user group ownership.

  File: test-dir

  Size: 4096          Blocks: 8          IO Block: 4096   directory

Device: 808h/2056d    Inode: 11928001    Links: 2

Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-08-07 10:31:04.867467573 0530

Modify: 2018-08-07 10:30:53.651807123 0530

Change: 2018-08-07 10:32:49.644427930 0530

 Birth: -

Note that if you are making recursive changes (refer to the Q&A discussing the -R option), and are dealing with symbolic links, you have the following options at your disposal:

       The following options modify how a hierarchy is traversed when  the  -R

       option  is  also  specified.   If  more than one is specified, only the

       final one takes effect.

       -H     if a command line argument is a symbolic link  to  a  directory,

              traverse it

       -L     traverse every symbolic link to a directory encountered

       -P     do not traverse any symbolic links (default)

Q11. Can UID and GID be used instead of user and group names?

Yes, you can use user ID and group ID instead of names. The syntax of the command remains the same though.

For example:

chown 1000:1000 file1

The above command will change the user and group ownership to the user with UID 1000 and group with GID 1000.

Q12. How to make chown display details of the operation in output?

If you want the chown command to display details of the operation it performs, use the -v command-line option.

For example, this command:

sudo chown howtoforge:howtoforge link1 -v

produces the following output:

changed ownership of 'link1' from root:root to howtoforge:howtoforge

Now, there’s another option -c that also acts like -v, just that it doesn’t display any details when nothing is changed.

Conclusion

The chown command, as most of you’d likely agree, isn’t difficult to use. What’s even better is that the tool’s man page contains a lot of details that might be of help to users (especially newbies). Try out the examples we’ve explained here, and for the rest of the features/options, go through the chown man page. to change the directory, use the cd command. In case of any doubt or query, leave a comment below.