chown ‐ #directories #files #permissons - five4nets/Linux-Knowledgebase GitHub Wiki

Linux chown Command Tutorial

The chown command in Linux is used to change the ownership of files and directories. It allows you to modify the user and/or group ownership of a file or directory, which is essential for managing permissions and access control in a Linux system. This tutorial covers the basics of the chown command, its syntax, options, and practical examples.

Table of Contents

  1. Syntax
  2. Common Options
  3. Examples
  4. References

Syntax

The basic syntax of the chown command is:

chown [OPTIONS] [USER][:GROUP] FILE...
  • USER: The username or user ID (UID) to assign as the new owner.
  • GROUP: (Optional) The group name or group ID (GID) to assign as the new group.
  • FILE: The file(s) or directory/directories whose ownership you want to change.
  • OPTIONS: Modifiers that control the behavior of the command.

If you specify only the user, the group ownership remains unchanged. If you include a colon (:) followed by a group, both user and group ownership are changed.

Common Options

Here are some commonly used options for chown:

Option Description
-R Recursively change ownership of directories and their contents.
-v Verbose mode; displays a message for each file processed.
-c Similar to verbose but only reports changes made.
--reference=FILE Use the ownership of a reference file instead of specifying user/group explicitly.
-h Change ownership of symbolic links themselves instead of the files they point to.
--no-dereference Affect symbolic links instead of the referenced files (useful with -R).

Examples

Below are practical examples demonstrating how to use the chown command in various scenarios.

1. Change the Owner of a Single File

To change the owner of a file named example.txt to a user named alice:

chown alice example.txt

This assigns alice as the owner of example.txt, leaving the group unchanged.

2. Change Both Owner and Group

To change both the owner and group of a file, use a colon (:) to specify the group. For example, to set alice as the owner and developers as the group for example.txt:

chown alice:developers example.txt

3. Recursively Change Ownership of a Directory

To change the ownership of a directory and all its contents, use the -R option. For example, to set alice as the owner of the directory project and all files within it:

chown -R alice project/

4. Change Only the Group

To change only the group ownership, use a colon followed by the group name, omitting the user. For example, to set the group of data.txt to developers:

chown :developers data.txt

5. Use a Reference File

To copy the ownership of one file to another, use the --reference option. For example, to make file2.txt have the same owner and group as file1.txt:

chown --reference=file1.txt file2.txt

6. Change Ownership Using User and Group IDs

You can use numerical user IDs (UID) and group IDs (GID) instead of names. For example, to set ownership to UID 1001 and GID 1002 for data.txt:

chown 1001:1002 data.txt

7. Verbose Output

To see what changes are being made, use the -v option. For example:

chown -v alice:developers example.txt

Output:

changed ownership of 'example.txt' from user:group to alice:developers

8. Recursively Change Ownership of Symbolic Links

To change the ownership of symbolic links themselves (not the files they point to), use the -h option with -R:

chown -hR alice:developers /path/to/symlink

9. Change Ownership of Multiple Files

To change the ownership of multiple files at once, list them separated by spaces:

chown alice file1.txt file2.txt file3.txt

10. Check Ownership

To verify the ownership of a file or directory, use the ls -l command:

ls -l example.txt

Sample output:

-rw-r--r-- 1 alice developers 1234 Jun 25 14:08 example.txt

This shows alice as the owner and developers as the group.

Notes

  • You typically need superuser privileges (sudo) to change ownership of files you don’t own.
  • Be cautious when using chown -R on system directories, as it can affect system functionality.
  • Use man chown in the terminal for detailed documentation.

References