5 chown command examples to change file owner and group

5 chown command examples changes the user and/or group ownership of each given file/directory. If only an owner is given, that user is made the owner of each given file/directory, and the file’s group is not changed. If the owner is followed by a colon (:) and a group name, with no spaces between them, the group ownership of the files/directory 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 omit‐ted, 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.

5 chown command examples

Syntax: chown UserName:GroupName FileName

Before running chown command on Files / Directories verify User and group is exist in Linux machine or not by reading below files. If user or group does not exists your chown command won’t execute properly

[root@ArkitServer chown]# cat /etc/passwd |grep ravikumar
ravikumar:x:1000:1000:Ravi Kumar:/home/ravikumar:/bin/bash

[root@ArkitServer chown]# cat /etc/group |grep ravikumar
ravikumar:x:1000:

Change User-Owner and Group-Owner for File and Directory

[root@ArkitServer chown]# chown ravikumar:ravikumar Arkit2.txt

Using above command User-Owner and Group-Owner has changed from root:root to ravikumar:ravikumar

To get an confirmation when you change File / Directory Ownership, use option -c will give you information whether it is newly changed or old one

[root@ArkitServer chown]# chown -c ravikumar:root Arkit1.txt
changed ownership of ‘Arkit1.txt’ from root:root to ravikumar:root
[root@ArkitServer chown]# chown -c ravikumar:root Arkit1.txt

chown -c changed log

User Ownership Change without changing group ownership

Without group name if you provide only user name it changes user ownership directly 

chown root Arkit2.txt

Group Ownership change without touching User-owner

This step is simply possible with chown command to change only group ownership without changing user ownership. Now provide group-owner name after (semicolon)

chown :ravikumar Arkit5.txt

Recursively change Ownership for Files / Directories

What does mean by recursively, within directory there are multiple files and sub directories. If you would like to change all of the files / directories permissions simple apply recursive option -R which will change all of the permissions in single go

chown -R ravikumar:ravikumar /chown/
chown -R command recursive Update

chown -R recursive Update

Take Reference of file and apply same ownership to destination file

It’s so simple applying same ownership from source file to destination file using below example

chown --reference=anaconda-ks.cfg /chown/Arkit10.txt

 

Symbolic Link permissions change

chown -h affect symbolic links instead of any referenced file

Different option you can use along with 5 chown command examples

  • –dereference affect the referent of each symbolic link (this is the default), rather than the symbolic link itself
  • –from=CURRENT_OWNER:CURRENT_GROUP
  • –no-preserve-root do not treat ‘/’ specially (the default)
  • –preserve-root fail to operate recursively on ‘/’
  • -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

Related Articles

chmod command to grant file permissions

Stickybit

25 Mostly used Linux commands 

Thanks for your wonderful Support and Encouragement