Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chown

Change the owner and/or the group of files and directories.

Level
RHCSA exam
Group
Permissions
Needs root
Yes
Package
coreutils

What it does

chown USER FILE gives a file to another user; chown USER:GROUP FILE also changes the group; chown :GROUP FILE changes only the group. Only root may change the owner.

With -R it works on a whole tree. Numbers (UID/GID) work as well as names, which helps when the user does not exist on this system (like files from a backup or NFS).

How to write it

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

Main parts

USER
Only the owner changes.
USER:GROUP
Owner and group change.
:GROUP
Only the group changes (same as chgrp).
USER:
Owner changes; group becomes the login group of USER.

Options 15

Most used

-R, --recursive
Change files and directories in the whole tree.
-v, --verbose
Report every file.
-c, --changes
Report only files that changed.
-f, --silent, --quiet
Do not show most errors.
-h, --no-dereference
Change a symbolic link itself, not the file it points to.
--dereference
Change the file a link points to (default).
--from=CURRENT_OWNER:CURRENT_GROUP
Change only files that have this owner/group now.
--reference=RFILE
Use the owner and group of RFILE.
--preserve-root
Refuse to work recursively on /.
--no-preserve-root
Do not treat / special (default).
-H
With -R: follow a link given on the command line.
-L
With -R: follow every link to a directory.
-P
With -R: follow no links (default).
--help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Give a file to a user

    chown lisa report.txt

    Only root can do this.

  2. 2Set owner and group

    chown lisa:sales report.txt

    Both in one step.

  3. 3Change only the group

    chown :sales report.txt

    Same as chgrp sales report.txt.

  4. 4Give a web app to the web server user

    chown -R apache:apache /var/www/html/app/uploads

    Only the upload folder; the code itself should stay owned by root (hardening).

  5. 5Show only what changed

    chown -Rc lisa:lisa /home/lisa
    changed ownership of '/home/lisa/old.txt' from root:root to lisa:lisa

    Good after you copied files as root into a home folder.

  6. 6Change only files of an old user

    chown -R --from=olduser newuser /srv/project

    Files of other owners stay as they are.

  7. 7Use numbers for files from another server

    chown -R 1500:3000 /srv/restore

    Works even when the names do not exist here.

  8. 8Copy the owner of another file

    chown --reference=/var/www/html/index.html /var/www/html/new.html

    Same owner and group as the reference.

  9. 9Change a symbolic link itself

    chown -h lisa /home/lisa/link-to-data

    Without -h the target file changes.

  10. 10Fix files after a UID change

    find / -xdev -uid 1500 -exec chown -h sam {} +

    All files of the old UID go to sam.

  11. 11Protect against a typo on /

    chown -R --preserve-root lisa /
    chown: it is dangerous to operate recursively on '/'

    Use it in scripts with variables.

Watch out

  • chown -R user / (for example from an empty variable $DIR/) breaks the system. Check paths in scripts.
  • A normal user cannot give files away.

Exit codes

0Done.
1Error.