Linux Command Reference ยท Permissions and Users
Linux chown command
Reference for the Linux chown command: change the owner and group of files, recurse into directories, handle symlinks, and understand why only root can give files away.
chown ("change owner") sets the user that owns a file and, optionally, its group. Ownership decides which set of permission bits applies to whom: the owner's bits apply to the owner, the group's bits to members of the group, and the "other" bits to everyone else.
On Linux only root can change the owner of a file. Ordinary users cannot give files away (that would let them dodge disk quotas or plant files in another user's name), although the owner may set the group to one of the groups they belong to. For that reason chown is mostly used with sudo, for example after copying files into a web server's directory.
sudo chown alice:staff report.txt
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...
| Form | Effect |
|---|---|
alice |
Set the owner only. |
alice:staff |
Set the owner and the group. |
alice: |
Set the owner and change the group to alice's login group. |
:staff |
Set the group only (same as chgrp staff). |
alice.staff |
Old dotted form; deprecated, since user names may contain dots. |
Owners and groups can be names or numeric IDs. Use a leading + (+1000) to force a number when a name could be all digits.
| Option | Description |
|---|---|
-R, --recursive |
Operate on files and directories recursively. |
-v, --verbose |
Print a line for every file processed. |
-c, --changes |
Print only when something changes. |
-f, --silent |
Suppress most error messages. |
-h, --no-dereference |
Change a symlink itself, not its target. |
-H, -L, -P |
With -R: follow command-line symlinks, follow all symlinks, or follow none (default). |
--from=CUR |
Change only files whose current owner:group matches. |
--reference=RFILE |
Use the owner and group of RFILE. |
--preserve-root |
Do not recurse on /. |
Returns: 0 if every file was changed (or already had the requested owner), non-zero otherwise.
Errors: Operation not permitted for a non-root owner change or a foreign group; invalid user or invalid group for unknown names; No such file or directory.
Usage notes
- Changing ownership clears setuid and setgid bits on regular files, a safety measure so a program does not silently run as its new owner. Re-apply them with
chmodafterwards if needed. - Recursive danger.
chown -R user /varor a stray space (chown -R user /home/ user) can break a system. Use--preserve-rootand double-check the path. Never runchown -Ron a path built from an empty variable. -Rand symlinks. By defaultchown -Rdoes not follow symlinks into other trees, but it changes the target of a link named on the command line unless you pass-h.- Typical uses: fix a web directory after unpacking as root (
sudo chown -R www-data:www-data /var/www/site), give a user their home directory back, set up a service data directory. - Containers and bind mounts: user IDs are just numbers; a file owned by
1000on the host is owned by whatever user has ID 1000 in the container. Matching IDs matter more than names. - Network filesystems (NFS with root squash) may refuse
chowneven for root. - Find files by owner with
find / -user aliceorfind . -nouserfor orphaned files after a user was deleted. chgrpis the group-only form, and an owner can use it without root when changing to one of their own groups.
Related
chgrp,chmod,id,stat,find- Lessons: File Permissions and Ownership, Users, Groups and sudo