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
| Notation | What it changes |
|---|---|
alice | the owner only |
alice:devs | owner and group |
:devs | the group only (same as chgrp devs) |
alice: | the owner, and the group to alice's primary group |
1001:1001 | by numeric UID and GID, even if no such user exists |
Options
| Option | What it does |
|---|---|
-R | recursively through a directory |
-c / -v | report changed files / all files |
-f | don't print most errors (the exit code is still non-zero) |
--reference=sample | take the owner and group from another file |
--from=old:oldgroup | change only files with the given current owner and/or group (--from=root, --from=:www-data) |
--preserve-root | refuse to operate recursively on / |
Symbolic links
| Option | What it does |
|---|---|
| (default) | a file on the command line — the link's target is changed; inside -R links are not dereferenced |
-h | change the owner of the link itself, not the target |
-R -H | dereference links given on the command line and descend into them |
-R -L | dereference all links to directories in the tree — dangerous, you can end up outside the directory |
-R -P | dereference 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 filewith no userbobgives "invalid user". Check withid boborgetent passwd bob. Same for groups: "invalid group". - A numeric UID with no user is accepted silently, and
ls -lshows 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
4755file becomes0755afterchown. If you need the bit, runchmodafterchown. - Symbolic links. Without
-h,chown linkchanges the owner of the target file. With-R, links inside the tree aren't dereferenced — files outside the directory aren't touched, but with-Lthey are. - A dot instead of a colon (
alice.devs) is an obsolete form: newer versions print a warning. Writealice:devs. - A regular user can't give a file away to someone else ("Operation not permitted"), and
chgrpis allowed only to a group they belong to. - Changing the owner doesn't grant access. If the owner's permissions lack
rorw, they still get "Permission denied". Check the permissions withls -l.