chown

file ownershiplinux
The chown command is one of the most frequently used commands in Linux/Unix-like operating systems. chown Change file owner and group

Quick Reference

Command Name:

chown

Category:

file ownership

Platform:

linux

Basic Usage:

chown [options] [arguments]

Common Use Cases

    Syntax

    chown [OPTION]... [OWNER][:[GROUP]] FILE...
    chown [OPTION]... --reference=RFILE FILE...

    Options

    Option Description
    -c, --changes Like verbose but report only when a change is made
    -f, --silent, --quiet Suppress most error messages
    -v, --verbose Output a diagnostic for every file processed
    --dereference Affect the referent of each symbolic link (default), rather than the symbolic link itself
    -h, --no-dereference Affect symbolic links instead of any referenced file
    --from=CURRENT_OWNER:CURRENT_GROUP Change the owner and/or group only if current owner/group match specified values
    --no-preserve-root Do not treat '/' specially (the default)
    --preserve-root Fail to operate recursively on '/'
    --reference=RFILE Use RFILE's owner and group rather than specifying values
    -R, --recursive Operate on files and directories recursively
    -H If a command line argument is a symbolic link to a directory, traverse it (with -R only)
    -L Traverse every symbolic link to a directory encountered (with -R only)
    -P Do not traverse any symbolic links (default) (with -R only)
    --help Display help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the chown command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Change the owner of a file
    chown user1 filename.txt
    # Change both owner and group of a file chown user1:group1 filename.txt
    # Change only the group using the owner: syntax chown :group1 filename.txt
    # Change owner of multiple files chown user1 file1.txt file2.txt file3.txt
    # Change owner using numeric user ID and group ID chown 1001:1001 filename.txt

    Advanced Examples:

    # Recursively change owner and group for a directory and its contents
    chown -R www-data:www-data /var/www/html
    # Copy ownership from a reference file chown --reference=reference.txt target.txt # Change owner and display what's being changed chown -v user1:group1 *.php # Change ownership only when current owner matches a specific user chown --from=olduser newuser *.txt # Change owner but don't follow symbolic links chown -h user1 symlink.txt # Preserve root directory safety when changing recursively chown --preserve-root -R user1:group1 /path/to/dir # Use the no-dereference option with recursive to change symlinks themselves chown -R -h user1:group1 /path/with/symlinks

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Key Features:

    The chown command is used to change the ownership of files and directories in a Linux or Unix-like operating system. It offers these capabilities:

    • Change user ownership, group ownership, or both simultaneously
    • Accept either names or numeric IDs for both users and groups
    • Apply changes recursively to entire directory trees
    • Handle symbolic links in different ways (follow or not follow)
    • Provide safety mechanisms to prevent accidental system-wide changes
    • Match ownership from reference files

    Ownership Specification:

    The owner and group can be specified in several formats:

    • user: Change the owner only
    • user:group: Change both the owner and group
    • :group: Change the group only
    • user:: Change the owner and set the group to the user's login group

    User/Group Specification:

    Both users and groups can be specified in two ways:

    • Name: Use the name as defined in /etc/passwd and /etc/group (e.g., john, developers)
    • ID: Use the numeric ID (e.g., 1001, 33)

    Important Considerations:

    • Root privileges (sudo) are typically required to change ownership
    • When using recursive changes with -R, be careful with system directories
    • The --preserve-root option prevents accidental changes to the root directory
    • Ownership changes on symbolic links can affect either the link itself or the target file, depending on options used
    • Permissions may be affected by changing ownership:
      • Setuid and setgid bits may be cleared on some systems for security reasons
      • Access to the file may change based on the new owner's permissions

    Use Cases:

    • Web development: Ensuring web files are owned by the appropriate web server user
    • System administration: Configuring proper ownership for system services and data
    • User management: Transferring ownership when users are added or removed from the system
    • Security hardening: Setting appropriate ownership to limit access to sensitive files
    • File transfers: Correcting ownership after copying or restoring files from backups

    Related Commands:

    • chgrp: Change only the group ownership
    • chmod: Change file permissions
    • ls -l: List files with ownership information
    • stat: Display detailed file information including ownership
    • id: Display user and group IDs

    Related Commands

    These commands are frequently used alongside chown or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the chown command works in different scenarios.

    $ chown
    View All Commands