Javatpoint Logo
Javatpoint Logo

Chown Command in Linux/Unix with Examples

Linux chown command is used to change a file's ownership, directory, or symbolic link for a user or group. The chown stands for change owner. In Linux, each file is associated with a corresponding owner or group.

The Linux system may have multiple users. Every user has a unique name and user ID. If only a user is available in the system, the user will be the owner of each file.

The Linux system may have multiple users. Every user has a unique name and user ID. If only a user is available in the system, the user will be the owner of each file.

Users can be listed in different groups. The group allows us to set permission on the group level instead of setting permission on an individual level.

The file's ownership in the system may be only altered or edited by a super-user. The users can't give away a file's ownership even if the user owns it. Only a group member can modify the group ID of a file to that group. The chown command is there as an isolated package for Microsoft Windows as a component of the UnxUtils collection of native Win32 ports of basic GNU Unix-like utilities. Also, this command has been shipped to the IBM i OS.

In the operating system, different users have permission and ownership to guarantee that the files are protected and put limitations on who can change the file contents. There are distinct users who utilize the system in Linux:

  • A group can contain zero or multiple users. A user can be related to a "default group". Also, it can be a part of other groups in the system.
  • All users have a few properties related to them, like a home directory and user ID. We can insert users into any group for making the process of handling users easier.

However, the -P, -L, and -H options are exclusive manually; describing more than one isn't taken as an error. The last specified flag determines the nature of the command will show. The chown command modifies the directory or file ownership indicated by the link and not the link ownership itself when we haven't mentioned the -h flag and a symbolic link is known.

The chown command contains the opposite effects and modifies the link ownership itself and not that of the directory or file pointed by the link if we mention the -h flag. This command descends the described directories recursively if we describe both -R and -h flags.

Permissions and Ownership: To secure and protect directories and files in Linux, we can use permissions for controlling what a user can implement with a directory or file. Linux utilizes three different types of permissions, which are mentioned below:

  • Read: It permits the user for reading files and enables the user for reading directories or subdirectories saved inside it.
  • Write: It permits the user to delete and modify a file. It also permits a user to change its contents (delete, create, and rename files inside it) for directories. Changes make an impact on them until the execute permission isn't provided to the directory.
  • Execute: It permits it to get run on a file. For instance, if we have any file called exe.sh until we don't provide with it the execute permission, it will not run.

File Permission Types

  • User: These file permissions impact the file's owner.
  • Group: These file permissions impact the group, which is the owner of the file. The user permission will be used if the user owner is inside this group rather than the group permissions.
  • Other: These file permissions impact every other user in the system.

Syntax:

Below is the general syntax of the chown command:

Options:

Following are the command-line options of the chown command:

-c, --changes: It is used to display the detailed output like verbose, but it is reported when only a change is made.

-f, --silent, --quiet: It is used to suppress the error messages.

-v, --verbose: It is used to display a diagnostic for every processed file.

--dereference: It is used to affect the referent of each symbolic link.

-h, --no-dereference: It is used to affect the symbolic links instead of any referenced file.

--from=CURRENT_OWNER:CURRENT_GROUP: It is used to change the specific owner and group.

--no-preserve-root: It is used for not treating the backslash ('/') especially.

--preserve-root: If the chown is failed to operate recursively on backslash ('/').

--reference=RFILE: It is used to specify the RFILE's owner and group rather than their values.

-R, --recursive: It is used to perform operations on files and directories recursively.

--help: It is used to display the help manual having a brief description of the usage and options.

--version: It is used to display the version information.

Options

-H: The command shall modify the user ID (also group ID if mentioned) of the directory preferred by the symbolic link and every file inside the file hierarchy under it if the -R flag is mentioned and a symbolic link referring to a type directory file is mentioned on the command line.

-L: The command shall modify the user ID (also group ID if mentioned) of the directory preferred by the symbolic link and every file inside the file hierarchy under it if the -R flag is mentioned. A symbolic link referring to a type directory file is mentioned on the command line or known during a file hierarchy traversal.

-P: The command shall modify the owner ID (also group ID if mentioned) of the symbolic link when the system provides its support for this operation if the -R flag is mentioned and a symbolic link is mentioned on the command line or known during a file hierarchy traversal. The chown command will not pursue the symbolic link of other parts of a file hierarchy.

-R: If a symbolic link is known and the link represents a directory, the directory's ownership is modified, but the directory is not traversed further. Also, if the -P, -L, -H, and -h options are not specified when the link represents a directory, and the symbolic link is known, that directory's group ownership is modified, but the directory is not further traversed.

Display the UID, GID, and Groups

To display all the groups, execute the groups command as follows:

The above command will list all the existing groups from your Linux system. Consider the below output:

Linux chown command

To list the UID and GID, execute the id command as follows:

The above command will list all the UID and GID of their corresponding users and groups. Consider the below output:

Linux chown command

Display the User and Group Ownership of a File

To display the user and group owner of a file, execute the "ls -l" command with a specific file name. Consider the below command:

The above command will display the user and group ownership of the 'Demo.txt.' Consider the below output:

Linux chown command

Change the owner of a File (Using user name)

To change the owner of a file, pass the user name (new owner) with the chown command as follows:

Consider the below command:

The above command will set the jtp as the owner of the file 'Demo.txt'. Consider the below output:

Linux chown command

From the above output, the owner of the given file has changed.

Change the Owner of a File (Using UID)

We can change the owner of a file or directory using the UID of a user. To change the ownership by using the UID, pass the UID with chown command as follows:

The above command will change the ownership of the 'Demo.txt'. Consider the below output:

Linux chown command

Change the Group of a File

To change the group of a file, specify group name or GID with the chown command. It represents similar behavior as chgrp command. Execute the chown command specifying the group name separated by a colon as follows:

or

Consider the below command:

The above command will change the group of 'Demo.txt.' Consider the below output:

Linux chown command

From the above output, we have changed the group using the group name in the first command. In the second command, we have changed the group name using the GID.

Change the User and Group Name

We can change the user and group names together. To change the user and group name at once, execute the chown command as follows:

Consider the below command:

The above command will change the specified user name and group name together. Consider the below output:

Linux chown command





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA