Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 — chown is the fix, not chmod 777.
  • Setting up servers (web, databases, shared folders) means handing directories to service accounts.
  • Recovering from a sudo cp or sudo 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 sudo to create or edit a file in a user’s directory, that file is now owned by root — and chown is 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 file changes only the group (same as chgrp).

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 find or ls -R first.
  • -R follows into subdirectories and changes every file inside; permissions set by chmod are 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 -R for 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; ! -user inverts it.
  • The -exec ... {} + form batches the fixes instead of running chown once 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=ownership keeps the original owner instead of making the copy yours.
  • See rsync for the transfer-oriented version.

Common Pitfalls

  • Running chown without sudo on files you don’t own. You’ll get “Operation not permitted” — ownership changes require root.
  • chown -R on the wrong path. A typo like chown -R alice: / (note the stray colon/space) would hand the whole system to alice. Type the path slowly.
  • Forgetting the group. chown alice file changes the owner but leaves the old group; use alice:devs when 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.

PageWhy it’s related
chmodThe permission bits that ownership works with
chgrpChange only the group
sudochown on other users’ files requires root
idCheck your own user and group names first
rsyncTransfer files with ownership preserved or set

chown Cheatsheet

CommandMeaning
chown alice fileChange owner to alice
chown alice:devs fileChange owner and group together
chown :devs fileChange group only
chown -R alice:devs dir/Recursively change owner and group
chown --reference=a.txt b.txtCopy ownership from an existing file
sudo chown -R www-data /srv/appHand a directory to a service account
find dir ! -user aliceFind files with the wrong owner
ls -l fileVerify the current owner and group