The chown command changes the owner and group of a file, and chgrp changes only the group. A typical call is sudo chown anna:developers report.txt, which makes anna the owner and developers the group. Changing the owner needs root privileges, while changing the group is allowed for the file's owner if they belong to the target group.
Ownership decides which permission triplet (owner, group or other) applies to each user, so fixing it is often the right answer to "permission denied".
ls -l shows the owner and group in the third and fourth columns:
Here the file belongs to root, so a normal user cannot edit it. To print only ownership, use stat:
| Command | Effect |
|---|---|
chown anna file | Set owner to anna, keep the group |
chown anna:developers file | Set owner and group |
chown :developers file | Set only the group |
chown anna: file | Set owner and use anna's login group |
chown --reference=a b | Copy owner and group of a onto b |
An older form with a dot (anna.developers) still works on many systems, but the colon is the portable standard. You can also use numeric IDs, for example chown 1000:1001 file, which is useful for files that belong to a user that does not exist on the machine, such as inside containers.
chgrp does one thing: it changes the group.
A regular user can do this only for files they own and only to a group they are a member of. Otherwise it fails with "Operation not permitted". The chown :developers form is equivalent, so many administrators use chown for everything.
Use -R to apply the change to a directory and everything inside:
Be careful with symbolic links. By default -R does not follow links to directories, which is the safe behaviour. The flags that change this are:
| Flag | Meaning |
|---|---|
-h | Change the link itself, not its target |
-H | Follow a link given on the command line |
-L | Follow every link (dangerous in trees with links to /) |
-P | Never follow links (default) |
Double-check the path before running a recursive chown as root. A typo such as chown -R anna / or a trailing space can rewrite ownership of the whole system and is hard to undo.
| Option | Use |
|---|---|
-v | Print each file that was processed |
-c | Print only files that changed |
--from=OLD | Change only files currently owned by OLD |
--preserve-root | Refuse to run recursively on / (default on modern GNU coreutils for -R) |
A safe routine for a recursive change:
ls -lR or find /var/www/site ! -user www-data | head.-c so you see what changes.find that no wrong owners remain.The --from=root option limits the change to files currently owned by root, leaving others untouched.
If any user could give files away, they could bypass disk quotas by handing files to others, or plant files owned by another user. For this reason the kernel allows only root (technically, a process with the CAP_CHOWN capability) to change a file's owner. A normal user can change the group only to one of their own groups.
When a service reports permission errors, check which user it runs as with ps -o user= -p PID, then give that user or its group the right access with chown and chmod.
chown can change the owner, the group or both, and chgrp changes only the group. Both are equivalent for group changes.
You are not root. Only root may change a file's owner, and a normal user may only change the group of their own files to a group they belong to.
No. It changes only owner and group. On modern Linux, changing the owner of a file that has setuid or setgid bits may clear those bits for safety.
Use chown -h anna link-name. Without -h, chown changes the target the link points to.
Use chown owner:group file to change both and chgrp group file to change the group only. Root is required to change the owner. Use -R with care, prefer -c and --from to limit and review changes, and remember that ownership and permission bits work together.