chown — Change File Owner and Group
Change file owner and group with chown — fix ownership after copying, set permissions for web servers and services, and apply changes recursively.
chown changes who owns a file or directory – its user and group. You reach for it constantly: ownership is often wrong after copying or extracting an archive, a web server such as www-data needs access to its files, or you want to reclaim control over a project directory. The chown user:group file syntax sets both at once, while a leading colon (:group) changes the group only. Be careful with the recursive -R flag: it rewrites ownership across entire directory trees, and aimed at system paths like / it can render your installation unusable. Almost every chown call needs sudo, because only root may change the owner of files it does not own.
Change Owner
chown <user> <file> — Change the owner of a file.
sudo chown www-data index.htmlchown <user>:<group> <file> — Change both owner and group.
sudo chown www-data:www-data index.htmlchown :<group> <file> — Change only the group (shorthand).
sudo chown :developers project/chown <user> <file1> <file2> — Change owner of multiple files.
sudo chown www-data *.html *.phpRecursive
chown -R <user>:<group> <dir> — Change ownership recursively for a directory.
sudo chown -R www-data:www-data /var/www/html/chown -R --preserve-root <user> <dir> — Recursive with protection against operating on /.
sudo chown -R --preserve-root deploy:deploy /opt/app/Options
chown -v <user> <file> — Verbose: report each change.
sudo chown -v www-data *.phpchown -c <user> <file> — Report only when a change is made.
sudo chown -c www-data *.phpchown --reference=<ref> <file> — Set ownership to match a reference file.
sudo chown --reference=index.html newfile.htmlchown -h <user> <symlink> — Change ownership of a symlink (not its target).
sudo chown -h www-data /var/www/currentCommon Patterns
chown -R www-data:www-data /var/www/ — Set web server ownership for a website directory.
sudo chown -R www-data:www-data /var/www/mysite/find <dir> -type f -exec chown <user> {} + — Change owner of files only (not directories).
sudo find /var/www -type f -exec chown www-data {} +find <dir> -type d -exec chown <user> {} + — Change owner of directories only.
sudo find /var/www -type d -exec chown www-data {} +chown $(id -u):$(id -g) <file> — Set file ownership to current user.
sudo chown $(id -u):$(id -g) downloaded-file.tar.gzchown -R $USER:$USER <dir> — Reclaim ownership of a directory for current user.
sudo chown -R $USER:$USER ~/projects/ Conclusion
chown is quick to reach for whenever a file's ownership is wrong – usually chown user:group file is enough, often combined with find -type f/-type d to treat files and directories separately. Treat the recursive -R variant with respect: it reaches into entire directory trees, and a typo in the path – especially in system areas like /, /etc or /usr – can break services or render the system unusable. Check the target path before you confirm, use --preserve-root as a safety net, and reach for -v/-c when in doubt to see exactly which changes happen. Closely related is chmod, which sets access permissions, while ls -l and stat show you the current ownership.
Further Reading
- GNU Coreutils: chown invocation – official reference for every option of GNU chown
- Wikipedia: chown – background on the Unix concept of file ownership