Change File Ownership with Chown

The complete guide to chown

Using the chown command, you can modify the ownership of a file, directory, or symbolic link on a Linux/Unix system. Each file on the system is linked to an owner and a group, and access permissions are granted to the owner, group members, and others. This tutorial will demonstrate the practical use of the chown command with examples.

How to use chown

Before delving into the usage of the chown command, it’s essential to understand its basic syntax. The chown command expression takes the following form:

chown [OPTIONS] USER[:GROUP] FILE(s)

USER can either be the username or the user ID (UID) of the new owner, and GROUP can either be the name of the new group or the group ID (GID). FILE(s) is the name of the files, directories, or links to be modified. Numeric IDs must be prefixed with the + symbol.

The following scenarios can be specified for the USER and GROUP options:

  • USER – If only the user is specified, the given user becomes the owner of the files, and the group ownership remains unchanged.
  • USER: – If the username is followed by a colon and the group name is not given, the user becomes the owner of the files, and the group ownership is changed to the user’s login group.
  • USER:GROUP – If both the user and group are specified (with no space between them), the user and group ownership of the files is changed to the given user and 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 changes are made.

By default, the chown command does not produce any output and returns zero on success.

Regular users are only able to modify the group ownership of a file if they are the owner of that file and a member of the intended group. On the other hand, administrative users have the ability to modify the group ownership of all files.

How to change the owner of a file

To modify the owner of a file, execute the chown command and provide the new owner’s username and the file to be modified as arguments:

chown USER FILE

For instance, the following command changes the owner of a file named file1 to a new owner named newuser:

chown newuser file1

To modify the ownership of multiple files or directories, list them as space-separated items. For example, the command below modifies the owner of a file named file1 and a directory named dir1 to a new owner named newuser:

chown newuser file1 dir1

You may also use the numeric user ID (UID) in place of the username. Consider this example, where the ownership of a file named file2 is altered to a new owner with a UID of 1000:

chown 1000 file2

In case a numeric owner already exists as a username, the ownership will be shifted to the username. To prevent this, prefix the ID with a + sign:

chown +1000 file2

How to change the group of a file

To change only the group ownership of a file, execute the chown command and provide the new group name after : without any intervening spaces, followed by the file to be modified:

chown :GROUP FILE

For instance, the following command modifies the group ownership of a file named file1 to newgroup:

chown : newgroup file1

Another command that you can use to modify the group ownership of files is chgrp.

How to change the owner and group of a file

To change both the owner and the group of a file, use the chown command and specify the new owner and group separated by : without any intervening spaces, followed by the target file.

chown USER:GROUP FILE

For instance, the following command alters the ownership of a file named file1 to a new owner named newuser and the group named users:

chown newuser:users file1

If you omit the group name after : in the command, the file’s group ownership is changed to the new owner’s login group:

chown newuser: file1

How to change file ownership recursively

To modify the ownership of all files and directories under a given directory recursively, add the -R (--recursive) option to the chown command:

chown -R USER:GROUP DIRECTORY

For example, to alter the ownership of all files and subdirectories under the /test/dir directory to a new group and owner named newgroup, execute:

chown -R newgroup: /test/dir

In case the directory includes symbolic links, use the -h option:

chown -hR newgroup: /test/dir

Other options that can be used for modifying the ownership of directories recursively are -H and -L. The -H option instructs the chown command to follow symbolic links that point to directories, while the -L option directs chown to traverse each symbolic link to a directory that is encountered. However, using these options may compromise your system or pose a security risk, so it’s best to avoid using them.

How to change file ownership with a reference file

You can set the user and group ownership of specified files to match those of a reference file using the --reference=REF_FILE option in the chown command. If the reference file is a symbolic link, chown will utilize the user and group ownership of the target file:

chown --reference=REF_FILE FILE

For instance, executing the following command assigns the user and group ownership of file1 to file2:

chown --reference=file1 file2