chown

What is chown?

The chown command-line tool allows you to change the user and/or group ownership of a given file, directory, or symbolic link.

In Linux, all files are associated with an owner and a group and assigned with permission access rights for the file owner, the group members, and others. For more information about this, checkout the following link

How to use chown

# chown syntax, options = optional, group = optional
chown <OPTIONS> USER<:GROUP> FILE(s)

##### change owner of a file 
# syntax : 
chown <USER> <FILE>

chown linuxUser file1 # change owner of file1 to linuxUser
chown linuxUser file1 dir1 # change owner of file1 and dir1
chown 1000 file2 # change the owner of file2 to the user with UID 1000

##### change owner and group of a file
# syntax
chown <USER:GROUP> <FILE>

chown linuxUser:users file1 # change file owner to linuxUser, file group to users
chown linuxUser: file1 # change owner, change group to the user's login group

##### change group of a file
# syntax
chown <:GROUP> <FILE>

chown :www-data file1 # change group of file to the group www-data

##### change symbolic link ownership
chown www-data: symlink1 # change owner and group of symbolic link

##### change ownership recursively
chown -R www-data: /var/www # this will change the ownership of all files and subdirectories under the /var/www directory to a new owner and group named www-data
chown -hR www-data: /var/www # use -h when the directory contains symbolic links

##### using a 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). If the 
# reference file is a symbolic link chown will use the user and group of the target file.
# syntax 
chown <--reference=REF_FILE> <FILE>
chown --reference=file1 file2

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.

  • 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.

By default, on success, chown doesn’t produce any output and returns zero.

Use the ls -l command to find out who owns a file or what group the file belongs to:

ls -l filename.txt
-rw-r--r-- 12 linuxize users 12.0K Apr  8 20:51 filename.txt
|[-][-][-]-   [------] [---]
                |       |
                |       +-----------> Group
                +-------------------> Owner

Last updated