Skip to main content
SysAdmin Shell Scripting Essentials

chown Unix Command: Syntax, Flags, and Troubleshooting

What is chown unix and when to use it?

chown is the Unix/Linux command to change file user and group ownership. Only root or a privileged user can transfer ownership to another user; file owners can change group to any group they belong to.

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

Syntax

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

OWNER may be a username or numeric UID. GROUP may be a group name or numeric GID. If only a colon is given (chown : FILE), the group is changed to the owner’s login group. Prefix a numeric owner with + to force numeric interpretation (avoid name collision).

Options and Flags

Flag Type Default Description
-c, –changes boolean off Report only when a change is made (implies -v).
-f, –silent, –quiet boolean off Suppress most error messages (e.g., file not found).
-v, –verbose boolean off Output diagnostic for every file processed.
-R, –recursive boolean off Operate on files and directories recursively. Combine with -H, -L, -P to control symlink traversal.
-h, –no-dereference boolean off Affect symbolic links themselves, not the target file.
–from=CURRENT_OWNER:CURRENT_GROUP pair none Change owner/group only if current owner/group matches the specified values.
–reference=RFILE file none Use owner and group of RFILE instead of specifying OWNER:GROUP.
–preserve-root boolean off Refuse to operate recursively on /.
–no-preserve-root boolean default Allow recursive operation on /.

Tested on Ubuntu 22.04 with coreutils 9.1.

Usage Examples

1. Change the owner of a single file

sudo chown master file1.txt

Transfers ownership of file1.txt to user master. The group remains unchanged. Requires root or sudo.

See also  Jenkins Crontab Syntax: CLI Reference & Troubleshooting

2. Change only the group

sudo chown :group1 file1.txt

Sets the file’s group to group1 without altering the owner. This is equivalent to chgrp group1 file1.txt.

3. Recursive change with verbose output

sudo chown -Rv master:developers /var/www/project

Recursively changes owner to master and group to developers for every file and directory under /var/www/project. The -v flag prints each file processed; -R traverses subdirectories.

4. Conditional change (–from)

sudo chown --from=oldadmin:legacy newadmin:newteam config.ini

Only changes ownership if the current owner is oldadmin and the current group is legacy. Useful for safe migrations.

5. Copy ownership from a reference file

sudo chown --reference=template.conf modified.conf

Applies the exact owner and group of template.conf to modified.conf. This avoids specifying UID/GID directly.

Troubleshooting & Common Errors

Error Message/Code Root Cause Resolution Command
chown: changing ownership of 'file': Operation not permitted Non-root user attempting to change owner to a different user. Use sudo chown user file or login as root.
chown: invalid user: 'nonexistent' User or group does not exist on the system. Verify with id nonexistent; create user if needed with useradd.
chown: cannot access 'file': No such file or directory Provided path does not exist. Check path with ls -la; ensure no typo.
chown: invalid group: 'group' Group name not found in /etc/group. Use numeric GID or create group with groupadd.

Exit Codes

Code Meaning Operational Impact
0 Success All requested changes completed.
1 Minor failure Some files could not be changed (e.g., permission denied). Report with -v or -c.
2 Serious failure Invalid arguments, missing required operand, or unrecoverable error.

chown unix — Performance Considerations and Tuning

Performance tuning of chown on Unix focuses on reducing filesystem metadata writes and output overhead. As documented in the GNU coreutils chown(1) man page (man7.org), the primary knobs are flags that limit the scope of changes and suppress unnecessary I/O.

  • Batch size via recursion: Use -R to apply ownership to a whole directory tree. To avoid unbounded output, pair it with --changes (-c) which reports only files whose ownership actually changes. This cuts the write volume to the terminal.
  • Selective filtering with --from: The --from=CURRENT_OWNER:CURRENT_GROUP flag skips files that do not match the current owner or group. This avoids useless metadata updates on files already correct — a key efficiency gain on large sets.
  • Symlink handling: Use --no-dereference (-h) to change the symlink itself rather than the referenced file. This reduces the number of metadata operations when only link ownership matters.
  • Suppress output with --silent (-f): Silently skip errors (e.g., on unreadable files) to prevent costly error message I/O. Default chown prints every error; -f suppresses them entirely, speeding bulk runs.
  • Reference mode: The --reference=RFILE option applies the owner/group of RFILE to the target files without requiring explicit user/group strings. Use it to apply the same ownership to many files with a single command invocation (one metadata pass per file, but no repeated parsing).
See also  chown Linux Command: Syntax, Recursive, Troubleshooting Guide

The following example applies the owner and group of template.txt to all files under /data, suppressing errors and reporting only changes:

chown -R --changes --reference=template.txt --silent /data

For deeper tuning on high‑performance workloads, consult the Linux kernel documentation (Documentation/filesystems/ext4.txt and sysctl/vm.txt) on filesystem journaling and inode cache sizing. Mount options like noatime reduce metadata updates and complement chown performance. No network buffering or MTU parameters affect chown directly, as it operates only on local filesystem metadata.

Frequently Asked Questions

What is the difference between chown USER:GROUP and chown USER.GROUP?

Answer: The colon syntax (user:group) is the only portable form across Linux, BSD, and macOS. The dot syntax is deprecated and may be interpreted as a username containing a dot if a user with that name exists. Use chown --from to restrict changes only from a specific owner/group, avoiding ambiguity.

# Correct modern usage
chown www-data:www-data /var/www
# Avoid dot (legacy)
chown www-data.www-data /var/www  # only works if no user named 'www-data.www-data'

When should I use the --from flag with chown?

Answer: Use –from CURRENT_OWNER[:CURRENT_GROUP] to change ownership only if the file currently matches the specified owner/group, preventing unintentional reassignment. This is critical in CI/CD scripts or migrations where you want to change ownership of files belonging to a specific user without affecting files already owned by others. Example: migrate only files owned by olduser to newuser.

# Change only files currently owned by olduser to newuser
chown --from olduser newuser -R /data

How do I fix “chown: changing ownership of ‘file’: Operation not permitted”?

Answer: Root ownership is required to change file ownership. On NFS exports, the default root_squash maps root to nobody, preventing changes. Remount with no_root_squash (insecure) or change ownership from the NFS server. For container environments, add the CAP_CHOWN capability.

# Use sudo to elevate
sudo chown user:group file
# On NFS client with root squash, run on server
ssh nfs-server "sudo chown user:group /export/path"

Does chown work on AWS Elastic File System (EFS) across all operating systems?

Answer: chown works on EFS when using NFSv4.1. By default, AWS applies root_squash which maps root to nfsnobody, causing Operation not permitted. To allow chown, configure an IAM policy granting elasticfilesystem:ClientRootAccess and mount with no_root_squash (using a custom EFS access point). On macOS (NFSv3) chown may fail due to missing protocol support; use Linux or mount with vers=4.1.

# Mount EFS with root access (requires IAM policy)
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-xxxx.efs.us-east-1.amazonaws.com:/ /mnt/efs

What is the fastest way to recursively change ownership of all files owned by a specific user in a large directory tree?

Answer: Use find with -exec ... + to reduce process creation overhead. If the filesystem has many small files, pipe via xargs for even better parallelism. Avoid chown -R --from because it walks all inodes; find with -user filters only matching files, drastically reducing I/O.

# Fast batch method
find /data -user olduser -exec chown newuser:newgroup {} +
# Even faster with parallel xargs
find /data -user olduser -print0 | xargs -0 -P $(nproc) chown newuser:newgroup