chown Command Guide for Changing File Owners
chown changes who owns a file or directory — the user, the group, or both.
What is chown, and Why Use It?
Every file on a Linux system has an owner (a user) and an owner group. Together with the permission bits set by chmod, they answer the question “can you do this?”. chown (“change owner”) reassigns that ownership.
It’s the right tool whenever a file belongs to the wrong account — a web file owned by your personal user instead of the www-data service account, or a copied config still owned by root in a user’s home directory.
Why Learn chown?
- “Permission denied” on a file you should be able to touch usually means wrong ownership —
chownis the fix, notchmod 777. - Setting up servers (web, databases, shared folders) means handing directories to service accounts.
- Recovering from a
sudo cporsudo mv— files copied as root stay owned by root until you change that. - Backups and migrations need ownership preserved or corrected.
Rule of thumb: if you had to use
sudoto create or edit a file in a user’s directory, that file is now owned by root — andchownis how you give it back.
1. Change the Owner
chown newowner file
Example:
# The project files were copied with sudo and are owned by root
sudo chown alice project/
- You need to be root (or use
sudo) to give a file to another user. Regular users can only change ownership in narrow ways (e.g. giving a file away). - After the change, check it with
ls -l:
ls -l project/
2. Change the Owner and Group Together
The full form is user:group:
chown alice:devs file
Example:
# Owner alice, group devs
sudo chown alice:devs /srv/app/config.yml
# Keep the current owner but change only the group
sudo chown root:www-data /var/www/html/index.html
- The colon separates user from group; either side may be omitted.
chown :devs filechanges only the group (same aschgrp).
3. Change Ownership Recursively
chown -R alice:devs project/
Example:
# Hand an entire web directory to the web server's account
sudo chown -R www-data:www-data /var/www/myapp/
- ⚠️ Recursion on system paths is risky — always verify the path and preview with
findorls -Rfirst. -Rfollows into subdirectories and changes every file inside; permissions set bychmodare unaffected.
4. Change Ownership Using a Reference File
Copy the ownership of one file onto another with --reference:
chown --reference=template.txt newfile.txt
Example:
# Make every new log file match the ownership of an existing one
sudo chown --reference=/var/log/syslog /var/log/myapp.log
- Useful when you don’t remember exact usernames and want “whatever that file has”.
- Also works with
-Rfor whole trees.
5. Find and Fix Wrong Ownership
A common admin task: find files that shouldn’t be owned by root in a user directory.
find /home/alice ! -user alice
Example:
# Find everything in the web root not owned by the web server account
sudo find /var/www -not -user www-data
# Fix exactly those files
sudo find /var/www -not -user www-data -exec chown www-data:www-data {} +
-user <name>matches by owner;! -userinverts it.- The
-exec ... {} +form batches the fixes instead of runningchownonce per file.
6. Change Ownership When Copying
cp and rsync can set ownership during the copy if you’re root:
sudo cp --preserve=ownership source.txt dest/
rsync -a --chown=alice:devs source/ dest/
--preserve=ownershipkeeps the original owner instead of making the copy yours.- See
rsyncfor the transfer-oriented version.
Common Pitfalls
- Running
chownwithoutsudoon files you don’t own. You’ll get “Operation not permitted” — ownership changes require root. chown -Ron the wrong path. A typo likechown -R alice: /(note the stray colon/space) would hand the whole system to alice. Type the path slowly.- Forgetting the group.
chown alice filechanges the owner but leaves the old group; usealice:devswhen both matter. - Assuming a reboot fixes it. Ownership changes are permanent on disk; there’s no undo except changing it back.
- Changing ownership of running services’ files carelessly. A database or web server may refuse to start if its files have the wrong owner.
Related Pages
| Page | Why it’s related |
|---|---|
| chmod | The permission bits that ownership works with |
| chgrp | Change only the group |
| sudo | chown on other users’ files requires root |
| id | Check your own user and group names first |
| rsync | Transfer files with ownership preserved or set |
chown Cheatsheet
| Command | Meaning |
|---|---|
chown alice file | Change owner to alice |
chown alice:devs file | Change owner and group together |
chown :devs file | Change group only |
chown -R alice:devs dir/ | Recursively change owner and group |
chown --reference=a.txt b.txt | Copy ownership from an existing file |
sudo chown -R www-data /srv/app | Hand a directory to a service account |
find dir ! -user alice | Find files with the wrong owner |
ls -l file | Verify the current owner and group |