Read in 3 minutes

last updated 

Linux Chown Command

In Linux, all files are associated with an owner and a group. The chown command is used to change the user and group ownership of a given file, directory or link.

In this tutorial, we will show you how to use the chown command through simple examples.

Chown Command Syntax

Before going into how to use the chown command, let’s start by reviewing the basic syntax.

The chown command expressions take the following form:

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) and FILE(s) is the name of one or more files, directories or links.

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

To be able to change the ownership of a file, the user running the chown command must have sudo privileges.

How to Change the Owner of a File

To change the owner of a file use the chown command followed by the user name of the new owner and the target file.

chown USER FILE

For example, the following command will change the ownership of a file named file1 to a new owner named linuxize:

chown linuxize file1

To change the ownership of multiple files or directories specify them as a space-separated list. For example, the command bellow changes the ownership of a file named file1 and directory dir1 to a new owner named linuxize:

chown linuxize file1 dir1

The numeric user ID (UID) can be used instead of the username. The following example will change the ownership of a file named file2 to a new owner with UID of 1000:

chown 1000 file2

If a numeric owner exists as a user name, then the ownership will be transferred to the user name.

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 followed by the new owner and group separated by a colon (:) with no intervening spaces and the target file.

chown USER:GROUP FILE

The following command will change the ownership of a file named file1 to a new owner named linuxize and group users:

chown linuxize:users file1

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

chown linuxize: file1

How to Change the Group of a File

The chown command can performs the same function as the chgrp command, i.e it can change the file group.

To change only the group of a file use the chown command followed by a colon (:) and the new group name and the target file.

chown :GROUP FILE

The following command will change the owning group of a file named file1 to www-data:

chown :www-data file1

How to Recursively Change the File Ownership

To recursively operate on all files and directories under the input directory use the -r (--recursive) option:

chown -R USER:GROUP DIRECTORY

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

chown www-data: /var/www

Conclusion

By now you should have a good understanding of how to use the Linux chown command. If you want to learn more about the chown command visit the chown man page.