WebMasterCampus
WEB DEVELOPER Resources

Linux chown Command

Learn Linux chown Command with examples


Linux chown Comman

In Linux we can use “chown” command to change the user and/or group ownership of a given file, directory, or symbolic link.

In Linux every user has a unique name and user ID. If only one user is available in the system, the user will be the owner of each file.

Users in Linux can be listed in different groups. The group allows us to set permission on the group level instead of setting permission on an individual level.

chown command Syntax

>> chown [OPTIONS] USER[:GROUP] FILE(s) 
  • USER is the user name or the user ID (UID) of the new owner.
  • GROUP is the name of the new group or the group ID (GID).
  • FILE(s) is the name of one or more files, directories or links.
  • Numeric IDs should be prefixed with the + symbol.
Option Description
USER If only the user is specified; the specified user will become the owner of the given files; the group ownership is not changed.
USER: When the username is followed by a colon : and the group name is not given; the user will become the owner of the files; and the files group ownership is changed to user’s login group.
USER:GROUP If both the user and the group are specified (with no space betwen them); the user ownership of the files is changed to the given user and the group ownership is changed to the given group.
:GROUP If the User is omitted and the group is prefixed with a colon : only the group ownership of the files is changed to the given group.
: If only a colon : is given; without specifying the user and the group; no change is made.

Successful chown Command doesn’t produce any output and returns zero.

Display the UID, GID, and Groups

The following command will list all the existing groups from your Linux system.

>> groups

To list the UID and GID, execute the id command as follows:

>> id

Display the User and Group Ownership of a File

To display the user and group owner of a file, execute the “ls -l” command with a specific file name.

>> ls -l Demo.txt 

chwon Command to Change the Owner of a File

Following chown command will change fruits.txt file owner to a new owner named webmastercampus.

>> chown webmastercampus fruits.txt

chwon Command to Change the Owner of Multiple Files

In Linux to change the ownership of multiple files or directories, specify them as a space-separated list.

Following chown command will change fruits_1.txt fruits_2.txt file owner to a new owner named webmastercampus.

>> chown webmastercampus fruits_1.txt fruits_2.txt

chwon Command to Change the Owner using numeric user ID

The numeric user ID (UID) can be used instead of the username.

The following chown example change the ownership of a file named fruits_2 to a new owner with UID of 1000:

>> chown 1000 fruits_2.txt

chwon Command to Change the Owner and Group of a File

To change both the owner and the group of a file use the chown command followed by the new owner and group separated by a colon (:) with no intervening spaces and the target file.

>> chown webmastercampus:web fruits_2.txt

chwon Command to Change the Specified Owner and User Login Group

If you remove the group name after the colon : the group of the file is changed to the specified user’s login group.

>> chown webmastercampus: fruits_2.txt

chown to Change the Group of a File

To change only the group of a file use the chown command followed by a colon (:) and the new group name (with no space between them) and the target file as an argument.

>> chown :web fruits_2.txt

chgrp Command to change the group ownership

chgrp is another command that you can use to change the group ownership of files.

>> chgrp web fruits_2.txt

To change the group ownership of the symlink itself, use the -h option. If the recursive option is not used, chown command changes the group ownership of the files to which the symlinks points, not the symbolic links themselves.

For example, if you try to change the owner and the group of the symbolic link sym_link1 that points to /var/www/webmastercampus, chown will change the ownership of the file or directory the symlink points to.

>> chown -h webmastercampus sym_link1

chown -R to Recursively Change the File Ownership

To recursively operate on all files and directories under the given directory, use the -R (–recursive) option:

>> chown -R webmastercampus:web /var/local/

The following example will change the ownership of all files and subdirectories under the /var/www directory to a new owner and group named www-data:

>> chown -R webmastercampus: /var/local/

If the directory contains symbolic links pass the -h option with R:

>> chown -hR webmastercampus: /var/local/

Other options that can be used when recursively changing the directory ownership are -H and -L.

-L tells chown to traverse each symbolic link to a directory that is encountered.

Note: Mostly, you don’t use these options because you might mess up your system or create a security risk.

chown –reference to Reference File

The –reference=ref_file option allows you to change the user and group ownership of given files to be same as those of the specified reference file (ref_file).

>> chown --reference=REF_FILE FILE

Note: If the reference file is a symbolic link chown will use the user and group of the target file.

For example, the following command will assign the user and group ownership of the file1 to file2

>> chown --reference=fruits_1.txt fruits_2.txt

chown Command Important Points

  • Owner is unchanged if missing.
  • Group is unchanged if missing, but changed to login group if implied by a ‘:’ following a symbolic OWNER.
  • OWNER and GROUP may be numeric as well as symbolic.

chown Command More Examples

Change the owner of /directory_important_files to “root”.

>> chown root /directory_important_files

Following chown Command change owner to root and group to “staff”.

>> chown root:staff /directory_important_files

Following chown Command change the owner of /directory_important_files and subfiles to “root”.

>> chown -hR root /directory_important_files

chown Command is use for changing the file’s user and/or group ownership.

chown Command Options

chown Command Options
Option Description
-c
--changes
It is used to display the detailed output like verbose; but it is reported when only a change is made.
-f
--silent --quiet
It is used to suppress the error messages.
-v
--verbose
It is used to display a diagnostic for every processed file.
--dereference It is used to affect the referent of each symbolic link.
-h
--no-dereference
It is used to affect the symbolic links instead of any referenced file.
--from=CURRENT_OWNER:CURRENT_GROUP It is used to change the specific owner and group.
--no-preserve-root It is used for not treating the backslash ('/') especially.
--preserve-root If the chown is failed to operate recursively on backslash ('/').
--reference=RFILE It is used to specify the RFILE's owner and group rather than their values.
-R
--recursive
It is used to perform operations on files and directories recursively.
--help It is used to display the help manual having a brief description of the usage and options.
--version It is used to display the version information.

chown Command in Linux (Documentation)

~$ man chown


NAME
       chown - change file owner and group

SYNOPSIS
       chown [OPTION]... [OWNER][:[GROUP]] FILE...
       chown [OPTION]... --reference=RFILE FILE...

DESCRIPTION
       This  manual  page documents the GNU version of chown.  chown changes
       the user and/or group ownership of each given file.  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.

OPTIONS
       Change  the  owner  and/or  group of each FILE to OWNER and/or GROUP.
       With --reference, change the owner and group of each FILE to those of
        RFILE.

       -c, --changes
              like verbose but report only when a change is made

       -f, --silent, --quiet
              suppress most error messages

       -v, --verbose
              output a diagnostic for every file processed

       --dereference
              affect  the  referent  of  each symbolic link (this is the de‐
              fault), rather than the symbolic link itself

       -h, --no-dereference
              affect symbolic links instead of any referenced  file  (useful
              only on systems that can change the ownership of a symlink)

       --from=CURRENT_OWNER:CURRENT_GROUP
              change the owner and/or group of each file only if its current
              owner and/or group match those specified here.  Either may  be
              omitted, in which case a match is not required for the omitted
              attribute

       --no-preserve-root
              do not treat '/' specially (the default)

       --preserve-root
              fail to operate recursively on '/'

       --reference=RFILE
              use RFILE's owner and group rather than specifying OWNER:GROUP
              values

       -R, --recursive
              operate on files and directories recursively

       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)

       --help display this help and exit

       --version
              output version information and exit

 Owner is unchanged if missing.  Group is unchanged  if  missing,  but
       changed  to  login  group  if  implied  by a ':' following a symbolic
       OWNER.  OWNER and GROUP may be numeric as well as symbolic.

EXAMPLES
       chown root /u
              Change the owner of /u to "root".

       chown root:staff /u
              Likewise, but also change its group to "staff".

       chown -hR root /u
              Change the owner of /u and subfiles to "root".
Created with love and passion.