chown - File Ownership Cheatsheet
What it is
chown is a command-line utility for changing the owner and group of files and directories on Unix-like systems. You reach for it when you need to grant or revoke access to files by changing who "owns" them.
Installation
chown is a core utility and is pre-installed on most Linux and macOS systems.
Linux: No installation needed.
macOS: No installation needed.
Windows:
chown is not a native Windows command. For similar functionality on Windows, you would typically use the icacls command or the graphical user interface (GUI) for file properties.
Core Concepts
- Owner: The user account that has primary control over a file or directory.
- Group: A collection of user accounts. Files and directories can be associated with a group, allowing multiple users within that group to share access.
- Permissions: While
chownchanges ownership, permissions (read, write, execute) determine what the owner, group, and others can do with the file.chownis often used in conjunction withchmodto manage access.
Commands / Usage
Changing the Owner
-
Change owner of a file:
chown alice my_document.txtMakes
alicethe new owner ofmy_document.txt. -
Change owner of a directory (and its contents recursively):
chown bob /var/www/htmlMakes
bobthe new owner of the/var/www/htmldirectory and everything inside it.
Changing the Group
-
Change group of a file:
chown :developers report.pdfChanges the group ownership of
report.pdfto thedevelopersgroup. The colon:indicates only the group is being changed. -
Change group of a directory (and its contents recursively):
chown :www-data /var/log/apache2Changes the group ownership of the
/var/log/apache2directory and its contents to thewww-datagroup.
Changing Both Owner and Group
-
Change owner and group of a file:
chown alice:developers config.ymlMakes
alicethe owner anddevelopersthe group owner ofconfig.yml. -
Change owner and group of a directory (and its contents recursively):
chown charlie:staff /home/charlie/projectsMakes
charliethe owner andstaffthe group owner of the/home/charlie/projectsdirectory and everything within it.
Using User and Group IDs (UID/GID)
You can also use numerical User IDs (UID) and Group IDs (GID) instead of names. This is useful when user/group names might not be consistent across systems or when dealing with system accounts.
-
Change owner using UID:
chown 1001 my_script.shSets the owner of
my_script.shto the user with UID1001. -
Change group using GID:
chown :1005 data.csvSets the group of
data.csvto the group with GID1005. -
Change owner and group using UID and GID:
chown 1001:1005 shared_file.datSets the owner to UID
1001and the group to GID1005forshared_file.dat.
Common Flags
-
-R,--recursive: Recursively change ownership of directories and their contents. This is one of the most frequently used flags.chown -R webmaster:www-data /srv/wwwChanges ownership of
/srv/wwwand all files/directories within it towebmasterandwww-datarespectively. -
-v,--verbose: Explain what is being done. Useful for scripts or when you want to see each file being processed.chown -v user1:group1 /data/*Shows each file in
/data/for which ownership is being changed touser1:group1. -
-c,--changes: Like verbose, but report only when a change is made.chown -c alice:users *.txtReports changes only for
.txtfiles that were actually modified. -
-H: If the target of a symbolic link is a directory, traverse it.# Example scenario: link_to_dir is a symlink to /path/to/actual/dir chown -R -H owner:group link_to_dirThis will change ownership recursively for files within
link_to_diras iflink_to_dirwere the actual directory/path/to/actual/dir. -
-L: Always follow symbolic links. If a symbolic link points to a directory, traverse it.chown -R -L owner:group my_project_dirEnsures all symbolic links are followed and their contents are processed recursively.
-
-P: Never follow symbolic links. This is the default behavior.chown -R -P owner:group my_project_dirOwnership changes will not be applied to the targets of symbolic links.
Common Patterns
-
Setting up web server directories:
sudo chown -R www-data:www-data /var/www/htmlCommonly used to give the web server user (
www-dataon Debian/Ubuntu,apacheon CentOS/RHEL) ownership of web root files. -
Giving a user full control over their home directory:
sudo chown -R $(whoami):$(whoami) $HOMEEnsures the current user owns all files and directories in their home directory.
$(whoami)dynamically inserts the current username. -
Changing ownership of newly created files (e.g., by a service): If a service creates files owned by
rootbut you want your user to manage them:sudo chown -R youruser:yourgroup /path/to/service/data -
Changing group for shared access:
sudo chown -R :project_team shared_files/Makes all files in
shared_files/belong to theproject_teamgroup, allowing members of that group to access them (assuming correct permissions are set viachmod).
Gotchas
-
sudois usually required: Changing ownership of files not owned by your user typically requires root privileges. Forgettingsudowill result in "Operation not permitted" errors.# This will likely fail unless you are the owner of /etc/passwd chown alice /etc/passwd # This is the correct way if you are not root sudo chown alice /etc/passwd -
Recursive (
-R) is powerful and dangerous: Be extremely careful when usingchown -R. Applying it to the wrong directory (like/) can break your system by changing ownership of critical system files. Always double-check the target directory. -
Symbolic Link Behavior (
-H,-L,-P): The default behavior (-P) is often what you want – not to follow symlinks. If you intend to change ownership of the target of a symlink, use-Lor-Hwith caution.-Lis generally safer than-Hif you want consistent recursive behavior across symlinks. -
User/Group Existence:
chownwill accept any number or name for a user/group. If the user or group doesn’t actually exist on the system, it might not cause an immediate error but can lead to confusion or access issues later. It’s best practice to ensure the target user and group exist. -
Changing Ownership of Mount Points: If you
chown -Ra directory that is a mount point for another filesystem (e.g., an external drive, NFS share),chownwill operate on the mounted filesystem, not the directory itself. This can lead to unexpected results or errors if the target filesystem doesn’t support the operation or has different ownership rules.