You are currently viewing Linux Commands – chown

Linux Commands – chown

Linux Commands – chown

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed chmod command in Linux which is used to modify a file or directory permissions.

https://cloudaffaire.com/linux-commands-chmod/

In this blog post, we will discuss chown command in Linux. chown command is used to change a file or directory owner or group or both. chown command changes the file/directory owner if only owner is provided or group if only group is provided. You can also change both owner and group at once using chmod new_owner:new_group file/directory command.

Note: A regular user cannot change a file or directory ownership by his own even if he is the owner of the file. You need to have root or sudo privileges to change a file or directory ownership. However, a regular user can change the group ownership of a file or directory provided he owns the file or directory and he is the member of the target group.

Linux Commands – chown:

You can use chown command to change a file or directory owner or group or both.

You can use chown -c or –changes options to verbosely describe the action for each FILE whose ownership actually changes.

You can use chown -f or –silent or –quiet options to suppress printing of error messages about files whose ownership cannot be changed.

You can use chown –from=OLD-OWNER option to change a file or directory ownership, provided the OLD-OWNER is the current owner of the file.

You can use chown –dereference option to not change the ownership of a symbolic link, instead follow the symbolic link and change the ownership of file or directory that the symbolic link refers to. This is the default behavior of chown and works the same without this option.

You can use chown -h or –no-dereference options to change the ownership of a symbolic link themselves instead of what they point to. This mode relies on the ‘lchown’ system call. On systems that do not provide the ‘lchown’ system call, ‘chown’ fails when a file specified on the command line is a symbolic link.

You can use chown –reference=REF_FILE option to change the user and group of each FILE to be the same as those of REF_FILE. If REF_FILE is a symbolic link, do not use the user and group of the symbolic link, but rather those of the file it refers to.

You can use chown -R or –recursive options to recursively change ownership of directories and their contents. By default, chown changes only the ownership of directories, not their content.

You can use chown -v or –verbose options to output a diagnostic for every file processed. If a symbolic link is encountered during a recursive traversal on a system without the ‘lchown’ system call, and ‘–no-dereference’ is in effect, then issue a diagnostic saying neither the symbolic link nor its referent is being changed.

You can use chown -H or -L or -P options with -R option to control the behavior of symbolic link in recursive mode if they refer to a directory.

  • -H : follow the symbolic link provided as a command-line argument, but do not follow any other symbolic link during traversing
  • -L : follow all symbolic links
  • -P : Do not follow any symbolic link

You can use chown –preserve-root option with -R option to prevent any attempt to recursively change the root directory (/). It’s a failsafe mechanism of OS to prevent recursively changing ownership in the root directory.

You can use chown –no-preserve-root option with -R option to cancel the effect of any preceding ‘–preserve-root’ option. This is a dangerous command and should not be executed. The below example is given just for the demo, do not execute.

Hope you have enjoyed this article. In the next blog post, we will discuss chgrp command in Linux.

 

Leave a Reply