How do you change file ownership with chown?
chown owner:group file sets who owns a file; only root may hand a file to a different owner, while a regular user can at most change the group (and only to a group they belong to).
The root-only restriction exists to stop "quota dumping" and privilege tricks: if any user could give their files away, they could dodge disk quotas or plant a file under someone else's name. Group ownership is looser because you can only assign groups you're already in. Remember a freshly created file's owner is you and its group is your primary group — chown is how you change that afterward. Note chown follows symlinks by default; use -h to retarget the link itself.
chown owner file # Change owner only
chown owner:group file # Change owner and group
chown :group file # Change group only
chown -R owner:group dir/ # Recursive
Examples:
chown labstudent file.txt
chown labstudent:developers file.txt
chown -R www-data:www-data /var/www/
Who can use chown?
- root: Can change to any owner/group
- Regular users: Can only change group (to groups they belong to)
Related command - chgrp:
chgrp groupname file # Change group only
chgrp -R groupname dir/ # Recursive
Symbolic links:
chown user file # Changes link target
chown -h user link # Changes the link itself
Note: When a file is created, the owner is the creating user and the group is their primary group.
Go deeper:
chown(1) — OWNER:GROUP, -R, -h — the ownership syntax, recursive
-R, and the-hno-dereference symlink behaviour.