Chown Command in Linux

Learn via video courses
Topics Covered

In Linux, file ownership is critical for security and user management. The chown command, or "change owner," allows users to modify file and directory ownership, facilitating access control. Administrators find this command valuable for managing resource access. Efficient use of chown safeguards files, preventing unauthorized changes. This article explores Linux file ownership fundamentals, providing insights into the versatile usage of the chown command. Understanding and effectively employing chown is essential for securing files on virtual private servers.

Chown Command Syntax in Linux

The syntax for the chown command in Linux is as follows:

Chown Command Syntax in Linux

Options

Chown command in Linux has numerous arguments that can be used to customise the command's behaviour.

The most commonly used options are as follows:

-R Modifies the ownership of all files and directories recursively under the specified directory.

-v Displays the ownership of altered files and folders in greater detail.

-c Only display output if ownership changes to files and directories have been made.

-h Modulates the ownership of symbolic links as opposed to the linked file.

--from=CURRENT OWNER[:CURRENT GROUP]Change ownership on files and directories held by the user or group specified.

Types of Permissions for Files

Each file and directory in Linux has an associated owner and group. Specific permissions granted by the owner of a file or directory define who can access and edit the file or directory. With the chmod command, the permissions of a file or directory can be modified.

There are three major file permission levels in Linux:

Read: Enables users to read the file or directory's contents.

Write: Permits users to edit the file or directory's contents.

Execute: Permits users to execute the file or access the directory's contents.

How Can You Check a File's Ownership in Linux?

Use the ls -l command to check the ownership of a file or directory in Linux. This command displays the file or directory's owner along with its permissions.

Change a File's Ownership

To change the owner of a file or directory in Linux, type chown followed by the new owner and the file or directory to be changed. Use the following command to transfer ownership of a file named example.txt to the user rose.

Change a File's Ownership

How to Change the Owner of a File with UID?

To change the owner of a file or directory using the UID (user ID) instead of the username, use the chown command followed by the file or directory to be changed and the new owner's UID. Use the following command, , for example,, to transfer ownership of the file example.txt to the user with UID 1000.

Change the Owner of a File with UID

Change Ownership of Multiple Linux Files

To change the ownership of multiple files or directories in Linux, use the chown command with the new owner and the files or directories to be changed separated by spaces. Use the following command to transfer ownership of two files named example1.txt and example2.txt to the user rose.

Change Ownership of Multiple Linux Files

Change the Group of a File

Let's explore the ways to change the group of a file with the options available.

Using GID

To change the group of a file or directory in Linux, type chown followed by (:) and then the new group and the file or directory to be changed. To change the group of a file named example.txt to test, for example, execute the following command:

Change the Group of a File Using GID

Change Owner and the Group

To change the owner and group of a file or directory in Linux, use chown followed by the new owner, a colon (:), the new group, and the file or directory to be changed. Use the following command to change the ownership of the file example.txt to the user root and the group test.

Change Owner and the Group

Change Group to a User's Login Group

To change the group of a file or directory to the login group of a user in Linux, type chown followed by the file or directory to be modified and a period (.). Execute the following command, for instance, to change the group of the file example.txt to the login group of the user surajit.

Change Group to a User's Login Group

How do You Transfer Ownership and Group Settings From One File to Another?

Use the chown command, followed by the reference file or directory and the destination file or directory, to transfer the ownership and group settings from one file or directory to another in Linux. Use the command below to transfer the ownership and group settings of the example.txt file to the example1.txt file.

Transferring ownership and group settings from one file to another

Conclusion

  • The chown command in Linux is used to change the ownership and group of files and directories.

  • Syntax of the chown command is "chown [OPTIONS][OWNER][:[GROUP]] FILE."

  • The chown command includes the -R, -v, -c, -h, and --from options.

  • In Linux, files and directories have ownership and permissions that determine who can access and modify them.

  • With the chown command, the ownership of a file or directory can be modified.

  • In Linux, there are three different kinds of file permissions: read, write, and execute.

  • Using chown with the - -reference option, the ownership and group settings of a file or directory can be transferred to another file or directory.