en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. Chown command in Linux: how to change file ownership?

Chown command in Linux: how to change file ownership?

Various operating system users are authorized and entitled to make sure the files are safe and limit who can access the directory structure. There are various people who use the Linux framework, and a user has such properties as a user identity and a home directory. We should have users in a community to facilitate the management of users. 

Linux Tutorial Jun 15, 21 by Nisal N 6 min Read
Chown command in Linux: how to change file ownership?

A community can have zero users or more. You may link to a 'default party' a specified person. It may also be part of other systems classes. So in this guide, you will get to know about the Chown command and various factors of this command in Linux. 

Permissions and ownership

In Linux, permissions are used to manage what a user can do with files and folders, protecting and safeguarding them. Permissions in Linux are divided into three types:

  • Read: This permit grants the user the ability to read files and folders, as well as folders and subfolders inside them.
  • Write: This consent allows a user to make changes to and remove files. It also helps users change folders' contents (create, erase, and rename files). Changes to directories do not affect them until they have the execute approval.
  • Execution: A file's write permission requires it to be executed. E.g., if we have a file called PHP.sh, it will not run until we grant it execute permission.

Permissions for Different Types of Files

  • User: The owner of the file is affected by these types of file permissions.
  • Group: The party that owns the file is affected by these types of file permissions. If the owner user is a community member, the user permissions will take precedence over the group permissions.
  • Other: The other users on the server are affected by these types of file permissions.

Note: To see the permissions we employ, go to:

ls -l 

The chown command is used to modify the owner of a file or a set of files. You may use the chown command to change ownership at any time.

How to Change a File's Ownership

Using the chown command with the new owner's user id as well as the goal file as a rationale to modify the ownership of a file:

chown USER FILE

The following order, for example, would shift the owner of a file called file2 to a new owner named linu:

chown linu file2

Specify several files or folders as a space-separated list to alter their possession. The following command transfers control of a file called file2 and a directory named dir2 to a new user named linu:

chown linu file2 dir2

Instead of a username, you can use a numeric user ID (UID). The following example would transfer ownership of file1 to a new owner with the UID 2000:

chown 2000 file1

If a user name with a numeric owner still exists, copyright would be passed to that user name. Prefix the ID with + to prevent this.

chown 2000 file1

How to Change a File's Owner and Group

Using the chown command followed by the current owner and group separated by a colon (:) with no intervening spaces and the goal file to modify both the owner and the group of a file.

chown USER:GROUP FILE

The command below can transfer control of a file called file2 to a new owner named linu, as well as community users:

chown linu:users file2

If the group name is omitted after the colon (:), the file's group is changed to the designated user's login group:

chown linu: file2

How to Change a File's Group

Use the chown command, followed by a colon (:), the current group name (without a space between them), and the target file as an excuse to modify only the group of a file:

chown :GROUP FILE

It will change the ownership party of a file called file2 to www-bit with the following command:

chown :www-bit file2

chgrp is another command that you can use to alter the party control of files.

How to Change the Status of ownership of Symbolic Links

When the recurrent alternative is not defined, the chown command only affects the community possession of the files that the symbolic links point to, not the symbolic links themselves.

If you want to modify the ownership and category of the symbolic connection symlink2 that points to /var/www/file2, chown can modify the control of the files and folders to which the symlink refers:

chown www-bit: symlink2

Rather than modifying the goal ownership, you are likely to get a "cannot dereference 'symlink2': Permission denied" failure.  The error exists since symlinks are shielded by default in most Linux distributions. You cannot run on target directories. 

You can find this setting in /proc/sys/fs/protected symlinks. A value of 1 indicates that it is available, while 0 indicates that it is disabled. We do not suggest disabling the symlink defence. Using the -h alternative to modify the party ownership of the symlink itself:

chown -h www-bit symlink2

How to Adjust File Ownership Recursively

Use the -R (—recursive) alternative to recursively function on all files and folders in the specified directory:

chown -R USER:GROUP DIRECTORY

The following example assigns ownership to all files and subdirectories in the /var/www directory to a new owner and party called www-bit:

chown -R www-bit: /var/www

Pass the -h alternative if the directory includes symbolic connections.:

chown -hR www-bit: /var/www

When modifying directory ownership recursively, you can use the -H and -L alternatives. The -H alternative causes the instruction to navigate a symbolic link that refers to a folder if the argument provided to chown is a symbolic link that points to a directory. 

The -L option instructs chown to follow each symbolic relation to a directory it encounters. Generally, you should avoid using these alternatives because they can cause the device to malfunction or pose a security danger.

Making Use of a Reference File

You may use the --reference=ref file option to adjust the user and group ownership of provided files to match the defined reference file (ref_file). If the reference file is a symbolic relation, chown may use the target file's user and party.

chown --reference=REF_FILE FILE

The following instruction, for example, would give file1 the user and community ownership of file2.

chown --reference=file2 file1

Conclusion

Chown is a command-line Linux/UNIX tool to change the administrator and the control of the directory. Explore your chown man page or enter chown in your console to understand much more about the chown function. You can visit our official website to learn more about the different functions of the Linux world. 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment