Posted in

Admin Tips: chown

chown

In the next post in the Admin Tips series, we’ll explore the chown program.

The chown program changes the owner and/or group ownership of any given file.

If only the owner (username or numeric user ID) is provided, that user becomes the owner of each specified file, and the group is not changed. If the owner is followed by a colon and the group name (or numeric group ID), with no spaces between them, the file’s group is also changed. If the owner is followed by a colon but the group name is not provided, the file’s owner becomes the specified user, and the group is changed to the owner’s primary (reporting) group. If a colon and group are provided but the owner name is omitted, only the file’s group is changed. If only a colon or nothing is provided, nothing is changed.

Options
-c, –changes Similar to –verbose, but only reports changes made.
-f, –silent, –quiet suppresses most error messages.
-v, –verbose displays diagnostic information about each file processed.
–dereference works on files pointed to by symbolic links, rather than the links themselves.
-h, –no-dereference works on symbolic links rather than the files they point to.
–from=CURRENT-OWNER:CURRENT-GROUP changes the owner and/or group of each file only if the current owner and/or group match the specified values.
–no-preserve-root does not treat the / root directory specially.
–preserve-root refuses to operate recursively on /.
–reference=PATTERN-FILE uses the owner and group of PATTERN-FILE instead of the specified OWNER:GROUP values.
-R, –recursive operates recursively. on files and directories
-H if the command line argument is a symbolic link to a directory, follow it.
-L follows any symbolic link to a directory encountered.
-P does not follow any symbolic links (default).

The ‘chown’ program is part of the ‘coreutils’ package, which is available in most Linux distributions.

Syntax

chown NEWOWNER file1

Examples

Changes the owner of /tmp to “root”

sudo chown root /tmp

Similarly, but also changes its group to “staff”.

sudo chown root:staff /tmp

Changes the owner of /tmp and subdirectories to “root”

sudo chown -hR root /tmp

Changes the owner of the group to friends

sudo chown :friends /tmp

Change the owner only if the file is owned by a specific user

Using the “–from” flag, you can change the owner of a file (guest) only if that file is already owned by the specified owner.

chown –from=guest pawel file1

Change the group only if the file already belongs to the specified group.

Here, the “–from” flag is also used to change the group ‘friends’ to ‘family’ of file ‘file1’.

chown –from=:friends :family file1

Copy the owner/group settings from one file to another.

This is possible using the “–reference” flag; in this case, the owner of file ‘file1’ was copied as the owner of file ‘file2’.

chown –reference=file1 file2

For more information about chown, use the following commands:

chown –help
man chown

Click to rate this post!
[Total: 0 Average: 0]
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Accessibility Toolbar