← Full reference

chown

Change a file's owner and group; chgrp — group only

chown sets a file's owner and group. They determine which permission triplet from chmod applies to a user. Only root can change the owner; a regular user can only change the group of their own file to one of their own groups (chgrp).

Syntax

chown [options] owner[:group] file…
chown [options] --reference=sample file…
chgrp [options] group file…

The owner and group can be given by name or by number (UID/GID). To see the current ones: ls -l file (third and fourth columns) or stat -c '%U:%G' file.

Forms

NotationWhat it changes
alicethe owner only
alice:devsowner and group
:devsthe group only (same as chgrp devs)
alice:the owner, and the group to alice's primary group
1001:1001by numeric UID and GID, even if no such user exists

Options

OptionWhat it does
-Rrecursively through a directory
-c / -vreport changed files / all files
-fdon't print most errors (the exit code is still non-zero)
--reference=sampletake the owner and group from another file
--from=old:oldgroupchange only files with the given current owner and/or group (--from=root, --from=:www-data)
--preserve-rootrefuse to operate recursively on /

Symbolic links

OptionWhat it does
(default)a file on the command line — the link's target is changed; inside -R links are not dereferenced
-hchange the owner of the link itself, not the target
-R -Hdereference links given on the command line and descend into them
-R -Ldereference all links to directories in the tree — dangerous, you can end up outside the directory
-R -Pdereference nothing (the default for -R)

chgrp has all these options too.

Examples

# hand the application directory to the www-data user and group
chown -R www-data:www-data /srv/app

# change only the group
chgrp devs report.txt
chown :devs report.txt

# the owner and their primary group
chown alice: notes.txt

# across the whole tree, change the owner only of files that belonged to root
chown -R --from=root alice ~/project

# copy the owner from a neighboring file
chown --reference=/etc/hosts /etc/hosts.new

# owner of the symbolic link, not of the file it points to
chown -h alice /srv/app/current

# show only actual changes
chown -Rc alice:devs /srv/shared

# find and fix files with an "orphaned" owner
find /srv -nouser -exec chown root:root {} +

# check the result
stat -c '%U:%G %n' /srv/app /srv/app/*

Pitfalls

  • The user must exist. chown bob file with no user bob gives "invalid user". Check with id bob or getent passwd bob. Same for groups: "invalid group".
  • A numeric UID with no user is accepted silently, and ls -l shows a number instead of a name. This happens with files from archives and container volumes.
  • setuid and setgid are cleared when the owner changes: a 4755 file becomes 0755 after chown. If you need the bit, run chmod after chown.
  • Symbolic links. Without -h, chown link changes the owner of the target file. With -R, links inside the tree aren't dereferenced — files outside the directory aren't touched, but with -L they are.
  • A dot instead of a colon (alice.devs) is an obsolete form: newer versions print a warning. Write alice:devs.
  • A regular user can't give a file away to someone else ("Operation not permitted"), and chgrp is allowed only to a group they belong to.
  • Changing the owner doesn't grant access. If the owner's permissions lack r or w, they still get "Permission denied". Check the permissions with ls -l.

See also

chmod · useradd · id · getent · umask · find · stat