How do you change a file's owner and group with chown?
Use chown user file to change the owner and chown user:group file to change both. Add -R to apply it to a whole directory tree. Only root can give files away to another user; chgrp changes just the group.
Explain it simply
Every file has a name tag for its owner and another for its group. chmod decides what each tag may do; chown decides who the tags name.
Forms to remember: chown amit file changes the owner; chown amit:devteam file changes owner and group; chown :devteam file changes only the group (same as chgrp devteam file).
-R goes through every file and folder below. Be careful with it: chown -R amit /var would damage a server. Also note that -R follows nothing unusual by default, but a stray symlink can still surprise you, so check paths first.
Regular users may change the group of their own files only to a group they belong to, and cannot hand a file to someone else. Root can do anything. A very common real task: after uploading a website as root, run chown -R www-data:www-data /var/www/site so the web server can read it.
Example
ls -l app.log
# -rw-r--r-- 1 root root 120 ... app.log
sudo chown amit:devteam app.log
ls -l app.log
# -rw-r--r-- 1 amit devteam 120 ... app.logWhat to say in the interview
"chown owner:group path changes ownership, -R does it recursively, and chgrp changes only the group. Only root can change the owner. After deploying files as root I fix ownership with chown -R so the service user can read or write them."
Common mistakes
- Running chown -R on a wrong parent path such as / or /var.