How to Use chown Command in Linux

The chown command in Linux is a utility that will let you seamlessly change user ownership of any file. In Linux, the files are associated with an owner and a group, and the same can be altered.

Users in the operating system bags varied ownership and permissions altogether, and the scenes are no different with Linux. However, bringing alterations becomes necessary at times to ensure security. In this article, you’ll walk through the most comprehensive tutorial of using the chown command.

Before you start using the chown command in Linux, make sure you have access to all the following elements:

A Linux or UNIX-based system to operate on.

Full access to the system command line or Terminal with sudo privileges.

chown Command in Linux: Syntax

Just like any other command, the chown command in Linux has a basic syntax. The format comprises a few segments and looks something like this:

$ sudo chown [OPTIONS] USER [:GROUP] [FILE]s
chown command in Linux syntax

Here, the USER flag signifies the username or even the numeric user ID of the new file owner, where the FILE is the target. The OPTIONS segment is to serve additional choices. Besides, the GROUP flag is optional and is used to change the group ownership for any file.

USER – While using the chown command in Linux, the defined user will eventually become the owner alongside the files given when only the user is specified. However, in this case, the group ownership is not altered.

USER: – When you use a colon followed by the username in the chown command and the corresponding group name is not specified, the user will again become files’ owner, and the entire group ownership is assigned to the provided user’s login group.

USER:GROUP – When you define the user flag, and the group in a manner with no space between them, the ownership of the files is assigned to the specified user. For the group ownership, the same is given to the provided group.

:GROUP – While using the chown command in Linux, if you tend to omit the User segment and specify only the group with a colon in the prefix, the files’ group ownership is changed.

: When neither the user nor the group is specified, and only a colon is inputted alongside the chown command, the output sees no alteration.

Using chown Command in Linux

Usually, the chown command in Linux is used to alter the user ownership of any file, link, and directory. But before making any changes, it is essential to learn about the original file owner or group ownership. 

For checking the file ownership in the current location, launch the Terminal and run the following command:

$ ls -l
chown command in linux to check current user

Use chown Command in Linux to Change File Ownership: Username Version

To change the owner of a file and assign a new user with the chown command in Linux, you’ll need to specify the new user as the file owner. The general format of the code looks like this:

$ sudo chown NewUser FILE

For Example

$ sudo chown test sample
assigning new user chown command in Linux

Running the code as mentioned above changes the ownership of the sample file from the root user to the test user.

Use chown Command in Linux to Change File Ownership: User ID Version

In case using the desired username is not much of a flexible option, you can also specify the corresponding user ID alongside the chown command and change the ownership of a file.

For example:

$ sudo chown 1004 sample

This will instruct the system to make the user with UID 1004 the owner of the sample file. It is vital to cross-check that your system has no user with the same name. If it happens, the chown command will eventually prioritize the username over the UID.

Don’t know how to check UID? Well, the process is pretty straightforward.

Launch the Terminal hitting the “Ctrl+Alt+T” key combination.

Run the following command:

$ id -u USERNAME
check username chown command

chown Command in Linux for Changing the Ownership of Multiple Files

The chown command in Linux is also a great utility to change the ownership of multiple files. To do this, you’ll need to list all the target file names and input them after the new user section in the command line.

Make sure to use single spaces in between the file names.

For Example:

$ sudo chown root sample7 sample8
change ownership for multiple flies in Linux

Running the code mentioned above will assign root as the new owner for sample7 and sample 8.

You can also repeat the process for file names and directory names to change the ownership with the chown command. 

For example:

$ sudo chown root sample8 Directory1

Change the Group of a File Using chown Command in Linux

Employing the chown command in Linux, you can assign a new group for a file or directory. Simply specifying the group will make sure that the user remains unchanged.

Run the chown command in the following manner:

$ sudo chown :NewGroup FILE

For Example:

$ sudo chown :group1 sample1
chown command new group

The code changes the group of the specified file sample1 to group1.

Similar to user ID, the group ID (GID) can also be used in place of the group name.

For example:

$ sudo chown :1001 sample1

Changing Both the Owner and the Group

Are you looking to assign a new owner and also change the group for a file simultaneously? In that case, you’ll need to run the chown command in this format:

$ sudo chown NewUser:NewGroup FILE

For Example:

$ sudo chown Linuxes:group1 sample1
chown command group owner

Here, the sample1 undergoes a group change, and the owner is also changed to Linuxes.

Changing the Group to the User-Login Group

The chown command in Linux is also capable of assigning the desired owner’s login group to the respective file when you don’t specify any group.

Run the chown command in the following format:

$ sudo chown NewUser: FILE

Transferring the Ownership and Also Group Settings from One File to Another

Using the --reference flag alongside the chown command in Linux helps your system copy the settings from one file and transfer them to the other. Also, you can use the group and owner of a reference file instead of changing the ownership to any specific user,

$ sudo chown --reference=ReferenceFILE FILE
transfering ownershio in Linux

Use chown Command to Recursively Change File Ownership

The chown command is a great way to change the ownership of all the files and also the subdirectories within any specified directory recursively. All you need to do is add the -R flag.

$ sudo chown -R NewUser:NewGroup DirNameOrPath
recursively change file ownership in Linux

For changing the owner of a symbolic link, employ the -h flag together with the chown command. In other words, you’ll need to run the command in the following format:

$ sudo chown -h NewUser:NewGroup SymbolicLink
symbolic links in linux

How to Display chown Command Process Details

The default system doesn’t instruct the Terminal to display the detailed chown process information. However, you can do that using the -v flag and -c flag.

–v: It displays the process details even when there is no alteration in the ownership status.

-c: The command displays the process information only when there is an ownership alteration.

Suppressing the Command Errors

Running chown command in Linux can make you come across potential error messages. To avoid seeing such messages, use the -f flag. The option basically suppresses most of the error messages. However, in case you specify a username that is invalid, coming across the error message is inevitable.

Run the chown command in the following format:

$ sudo chown -f NewUser FILE

This is everything you need to know about the chown command in Linux. Using chown is pretty simple; however, it is vital to have discretion while altering the group or ownership details. 

If this guide helped you, please share it.

Leave a Reply
Related Posts