Using chown Command In Linux [Examples]

chown command in linux

chown command in Linux is a way allowing users to change or alter user or group ownership for a file, directory or link. in Linux operating system, actually all files are associated with a specific owner or a group. based on these entities, the users have access rights for files or directories. So, let’s check out the tutorial here regarding how to use chown command in Linux.

Understanding Owner, Group, and Permissions

While using any operating system, various users have ownership and permission to make sure if the files are secured on system. Also, this allows to put restriction on who can alter the contents of files, and so on. This is also applicable in case of using Linux operating system and here includes the fact points:

  • Every user name in Linux have their own properties like user ID, home directory, desktop and so on. And in order to manage those users easily, we can create a group.
  • A group can have no or multiple members. But, a user can be associated with a group “default group” if no custom group is created so far. A user can also be a member of different groups if assigned.

Ownership and Permissions

In order to control, protect and secure files in a Linux system, we use a number of permissions which are assigned to specific users. Means, it’s assigned what a user can do with a directory or file. Possibly, such permissions can be classified as Read, Write, and Execute, as explained here:

  • Read: Under this permission, the users can read the files and directories.
  • Write: This permission allows the users to modify or delete a file. Means, a user having Write permissions can modify the contents of a file or directory, even they can delete them as well. But, unless the execute permission is not granted, such made changes won’t be executed.
  • Execute: It’s already clear through Write permission that if Execute permission is not granted, the users won’t be able to run any file or directory.

Various permission types for files in Linux

  • User: If a file’s permission is granted to a user, it affects the owner of a file.
  • Group: In this case, the permission affect the group that owns the file. Despite this permission, the user permission will be applied if the owner user in in the group.
  • Other: This permission affects all other users on Linux OS.

To know about the file(s) ownership details, you can use ls -l command, and it will display the file’s or directory’s user or group details.

Example:

ls -l servo.txt

Syntax of chwon command and its explanation

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

In the mentioned syntax above, ‘USER’ is actually the username or user ID of a new owner, ‘GROUP’ is the name of a new group or group ID, ‘FILE(s)’ is the name of file or files, directories, links, etc. let’s check out how these entities are used and what they actually mean when used in following manner:

  • USER – If only the user is mentioned with chown, that user will become the owner for the files, however the group ownership privileges will not be altered.
  • USER: – When username with :, but no group name is specified, the user will become the owner of specified files. obviously, the files group ownership will also be altered to user’s login group.
  • USER:GROUP – in case if the user and the group are specified, the user ownership of the files will be altered to specified user. Also, the group ownership will be changes to specified group.
  • :GROUP – in case if the User is not specified, but the group is prefixed with ‘:’, only the group ownership of the files will be altered.
  • : – However, in case if only a colon is specified without user and group, no changes will be made.
Note: Normal users can change the group of files only if they own the file and only to the group to which they belongs to, on the OS. But, root users can change the group ownership of all files.

Changing Owner of File/Directory

In order to change the owner of a file using chown command in Linux distributions, the following syntax will be used:

chown USER FILE_NAME

Example: Changing owner of servo.txt to an owner named admin

chown admin servo.txt [Filename 2... in case if to change owner of multiple files/directories]

The same thing can also be accomplished using USER ID:

chown 02 servo.txt [Filename 2... in case if to change owner of multiple files/directories]

Changing the Owner and Group of File/Directory

In order to alter the owner and the group of a file using chown command in Linux,here’s the syntax:

chown USER:Group File_NAME(s)

Means, we want to change the ownership of a file servo.txt to a new owner admin and group users, here’s the command to run:

chown admin:users servo.txt

in case if the group name is omitted in above example, the group of the file is altered according to specified user’s login group:

chown admin: servo.txt

Changing the Group of File/Directory

If you wish to change the group of a file using chown command, we need to use the following syntax:

chown :GROUP FILE_NAME

For an instance, here we’ll change the group of a file servo.txt to www-data:

chown :www-data servo.txt

We can also use chgroup command to change the GROUP ownership of a file or directory, and is discussed in previous article.

Changing Symbolic Links Ownership

In case if we use no recursive option while using chown command to change the ownership of a symlink, it not just changes the group ownership of symlinks only, but the files or directories as well, to which the symlink actually points.

Suppose, you try to alter the owner and the group of symlink xyz pointing to /var/www/servo.txt, the chown command will attempt to change the ownership of the file or directory which the symlink related to. means, you will use the following command in this condition.

chown www-data: xyz

However, the above attempt will fail and deliver an error “cannot dereference ‘xyz’: Permission denied error”. It’s actually occurring because the symlinks are protected by default in most of the Linux distributions and the user’s can’t operate on target files. in order to enable or disable this feature, you can navigate to /prox/sys/fs/protected_symlinks. if the value is set to 1, means protection is enabled, otherwise 0 indicates that symlink protection is disabled.

Means, the above used syntax will run without errors only if the symlink protection is disabled.

But, in case if you intend to change the group ownership of specified symlink only, you will need to use -h option:

chown -h www-data xyz.

Changing Ownership of File Recursively

In order to change the ownership of a files or directories within a specified directory, we need to use -R option, and the syntax would be:

chown -R USER:GROUP Directory_NAME

For an instance, the below mentioned command will change the ownership of all files and directories within /var/www/html to a new owner and group named www-data:

chown -R www-data: /var/www/html

in case if the mentioned directory includes a symlink as well, we need to add -h option as well as discussed in previous examples, and the command should be run as:

chow -hR www-data: /var/www/html

Changing Ownership of File Using Reference File

In order to do so, we will use –reference=ref_file option, and the syntax would be like this:

chown --reference=REF_FILE FILE_NAME

Means, if we intend to change the ownership of a file named servo.txt to the user and group owner of file node.txt, the following command will be used:

chown --reference-node.txt servo.txt.

Important Tip: To get more detailed information on how to use chown command in Linux, type the following command in terminal, or visit chown man page here.

man chown