Table of Contents

CHOWN Examples

2016


CHOWN is the Linux CHange OWNer Command. This allows the owner of a file or directory to be changed between users.

Change the owner of a file

  # ls -lart tmpfile    [ls is used to diplay the file/directory properties]
  -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile
  
  # chown root tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile


Change the group of a file

  Through the chown command, the group (that a file belongs to) can also be changed.
  # ls -l tmpfile
  -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile
  
  # chown :friends tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile


Change both owner and the group

  # ls -l tmpfile
  -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile
  
  # chown himanshu:friends tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile


Change owner only if a file is owned by a particular user


Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.

  # ls -l tmpfile
  -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile
  
  # chown --from=guest himanshu tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile
  
  # chown --from=root himanshu tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile


Copy the owner/group settings from one file to another

  This is possible by using the ‘–reference’ flag.
  
  # ls -l file
  -rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file
  
  # ls -l tmpfile
  -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile
  
  # chown --reference=file tmpfile
  
  # ls -l tmpfile
  -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile


In the above example, we first checked the owner/group of the reference-file ‘file’ and then checked the owner/group of the target-file ‘tmpfile’. Both were different. Then we used the chown command with the ‘–reference’ option to apply the owner/group settings from the reference file to the target file. The command was successful and the owner/group settings of ‘tmpfile’ were made similar to the ‘file’

List all the changes made by the chown command

  Use the verbose option -v, which will display whether the ownership of the file was changed 
  or retained as shown below.
  
  # chown -v -R guest:friends linux
  changed ownership of `linux/redhat/rh7' to guest:friends
  changed ownership of `linux/redhat' retained to guest:friends
  ownership of `linux/redhat_sym' retained as guest:friends
  ownership of `linux/ubuntu_sym' retained as guest:friends
  changed ownership of `linux/linuxKernel' to guest:friends
  changed ownership of `linux/ubuntu/ub10' to guest:friends
  ownership of `linux/ubuntu' retained as guest:friends
  ownership of `linux' retained as guest:friends