Download PuTTYgen - Putty key generator

Chown Command in Linux

Chown Command in Linux

In Linux and all Unix-like operating systems, ‘find’ is a command-line utility that locates files in one or more directory trees. More so, using the command, users can set specific search criteria and actions on files that match the search. Therefore it is a utility for file hierarchy, where not only the user can leverage it to find data but also perform successive operations on it.

Users can choose from several standards such as either search a matching filename or time range that matches when the file was accessed or modified. Users can search for files using the following criteria that find command supports – file pattern, name pattern, creation date, owner and permissions, folder, and modification date. While the command-utility can search for files in multiple directories, by default the ‘find command’ shows results of files in the current working directory.

On Unix-like operating systems, files and directories always belong to someone. In Linux, either they are associated with a single owner and a group. More so, each of the files and directories are assigned with special permission access rights.

Therefore, the programmer must remember the two aspects of files - ownership and permissions. Now, what if one wants to change the ownership or type of permission on a folder? It is possible by leveraging the chown command.

It is common knowledge among coders that Linux comes with simple, effective, and powerful commands, and so is chown. The expression is very beneficial as it allows a user to change the ownership of a file and a directory. Similarly, one can also change the permission access rights of a file or directory for the owner, group, or others using the chmod command.

The chown is one of the most widely-used commands, and therefore, we will explain in detail with examples on how to use the expression. On a system, users assign ownership and permission to secure their data and restrict who can view or modify the contents of the file. A file on Linux has owners and groups.

The owner is a user that has some properties associated with the system. Some of the properties include User ID, home directory, and others. Coders can choose to add users in a group for efficient system management. Meanwhile, a group of one or more users that share similar privilege on the filesystem.

Linux is an operating system that is designed for multiple users. In Linux, one can define multiple user accounts, and all can access the system simultaneously. Therefore, there is a need to maintain a record of files to determine which data belongs to whom. For the matter, Linux leverages uses ownership system where every file belongs to a user and a group.

So, the creator of the file - Owner, becomes the user, and similarly, the group where the file becomes the user’s current group. Both users and groups are defined by names and UID (Unique Identifier) and GID (Group Identifier).

Therefore, when you create a file, it belongs to you and the current group, which is the group you have signed into. Commonly, the group name is the same as the username and is, in most cases, it is created by default when one creates a new user on the system. To change the ownership values, programmers can use the chown command. Using chown, one can change the ownership and assign a new group.

A user must note that only the owner has the privilege to change the group ownership. Meanwhile, changing user ownership is only possible by root as it involves another user. Therefore, without root privileges, one can not force another user to adopt a particular file. Before we learn to execute chown command, let's look at the different types of ownerships and permissions.

Ownership and Permissions

While we have discussed file ownership, it is also essential to know what ownership permissions mean?

The Linux operating system is made to support a large number of users at the same time, and therefore, the system needs to know who is permitted to access a file. It not only provides ownership values but also defines how a user can access it and if one is allowed to make any changes to it or not.

Commonly, the access rules are known as permission, and there are three most types of file permissions. Let us understand each of them and know they mean.

  • User - The user permissions pertains to a single user who holds distinctive access to the file. Commonly, this particular user is known as the owner of the file.
  • Group - The group permission is for a group of users that have access to a file and is known as the ‘owning group.’
  • Other - The other permission applies to every other user on the system than the owner and the group. In some distributions, other users are known as the ‘world.’

Most of the descriptions of chown command and examples describe the GNU/Linux version.

Similarly, as the ownership of data is essential, permissions to access also play an important part in keeping files and directory secure. Therefore, Linux has permissions that define what a user can or can’t do. The three standard permissions include:

  • Read - The permission permits the user to read files, directories and subdirectories.
  • Write - With write permissions, a user can not only access files and directories but also modify and delete it. More so, one can also alter contents such as rename or even create a name file in the directory. But one must remember that until the ‘execute’ permission is not given to the user, the change won’t come into effect.
  • Execute - The execute permission allows the changes to be implemented.

Why Change File Ownership?

We will look into a few situations where there might be a need to change the file ownership. One such instance is when one has to transfer files between different Linux operating systems. In this case, one not only has to change the user but also the group owners on the new Linux system.

Another instance frequently occurs where an individual leaves an organization, and all the files owned by the employee are to be transferred to a new staff member. A user has to change the owner and group owner to the new team member. Another occurrence is when an individual authors a script that would be used by another specific user.

There are many other hypothetical situations where you might need to use chown. For instance, you create a file while logged in as root; therefore, it is owned by root. Another case is when one creates a directory as root but wants it to be accessed by another particular user.

Groups in Linux

In Linux, an individual can be a member of multiple groups at the same time. But a user can only have a single ‘current group.’ Therefore, the current group is known as users GID. As aforementioned, when a user creates a file, the ownership corresponds to the UID and GID.

For example, if John creates a new file, it is owned by John and his current group. In case John wants to transfer the ownership, then he has to execute the chown command. One must remember that only root can use the command to change the owner.

Every user has a configurable login group, and one can change it using the usermod command with -g option. Below is an example considering John as the owner of the file:

sudo usermod -g newlogingroup john

Use the id command with -g option to check the current group. Here is an example:

id -g

Result - 1002

The resultant is a user's numeric GID. In case one wants to see the name of the group, they can enter the following:

id -ng

Result - football

Similarly, to view all the group membership of a user, one has to type in ‘g’ in capital case.

id -nG

Result - football, sudo, usergroup, and team

Note: The command-utility prints the result directly without commas.

The above are all groups that John is a member of. Also, one must remember that every user on Linux has a private group that only has them as the member.

While other operating systems also use chown command, but their groups work differently. For instance, there are no private groups for the user in the macOS.


Before we jump right into using chown expression, let us take a look at how the basic syntax takes form.

chown [OPTION]… [OWNER][:[GROUP]] FILE… chown [OPTION]… –reference=RFILE FILE…

How to Use Chown

The new owner of a file is defined by the value - new-owner. It takes the following form:


More so, one can format the new-owner only in the following five ways:

  • USER - If one only executes the command with ‘USER,’ then only the owner of the file changes and not the group ownership.
  • USER: - When a command includes a username with a colon: and the group name is not specified, then the user becomes the owner, and the group ownership of the files changes to the user’s login group.
  • USER:GROUP - When both the user and group is defined, the file ownership is changed to the new user and group.
  • :GROUP - In this case, only the group switches to the given group.
  • : - If only a colon is specified, then the file ownership and group remains unchanged.

One must also remember that the exit status for chown is zero. So, when a user executes a successful chown command, it returns with zero. Also, users can view the permission of a file by simply running the below expression:

ls -l

Important Points

  • Users must also note that only users that have sudo privileges can switch the group ownership of a file. Therefore, owners can’t transfer the file ownership unless the owner is the root.
  • One can specify the user and group both by name or number.
  • The owner of a file can change the owning group only if he is a member of the group. However, members of the owning group can now switch file owning group. The root can change the owning group to any group.
  • One can also use the chgrp command instead of chown as they use the same system call.
  • Only the root can change the access time or modification time of a file.
  • It is always best to use the chown command as root or with sudo.


Command-line options, also known as command-line switches, are commonly leveraged to pass parameters to a program. They are one of the essential components of expression as they can make it easy to get results. More so, programmers must remember that syntax of command-line options differs among various operating systems.

  • -c and --changes - It displays only the files that are changed.
  • -v and --verbose - This shows the verbose information of all files processed.
  • -f, --silent, --quiet - It does not display output and activates the quiet mode.
  • --dereference - It will deference all symbolic links. Let's say if a file is a symlink so that it will change the owner of the file and nor the symlink.
  • -h and - --no-dereference - This is precisely the opposite of --dereference, whereby it will change the owner of the symlink instead of the file.
  • --from=currentowner:currentgroup - It will switch the owner or group of all files only if the owner or group or ‘currentowner’ or ‘groupowner.’
  • --no-preserve-root - Using the option, it will not consider the root directory in a specific manner.
  • -R,--recursive - It will recursively operate on file and directories.

How to Change the Owner of a File

One can use the chown command, followed by the username of the new-owner and target file. Here is an example:


Now let’s take an example and switch the ownership of file dubbed ‘trial’ to a new user ‘John.’

chown john trial

Now, what if one wants to change ownership of multiple files? It is easy! One only has to separate the file names by a ‘space.’ For example, we will change the ownership of file dubbed trial1 and directory dir1 to a new owner named ‘John.’

chown John trial1 dir1

One can use the UID instead of specifying the username. Let’s take the below example and change the ownership of the file dubbed ‘file5’ to a new owner with a UID - 1003. The expression will take the following form:

chown 1003 file5

In case the numeric ID or the UID also exists as a username, then the ownership will transfer to the username. One can avoid such confusion by adding a prefix of ‘+’ to the UID.

Change the Owner and Group of a File

We have learned how to change the owner of a file. Now let us also look at the chown command to alter both the owner and group of a file. One can do that in a single expression by splitting the owner and group by a colon (:). Here is how the chown command will take the form:


Let's take an example and change the file ownership of file3 to a new owner dubbed John and the group name ‘users’:

chown john:users file3

One can omit the group name after the colon to change the group to the new owner's login group.

chown john: file3

Users can also change the group of a file without altering the file ownership. The expression to change only the group is very simple. Below is how the command looks like:


The group name following the colon is the new group, and the target file is an argument. One must ensure not to leave any space between the colon and the new group name. Below is an example that changes the owning group of file dubbed ‘file3’ to the new group - contactsofjohn.

chown :contactsofjohn file3

While most programmers commonly use chown command, but it is not the only one. Many can also use the chgrp command to change the group ownership of files.

By default, the chown command changes the group ownership of files to which the symlinks directs. In other words, the chown does not change the owner and group of the symlink.

For example, if one does not use the recursive option, then the group and ownership of the ‘symlink3’ that directs to ‘var/www/file3, will remain the same. Commonly the command-line returns with the error - “cannot dereference ‘symlink3’: Permission denied”.

The error is standard as symlinks on most Linux distributions are protected. Therefore it denies users to operate on target files. One can disable the symlink protection, but we won’t recommend it at all.

To change the ownership of the symbolic links, one has to use the -h flag within chown expression. The -h flag stands for ‘-no-dereference,’ and it ensures that the command affects the symbolic links and not the referenced file. Here is how the expression will take form with the -h option.

chown -h www-data symlink3

Recursively Change File Ownership

Until now, we saw most examples on how to change the file ownership on single or multiple files, but now let’s look at how to recursively operate the chown command on all files and directories in a specific directory. To do so, one has to use the -R(--recursive) option, and it will take the following form:


Let’s take an example where we change the ownership and group of all files and subdirectories under the /var /downloads directory. The new owner and group will be john-home.

chown -R john-home: /var/downloads

In case the directory has symbolic links, then one has to use the -h option in conjunction with the -R option. Here is an example of the same:

chown -hR john-home: /var/downloads

A few other options that one can use are -H and -L option. The -H option traverse the chown command incase a symbolic link points to a directory. Similarly, the -L option ensures to traverse each symbolic link that passed through the chown expression. It is highly recommended not to use these options as one will risk crashing their entire system. One wrong command will create a significant security risk, and therefore, it is best to avoid using it.

Use a Reference File

The reference option ‘--reference=ref_file’ enables a user to alter the use and group ownership of files to the ones mentioned in a reference file(ref_file). It comes in convenient as it saves a lot of time and makes it easy to change file ownership and group. In case the reference file is a symbolic link, then chown command will take the user and group of the target file.

chown --reference=REF_FILE FILE

Now let’s take an example and allocate the user and group ownership of file2 to file3.

chown --reference=file2 file3

The above is a brief on how one can use the chown command to change the ownership and group of a file. We have covered most of the common ways to effectively use chown in Linux.