Signup/Sign In

Chown Command In Linux (File Ownership)

The chown command changes user ownership of a file, directory, or link in Linux. Every file is connected with an owner user or group. It is vital to establish file and folder permissions appropriately.

In this article, learn how to utilize the Linux chown command with examples supplied.

Prerequisites

  • Linux or UNIX-like system
  • Access to a terminal/command line
  • A user having sudo rights to alter the ownership. Remember to run the commands with sudo to execute them correctly.

Linux Chown Command Syntax

The basic chown command syntax consists of a few elements. The help file displays the following format:

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

[OPTIONS] – the command can be used with or without additional options.
[USER] – the username or the numeric user ID of the new owner of a file.
[:] – use the colon when changing a group of a file.
[GROUP] — altering the group ownership of a file is optional.
FILE – the target file.

Superuser rights are essential to perform the chown command.

On this article, we tested the command examples using the chown version 8.28 in Ubuntu 18.04.2 LTS.

To verify the chown version on your system, enter:

chown —version

How to Check Ownership of a File in Linux

First, you need to identify the original file owner or group before making ownership changes using the chown command.

To verify the group or ownership of Linux files and directories in the current location, execute the following command:

ls -l

An example output of the ls command looks like this:

How to Change the Owner of a File

Changing the owner of a file using chown requires you to provide the new owner and the file. The format of the command is:

chown NewUser FILE

The following command transfers the ownership of a file sample from root to the user test:

chown test sample

Change the owner of the file using chown command.

Use the same syntax to change the ownership for both files and folders.

Change the Owner of a File With UID

Instead of a username, you may give a user ID to modify the ownership of a file.

For example:

chown 1002 sample2

Make sure there is no user with the same name as the numeric UID. If there is, the chown command gives precedence to the username, not the UID.

Change Ownership of Multiple Linux Files

List the target file names after the new user to change the ownership for numerous files. Use single spaces between the file names.

In the following example, root will be the new owner of files sample2 and sample3.

chown root sample2 sample3

Combine file names and directory names to change their ownership with one command. For example:

chown root sample3 Dir1

Do not forget that the commands are case sensitive.

How to Change the Group of a File

With chown, you may change a group for a file or directory without affecting the owning user. The effect is the same as using the chgrp command.

Run the chown command using the colon and a group name:

chown :NewGroup FILE

The following example changes the group of the file sample3 from grouptest to group3.

chown :group3 sample3

Change the Group of a File Using GID

Similar to UID, use a group ID (GID) instead of a group name to alter the group of a file.

For example:

chown :1003 sample

Change Owner and the Group

chown :1003 sample

To assign a new owner of a file and change its group at the same time, execute the chown command in this format:

chown NewUser:NewGroup FILE

Therefore, to establish linuxuser as the new owner and group2 as the new group of the file sample2:

chown linuxuser:group3 sample3

Remember that there are no spaces before or after the colon.

Change Group to a Users Login Group

The chown command allocates the owner’s login group to the file when no group is supplied.

To do so, specify a new user followed by a colon, space, and the destination file:

chown NewUser: FILE
chown linuxuser: sample3

Transfer Ownership and Group Settings from One File to Another

Rather than setting the ownership to a particular user, you may utilize the owner and a group of the reference files.

Add the —reference argument to the chown command to replicate the settings from one file to another:

chown —reference=ReferenceFILE 

Remember to enter in the names of the files accurately to prevent the error message:

  • Transfer ownership and gropu settings between files using chown command.
  • Check Owner and Group Before Making Changes
  • The chown command —from option helps you validate the existing owner and group and then make modifications.

The chown syntax for verifying both the user and group looks like this:

chown —from=CurrentUser:CurrentGroup NewUser:NewGroup FILE

The example below shows we first checked the ownership and the group of the file sample3:

chown —from=root:group2 linuxuser:group3 sample3

Then chown changed the owner to linuxuser and the group to group3.

Conclusion

Now you know how to use chown command in Linux to alter a file’s user and/or group ownership.

Take extreme care while altering the group or ownership of a file or folder.