Files and directories
chown
Change the owner and/or group of files and directories.
chown [OPTION...] [OWNER][:[GROUP]] FILE...
Every file belongs to one user and one group. chown OWNER file changes the user,
chown OWNER:GROUP file both, and chown :GROUP file only the group (the same as chgrp).
Owners and groups can be given as names or numeric IDs.
Giving a file away is reserved for root: an ordinary user cannot make a file belong to someone
else, or the disk quota system and many security assumptions would fall apart. A regular user
may only change a file's group, and only to a group they belong to. That is why chown usually
appears with sudo, and why the examples here run as root.
The everyday cases are fixing ownership after copying files as root, handing a directory to a
service account (chown -R www-data:www-data /srv/site), and restoring ownership on files
extracted from an archive. Permission bits are a separate matter, set with chmod.
Examples
Ordinary users cannot give files away
chown root notes.txt
chown: changing ownership of 'notes.txt': Operation not permitted
Run as the regular dev user, the kernel refuses with "Operation not permitted". Changing
ownership needs root (or the CAP_CHOWN capability).
Change the owner
Setup run before this example
useradd -M alicechown alice notes.txt
ls -l notes.txt
-rw-rw-r-- 1 alice dev 190 Oct 25 05:30 notes.txt
The owner is now alice; the group (dev) is unchanged.
Change owner and group together
Setup run before this example
useradd -M alice
groupadd webchown alice:web notes.txt
ls -l notes.txt
-rw-rw-r-- 1 alice web 190 Oct 25 05:30 notes.txt
OWNER:GROUP sets both in one call.
Change only the group
Setup run before this example
groupadd webchown :web todo.txt
ls -l todo.txt
-rw-rw-r-- 1 dev web 62 Oct 24 21:28 todo.txt
With nothing before the colon, the owner stays and only the group changes, exactly what
chgrp web todo.txt does.
Owner plus that user's login group
Setup run before this example
useradd -M -U alicechown alice: notes.txt
ls -l notes.txt
-rw-rw-r-- 1 alice alice 190 Oct 25 05:30 notes.txt
A trailing colon with no group means "the owner's login group", here the alice group that
useradd -U created.
Change a whole directory tree
Setup run before this example
useradd -M -U deploychown -R deploy:deploy src
ls -lR src
src: total 3 -rw-rw-r-- 1 deploy deploy 49 Oct 28 03:10 Makefile -rwxrwxr-x 1 deploy deploy 363 Oct 28 09:43 app.py drwxrwxr-x 2 deploy deploy 3 Oct 28 06:26 lib -rw-rw-r-- 1 deploy deploy 83 Oct 28 15:30 main.c -rwxrwxr-x 1 deploy deploy 162 Oct 28 11:32 util.sh src/lib: total 1 -rw-rw-r-- 1 deploy deploy 88 Oct 28 07:32 helpers.py
-R applies the change to the directory and everything below it, the usual way to hand a
project to a service account.
Report only what actually changed
Setup run before this example
useradd -M -U deploy
chown deploy:deploy data/users.csvchown -c deploy:deploy data/*.csv
changed ownership of 'data/sales.csv' from dev:dev to deploy:deploy
-c prints a line only for files whose ownership changed; users.csv already belonged to
deploy, so only sales.csv is reported. -v would report every file.
Use numeric IDs
chown 1500:1500 todo.txt
ls -l todo.txt
-rw-rw-r-- 1 1500 1500 62 Oct 24 21:28 todo.txt
IDs need not exist in /etc/passwd. ls -l shows bare numbers when no name matches,
which is what you see on files extracted from another machine's archive or mounted from
another system.
Copy ownership from another file
Setup run before this example
useradd -M -U alice
chown alice:alice notes.txtchown --reference=notes.txt todo.txt README.md
ls -l notes.txt todo.txt README.md
-rw-rw-r-- 1 alice alice 230 Oct 24 02:38 README.md -rw-rw-r-- 1 alice alice 190 Oct 25 05:30 notes.txt -rw-rw-r-- 1 alice alice 62 Oct 24 21:28 todo.txt
--reference takes the owner and group from notes.txt and applies them to the other files.
Symlinks are followed unless you use -h
Setup run before this example
useradd -M alicechown -h alice link-to-notes
ls -l link-to-notes notes.txt
lrwxrwxrwx 1 alice dev 9 Oct 25 02:48 link-to-notes -> notes.txt -rw-rw-r-- 1 dev dev 190 Oct 25 05:30 notes.txt
-h changes the link itself. Without it, chown would change notes.txt, the target, and
leave the link alone.
Change only files owned by a particular user
Setup run before this example
useradd -M -U alice
useradd -M -U bob
chown alice:alice todo.txt notes.txtchown -v --from=alice bob todo.txt notes.txt README.md
changed ownership of 'todo.txt' from alice to bob changed ownership of 'notes.txt' from alice to bob ownership of 'README.md' retained as dev
--from=alice acts only on files currently owned by alice; README.md belongs to dev
and is retained, as -v reports.
A user name that does not exist
chown nobody2 notes.txt
chown: invalid user: ‘nobody2’
Names are looked up in the user database; an unknown name is an error. Numeric IDs are not checked.
Common pitfalls
chown -R user /path/*misses dotfiles at the top level because the glob does not match them. Usechown -R user /path(which also changes/pathitself).chown -Rfollows no symlinks by default, butchownon a single symlink name changes its target. Add-hwhen you mean the link.Recursive
chownon the wrong path (chown -R www-data /instead of./) breaks a system in ways that are hard to undo. Double-check before pressing Enter.On Linux, changing the owner of an executable clears its setuid and setgid bits, a deliberate safety rule; set them again afterwards if they are really needed.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
OWNER.GROUPwith a dot is an old, deprecated separator; use the colon everywhere.--reference,--fromand-care GNU options. BSD/macOSchownhas-R,-h,-vand the-H/-L/-Psymlink controls.
Options worth knowing
A selection, described in our own words. For the full list, see chown(1) or chown --help.
| Option | What it does |
|---|---|
-R, --recursive | Operate on directories and everything below them. |
-h, --no-dereference | Change symlinks themselves, not their targets. |
-v, --verbose | Report every file processed. |
-c, --changes | Report only files whose ownership changed. |
-f, --silent | Suppress most error messages. |
--reference RFILE | Use RFILE's owner and group. |
--from CURRENT_OWNER:CURRENT_GROUP | Change only files that currently have this owner and/or group. |
--dereference | Change the targets of symlinks (the default). |
--preserve-root | Refuse to operate recursively on /. |
-H | With -R, follow a symlink given on the command line. |
-L | With -R, follow every symlink to a directory. |
-P | With -R, follow no symlinks (the default). |
Related
lsList the contents of directories, or the details of individual files.statShow a file's metadata (size, permissions, owner, inode, timestamps), or print chosen fields in your own format.idPrint the user and group IDs of the current user or of a named user.groupsPrint the groups the current user, or each named user, belongs to.installCopy files into place and set their mode, owner and group in one step; can also create directories.