The chown command Linux

The basic philosophy of Linux all the operating system objects are files, for providing access to the different capabilities of the system, we just give the user access to the desired files or remove. In more detail, I talked about all the rights in the article of the right of access to files in Linux, here I can only say that each file has three sets of rights: for the owner, group, and others.

When you create a file, the user on whose behalf it was created becomes its owner and group set the primary group of the owner. But the file’s owner and group can be changed, for this use the commands chown and chgrp. In this article we will consider chown command linux, and basic examples of its use.

The chown command Linux

1. The syntax and options

The syntax of chown, as well as other similar linux commands are very simple:

$ chown user options /path/to/file

In the user field, it is necessary to specify the user that we want to transmit the file. You can also specify a colon group, for example, user:group. Then change not only the user but also the group. Here are the main options that you might need:

  • -c, –changes – detailed output of all the performed changes;
  • -f, –silent, –quiet – minimal information, hide error messages;
  • –dereference – change permissions for a file accessed by a symbolic link instead of the link itself (the default behavior);
  • -h, –no-dereference to change the permissions of symbolic links and not to touch the files to which they lead;
  • –from – change user only those files owned by the specified user and group.
  • -R, –recursive – recursively process all subdirectories;
  • -H – if passed a symbolic link to the directory – go for it;
  • -L – to follow all symbolic links in the directory;
  • -P – do not follow symbolic links on directories (the default).

Utility has several options, but this is the most basic and the majority of them you do not need. Now let’s see how to use chown.

2. Use chown

For example, we have several folders dir and owner user sergiy:

ls

Let’s change the owner of the folder dir1 in root:

chown root ./dir1

If you want to immediately change the owner and group of the directory or file, record them using a colon, for example, change user and group for the directory dir2 to root:

chown root:root ./dir2

If you want to have the changes applied not only to this directory, but all subdirectories, add the-R option:

chown-R root:root ./dir3

Then let’s change the group and owner to www-data for only those directories and files, which have owner and group root in the directory /dir3:

chown --from=root:root to www-data:www-data-cR ./

For reference the current directory, use path ./. We used it above. Next, specify the desired group by using the options –from and ask the tool to display the changes that it makes to the file system using the-c option.

Insights

The chown command is very simple and only allows you to change the owner and group. If you want more detail to set the permissions for owner, group, and others, you will need the chmod command. Right – quite a common problem with configuring different programs, two of these teams plus ls and namei can help to correct many mistakes.

Source: losst.ru

(Visited 24 times, 1 visits today)